Skip to content

Commit 83ec3ed

Browse files
committed
max sub array sum
1 parent 4b4c078 commit 83ec3ed

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

divideAndConquer/maxSubarray.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
int start = 0;
4+
int sum = 0;
5+
int maxSum = Integer.MIN_VALUE;
6+
for (int end = 0; end < nums.length; end++) {
7+
sum = sum + nums[end];
8+
maxSum = Math.max(maxSum, sum);
9+
10+
if (sum <= 0) {
11+
sum = 0;
12+
}
13+
System.out.println("Sum is " + sum + " MaxSum is " + maxSum);
14+
}
15+
return maxSum;
16+
}
17+
}

0 commit comments

Comments
 (0)