-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadanes.java
More file actions
44 lines (33 loc) · 1.1 KB
/
Kadanes.java
File metadata and controls
44 lines (33 loc) · 1.1 KB
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
import java.util.Scanner;
public class Kadanes {
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements you require in the array");
int n= sc.nextInt();
int[] myArray= new int[n];
for(int i=0;i<n;i++){
myArray[i]= sc.nextInt();
System.out.println("Enter Next Value");
}
for(int i=0;i<n;i++) {
System.out.print(myArray[i] + " ");
}
System.out.println("\nThe maximum Subarray is :- "+ Subarray_max(myArray));
}
static int Subarray_max(int myArray[]){
int size= myArray.length;
int max_so_far= Integer.MIN_VALUE;
int max_ending_here= 0;
for(int i=0;i<size;i++){
max_ending_here= max_ending_here + myArray[i];
if(max_ending_here > max_so_far){
max_so_far=max_ending_here;
}
if(max_ending_here<0){
max_ending_here=0;
}
}
return max_so_far;
}
}
//[ -1, 2, -2, 5, 7,-3, 1]