forked from Coder-forfun/Hactoberfest-accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadaneAlgorithm
More file actions
32 lines (32 loc) · 690 Bytes
/
KadaneAlgorithm
File metadata and controls
32 lines (32 loc) · 690 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
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class KadaneAlgo {
static int maxSubArraySum(int a[], int size)
{
int sum = a[0];
int curr_max = a[0];
for (int i = 1; i < size; i++)
{
curr_max = Math.max(a[i], curr_max+a[i]);
sum = Math.max(sum, curr_max);
}
return sum;
}
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++)
{
a[i]=sc.nextInt();
}
System.out.println(maxSubArraySum(a,n));
}
}
}