-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARRAY9.java
43 lines (34 loc) · 1.06 KB
/
ARRAY9.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
public class ARRAY9 {
public static void printArr(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static int MaxSubarrSum(int arr[]) {
int prefix[] = new int[arr.length];
prefix[0] = arr[0];
for (int i = 1; i < prefix.length; i++) {
prefix[i] = arr[i] + prefix[i - 1];
}
int max_sum = Integer.MIN_VALUE;
int sum;
for (int i = 0; i < prefix.length; i++) {
for (int j = 0; j < prefix.length; j++) {
if (i == 0) {
sum = prefix[j];
} else {
sum = prefix[j] - prefix[i - 1];
}
if (sum > max_sum) {
max_sum = sum;
}
}
}
return max_sum;
}
public static void main(String[] args) {
// MAXSUM SUBARRAY USING PREFIX METHOD
int arr[] = {1, 2, 3};
System.out.println("MAXSUM OF A SUBARRAY IS: " + MaxSubarrSum(arr));
}
}