-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathKadane.java
55 lines (43 loc) · 1.42 KB
/
Kadane.java
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
45
46
47
48
49
50
51
52
53
54
55
import java.io.*;
public class Kadane {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//size of array
int n = Integer.parseInt(br.readLine().trim());
int arr[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
//adding elements
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine[i]);
}
Kadane obj = new Kadane();
//calling maxSubarraySum() function
System.out.println(obj.maxSubarraySum(arr, n));
}
// Function to find subarray with maximum sum
int maxSubarraySum(int arr[], int n) {
if(n==0)
return 0;
int max_ending_here = 0, max_so_far = arr[0];
for(int i=0;i<n;i++) {
max_ending_here += arr[i];
if(max_ending_here < 0)
max_ending_here = 0;
else if(max_so_far <= max_ending_here) {
max_so_far = max_ending_here;
}
}
return max_so_far;
}
}
/**
Input:
5
1 2 3 -2 5
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which
is a contiguous subarray.
*/