-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint4.java
More file actions
43 lines (40 loc) · 1.28 KB
/
int4.java
File metadata and controls
43 lines (40 loc) · 1.28 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
/*
given array of integers, find the subarray with greatest sum
return subarray {-2,1,-3,4,-1,2,1,-5,4}; => {4,-1,2,1}
*/
import java.util.*;
public class int4 {
public static int[] findMaxSub(int[] arr) {
if (arr == null || arr.length < 1) return null;
int mLoc = arr[0], mGl=arr[0];
int start_i = 0, end_i = 0;
for (int i = 1; i < arr.length; i++) {
int n = arr[i];
int old_Gl = mGl;
if (n > n + mLoc) start_i = i;
mLoc = Math.max(n, n + mLoc);
mGl = Math.max (mLoc, mGl);
if (mLoc > old_Gl) end_i = i;
}
// make the subarry
int len = end_i - start_i + 1;
int[] result = new int[len];
for (int i = 0; i < len; i++) {
result[i] = arr[start_i];
start_i++;
}
return result;
}
public static void main(String[] args) {
int[] arr = {-2,1,-3,4,-1,2,1,-5,4};
int[] output = findMaxSub(arr);
System.out.println(Arrays.toString(output));
// for (int i = 0; i < output.length; i++) {
// System.out.print(output[i]);
// if (i < output.length - 1) {
// System.out.print(",");
// }
// }
// System.out.println();
}
}