-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindSubArrayWithSumZero
More file actions
37 lines (34 loc) · 876 Bytes
/
FindSubArrayWithSumZero
File metadata and controls
37 lines (34 loc) · 876 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
27
28
29
30
31
32
33
34
35
36
37
package Udemyprograms;
public class ArraysGeeksForGeeks22 {
public static boolean FindSubArrayWithSumZero(int[] arr) {
/**
* initialise two pointers front,rear,sum
* make front = 0;
* make rear = front;
* sum = 0;
* iterate a loop
* inside the loop add the elements from the rear till arr.lenght
* if in any iteration the sum become zero
* print true;
*/
int front = 0;
int rear = front;
int sum = 0;
while (front < arr.length) {
sum += arr[rear++];
if (sum == 0) {
System.out.println(front + "" + (rear-1));
return true;
}
if (rear == arr.length) {
rear = ++front;
sum = 0;
}
}
return false;
}
public static void main(String args[]) {
int[] arr = {-3, 2, 3, 1, 6};
System.out.println(FindSubArrayWithSumZero(arr));
}
}