-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarray.java
More file actions
49 lines (32 loc) · 1.33 KB
/
subarray.java
File metadata and controls
49 lines (32 loc) · 1.33 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
45
46
47
48
49
class Subarray {
public static void sub(int numbers[]) {
int currSum = 0;
int maxSum = Integer.MAX_VALUE;
int prefix[] = new prefix[numbers.length]; //use to defined new array
prefix[0] = numbers[0]; //we have set the value of the index of prefix equal to numbers
for(int i = 1; i<prefix.length; i++){
prefix[i] = prefix[i-1] + numbers[i]; //we use the formula in loop to calculate prefix of the number
}
for (int i = 0; i < numbers.length; i++) {
int start = i;
for (int j = i; j < numbers.length; j++) {
int end = j;
currSum = start == 0 ? prefix[end]: prefix[end]-prefix[start-1]; //ternary operator,
if(currSum > maxSum){
maxSum = currSum;
}
}
// for(int l = 1; l<numbers.length; l++){
// max = numbers[l];
// System.out.println("maxi" +max);
}
}
System.out.println();
}
System.out.println(currSum);
}
public static void main(String[] args) {
int numbers[] = {2, 4, 5, 6, 7, 8, 9};
sub(numbers);
}
}