forked from rakhi2207/java-programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadanesAlgorithm.java
More file actions
32 lines (29 loc) · 882 Bytes
/
KadanesAlgorithm.java
File metadata and controls
32 lines (29 loc) · 882 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
import java.util.*;
public class KadanesAlgorithm
{
public static int maxSubArraySum(int[] array)
{
int i,currSum,maxSum;
maxSum=currSum=array[0];
for(i=1;i<=array.length-1;i++)
{
currSum=Math.max((currSum+array[i]),array[i]);
if(currSum>maxSum)
maxSum=currSum;
}
return maxSum;
}
public static void main(String[] args)
{
int i,n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the array: ");
n=sc.nextInt();
int[] arr=new int[n];
System.out.print("\nEnter array elements: ");
for(i=0;i<=n-1;i++)
arr[i]=sc.nextInt();
sc.close();
System.out.println("\nMaximum Subarray sum of the given array: "+maxSubArraySum(arr));
}
}