You are given an integer array C of size A. Now you need to find a subarray (contiguous elements) so that the sum of contiguous elements is maximum. But the sum must not exceed B.
1 <= A <= 10^3
1 <= B <= 10^9
1 <= C[i] <= 10^6
The first argument is the integer A.
The second argument is the integer B.
The third argument is the integer array C.
Return a single integer which denotes the maximum sum.
Input 1:
A = 5
B = 12
C = [2, 1, 3, 4, 5]
Input 2:
A = 3
B = 1
C = [2, 2, 2]
Output 1:
12
Output 2:
0
Explanation 1:
We can select {3,4,5} which sums up to 12 which is the maximum possible sum.
Explanation 2:
All elements are greater than B, which means we cannot select any subarray.
Hence, the answer is 0.
function maximumSubarray(A, B, C){
if(A == 1){
return C[0]
}
let max = Number(0);
for(let i = 0 ; i < A ; i++){
let sum = Number(0)
for(let j = i ; j < A ; j++){
sum = Number(sum) + Number([j])
}
if(sum > Number(max)){
max = Number(sum)
}
if(sum == Number(B)){
return sum
}
}
return max
}