-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArray.java
More file actions
47 lines (41 loc) · 1.21 KB
/
MaxSubArray.java
File metadata and controls
47 lines (41 loc) · 1.21 KB
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
package com.ub.interview;
public class MaxSubArray {
/**
* Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
* @param A
* @return
*/
public int MaxSubArray(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int len = A.length;
int sum = 0;
int maxSum = -999;
int min = -999;
for(int i=0;i<len;i++) {
sum += A[i];
if(sum < 0) {
sum = 0;
}
if(maxSum < sum) {
maxSum = sum;
}
/**
* For corner cases [-1,0] [-1,-2,-3]
*/
if(min < A[i])
min = A[i];
}
if(len >= 1 && maxSum == 0) {
maxSum = min;
}
return maxSum;
}
public static void main(String args[]) {
MaxSubArray max= new MaxSubArray();
int sample[] = {-1,0};
System.out.println ("max = "+max.MaxSubArray(sample));
}
}