-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha84_sumOfSubArrays1.java
48 lines (36 loc) · 1.61 KB
/
a84_sumOfSubArrays1.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
45
46
47
48
public class a84_sumOfSubArrays1 {
public static void sumOfSubArrays(int arr[]) {
int ta = 0; // For calculating total number of subarrays
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
int sum = 0;
for (int i = 0; i < arr.length; i++) { // Outer loop - At first stay on index 0 which one is start then update
for (int j = i; j < arr.length; j++) { // inner loop - At first stay on index 0 which one is end then
// update, (first present in index 0 because single element is also
// sub arrays.)
for (int k = i; k <= j; k++) {
System.out.print(arr[k] + " ");
sum = sum + arr[k];
if (smallest > sum) {
smallest = sum;
}
if (largest < sum) {
largest = sum;
}
}
System.out.println();
System.out.println("Sum = " + sum);
sum = 0;
ta++;
}
System.out.println();
}
System.out.println("total number of sub array is: " + ta);
System.out.println("The largest sum of the sub array is: " + largest);
System.out.println("The smallest sum of the sub array is: " + smallest);
}
public static void main(String[] args) {
int arr[] = { 1, -2, 6, -1, 3 };
sumOfSubArrays(arr);
}
}