-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubarraySum.java
More file actions
56 lines (54 loc) · 1.55 KB
/
SubarraySum.java
File metadata and controls
56 lines (54 loc) · 1.55 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
52
53
54
55
56
package datastructure.array.program;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Given an unsorted array of non-negative integers, find a continuous sub-array
* which adds to a given number.
*
* @author skedia
*
*/
public class SubarraySum {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int tc = Integer.parseInt(br.readLine());
for (int i = 0; i < tc; i++) {
String[] lOne = br.readLine().split(" ");
int n = Integer.parseInt(lOne[0]);
int sum = Integer.parseInt(lOne[1]);
String[] lTwo = br.readLine().split(" ");
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(lTwo[j]);
}
// traverse the array taking two pointers to denote start and
// end of the array add the contiguous numbers by incrementing
// 1st pointer and if the sum of subArray is greater than the
// required sum subtract the element at second pointer and
// increment the second pointer
int a = 0;
int b = 0;
boolean isPresent = false;
int tempSum = 0;
while (b < n || a < n) {
if (sum == tempSum) {
isPresent = true;
break;
}
tempSum += b < n ? arr[b++] : 0;
while (tempSum > sum) {
tempSum -= arr[a++];
}
}
if (isPresent) {
System.out.print((a + 1) + " " + b + "\n");
} else {
System.out.println(-1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}