File tree 1 file changed +32
-0
lines changed
src/main/java/com/thealgorithms/dynamicprogramming 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments