Skip to content

Commit 86fcc0f

Browse files
authored
Create KadanesAlgorithm.java
Kadane's Algorithm Java Code
1 parent f34fe4d commit 86fcc0f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
5+
class Kadane
6+
{
7+
public static void main (String[] args) throws java.lang.Exception
8+
{
9+
Scanner sc=new Scanner(System.in);
10+
System.out.print("Enter the size of the array : ");
11+
int n=sc.nextInt();
12+
int[] arr=new int[n];
13+
System.out.println("Enter the elements of the array : ");
14+
for(int i=0;i<n;i++){
15+
arr[i]=sc.nextInt();
16+
}
17+
int result=Integer.MIN_VALUE,max_ending_here=0;
18+
for(int i=0;i<n;i++)
19+
{
20+
max_ending_here+=arr[i];
21+
22+
if(result<max_ending_here){
23+
result=max_ending_here;
24+
}
25+
if(max_ending_here<0){
26+
max_ending_here=0;
27+
}
28+
}
29+
System.out.print("The Maximum contiguous sum in the array is : "+result);
30+
31+
}
32+
}

0 commit comments

Comments
 (0)