-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMxxSubArraySum.java
executable file
·52 lines (40 loc) · 1.18 KB
/
MxxSubArraySum.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
/* package joney_000 */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class MaxSubArraySum
{
public static long ans=0;
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
int tests=Integer.parseInt(br.readLine());
for(int t=0;t<tests;t++){
int n=Integer.parseInt(br.readLine());
int num[]=new int[n];
String[] s=br.readLine().split(" ");
for(int i=0;i<n;i++){
num[i]=Integer.parseInt(s[i]);
}
ans=0;long tempSum=0;
/* //does not handle the case all array element are negative.
for(int i=0;i<n;i++){
tempSum=+num[i];
if(tempSum<0){
tempSum=0;
}
ans=Math.max(ans,temp);
}
*/
//handle the all negative case
ans=-100000000000L;
for(int i=0;i<n;i++){
tempSum=Math.max(num[i],tempSum+num[i]);
ans=Math.max(ans,tempSum);
}
out.write(""+ans+"\n");out.flush();
}
}
}