-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumSubarray.java
57 lines (45 loc) · 1.36 KB
/
MaximumSubarray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ProjectFiles;
public class MaximumSubarray {
public static void main(String[] args) {
System.out.println(m(new int[]{-2,-3,-1} ,3));
System.out.println(maxSubArray(new int[]{1,3,-5,10} ));
System.out.println(maxSubArray(new int[]{-1,89,-4} ));
System.out.println(maxSubArray(new int[]{1,3,4} ));
System.out.println(maxSubArray(new int[]{-1,3,4} ));
System.out.println(maxSubArray(new int[]{-2,-3,-1} ));
System.out.println(maxSubArray(new int[]{-1,-3,0,-9,7} ));
}
// This code covers all the cases including the all negative numbers.
public static int maxSubArray(int[] nums) {
if(nums.length == 0){
return 0;
}
if(nums.length == 1)
return nums[0];
int maxSoFar = Integer.MIN_VALUE;
int sum = 0;
for(int i = 0; i < nums.length ; i++){
sum += nums[i];
if(sum > maxSoFar){
maxSoFar = sum;
}
if(sum < 0){
sum = 0;
}
}
return maxSoFar;
}
//This code doesn't cover the all negative numbers case.
public static int m(int[] a, int size){
int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
}