-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaxSubArray.java
51 lines (46 loc) · 1.38 KB
/
MaxSubArray.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
/**
* Created by sidharthan on 6/2/16.
*/
import java.util.Scanner;
public class MaxSubArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] res1 = new int[n];
int[] res2 = new int[n];
for (int i = 0; i < n; i++) {
subArray(res1, res2, i, scan);
}
for (int i = 0; i < n; i++) {
System.out.println(res1[i] + " " + res2[i]);
}
}
static void subArray(int[] res1, int[] res2, int i, Scanner scan) {
int t = scan.nextInt();
if(t==0)
{
return;
}
int max = 0, currMax = 0, currNCMax = 0, maxNC = 0;
int vals = scan.nextInt();
//Choose first number as max
currMax = vals;
currNCMax = vals;
maxNC = vals;
max = vals;
for (int j = 1; j < t; j++) {
vals = scan.nextInt();
currMax = Math.max(currMax + vals, vals);
max = Math.max(currMax, max);
//If the negative number in array, check its greater or not.
if (currNCMax + vals > vals) {
currNCMax = Math.max(currNCMax + vals, currNCMax);
} else {
currNCMax = vals;
}
maxNC = Math.max(currNCMax, maxNC);
}
res1[i] = max;
res2[i] = maxNC;
}
}