Below code works for -ve numbers : public static …
vikramdpatil - May 4, 2014
Below code works for -ve numbers :
public static int findMaxSum(int arr[]) {
if (arr.length == 0) return 0;
if (arr.length == 1) return arr[0];
int a = arr[0];
int b = Math.max(arr[0], arr[1]);
int c = b;
for(int i=2; i<arr.length; i++) {
c = Math.max(arr[i], Math.max(b, arr[i] + a));
a = b; // a now becomes second last sum
b = c; // b now becomes previous sum
}
return c;
}
Please correct me if i am wrong.
Nice answer - I ideone it, and it was correct - http://ideone.com/TYBCai.
Thanks Vikram.