-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestContiguousSumArray.java
More file actions
34 lines (25 loc) · 1017 Bytes
/
LargestContiguousSumArray.java
File metadata and controls
34 lines (25 loc) · 1017 Bytes
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
import java.util.*;
import java.lang.*;
import java.io.*;
class LargestContiguousSumArray {
public static void main (String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
for(int i = 0 ; i < test_cases ; i++){
int input_len = Integer.parseInt(br.readLine());
int[] num_array = new int[input_len];
String[] input = br.readLine().split(" ");
int max_so_far = -1000;
int max_here = 0;
for(int x = 0 ; x < input_len ; x++){
num_array[x] = Integer.parseInt(input[x]);
int element = num_array[x];
System.out.println("Element " + input[x] + ", max_here " + max_here + ", max_so_far " + max_so_far);
max_here = Math.max(max_here + element,element);
max_so_far = Math.max(max_so_far, max_here);
System.out.println("Element " + input[x] + ", max_here " + max_here + ", max_so_far " + max_so_far);
}
System.out.println(String.valueOf(max_so_far));
}
}
}