-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSum.java
More file actions
37 lines (35 loc) · 1015 Bytes
/
SubSum.java
File metadata and controls
37 lines (35 loc) · 1015 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
import java.util.Arrays;
import java.util.Scanner;
public class SubSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}
int left = 0;
int target = sc.nextInt();
int subSum = 0;
int []res = new int[2];
boolean isfound = false;
for(int right = 0; right < arr.length; right++){
subSum += arr[right];
while(subSum > target && left <= right){
subSum -= arr[left];
left++;
}
if(subSum == target){
res[0] = left + 1;
res[1] = right + 1;
isfound = true;
break;
}
}
if(isfound){
System.out.println(Arrays.toString(res));
}else{
System.out.println(-1);
}
}
}