Maximum Subsequence Sum
Given array of +ve and -ve numbers. Find maximum subsequence sum in it.
For example, A={10,-12, 20, 5, -15, -10, 4, 7, 0, -11, 15} o/p: Max subsequence sum is 25.
Sol:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| //Find maximum subsequence sum from given array of +ve and -ve numbers int MaxSubsequenceSum( int [] a) { int maxSum = nums[0], tempSum=nums[0];
for(int i=1; i<nums.length; i++) {
tempSum = nums[i]+Math.max(0, tempSum);
if(tempSum>maxSum){
maxSum = tempSum;
}
}
return maxSum; } |
No comments:
Post a Comment