-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMaxSubarraySum.java
44 lines (36 loc) · 974 Bytes
/
MaxSubarraySum.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
/*
* Prifix Array
* {1,-2,6,-1,3}
*
* prefix Array={1,-1,5,4,7}
*
*/
public class MaxSubarraySum {
public static void maxSubarraySum(int array[]) {
int max=Integer.MIN_VALUE;
int prefix[]=new int[array.length];
prefix[0]=array[0];
// Calculate prifixArray
for(int i=1; i<array.length; i++){
prefix[i]=array[i]+ prefix[i-1];
}
// calculate Max Subarray Sum
for(int i=0; i<array.length; i++){
for(int j=i; j<array.length; j++){
int currSum= i==0? prefix[j] : prefix[j]-prefix[i-1];
if (max<currSum) {
max=currSum;
}
}
}
System.out.println("Max Subarray Sum: " + max);
}
public static void main(String[] args) {
int array[] = { 1, -2, 6, -1, 3 };
maxSubarraySum(array);
}
}
/*
* Output:
* Max Subarray Sum: 8
*/