- Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMax_SubArray.java
More file actions
Latest commit
26 lines (20 loc) · 711 Bytes
/
Copy pathMax_SubArray.java
File metadata and controls
26 lines (20 loc) · 711 Bytes
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
importjava.util.*;
publicclassMax_SubArray {
publicstaticvoidmain(String[]args){
Scannersc = newScanner(System.in);
System.out.print("How many numbers in the array?: ");
intn = sc.nextInt();
intarray[] = newint[n];
for (inti = 0; i < n; i++){
array[i] = sc.nextInt();
}
intmax_sum = array[0];
intcurrent_max_sum = array[0];
for (inti = 1; i < array.length; i++){
current_max_sum = Math.max(current_max_sum + array[i], array[i]);
max_sum = Math.max(max_sum, current_max_sum);
}
System.out.println("Largest sum is " + max_sum);
sc.close();
}
}