-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRR000L Zero Sum Subarray.java
More file actions
51 lines (44 loc) · 1.48 KB
/
RR000L Zero Sum Subarray.java
File metadata and controls
51 lines (44 loc) · 1.48 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
50
51
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> arr = new ArrayList<Integer>();
try{
while (sc.hasNext()) {
if (sc.hasNextInt())
arr.add(sc.nextInt());
else
sc.next();
}
int n = arr.size();
if (n < 1 || n > 10) {
System.out.println("Array size must be between 1 and 10");
return;
}
for (int i = 0; i < n; i++) {
int element = arr.get(i);
if (element < -10 || element > 10) {
System.out.println("Array elements must be from -10 to 10");
return;
}
}
for (int i = 0; i < n; i++) {
int s = 0;
for (int j = i; j < n; j++) {
s += arr.get(j);
if (s == 0) {
System.out.println("True");
System.out.println(arr.size());
return;
}
}
}
}
catch (Exception e) {
System.out.println("Array elements must be integers");
}
System.out.println("False");
System.out.println(arr.size());
}
}