-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindexOfSubarraySum.java
More file actions
44 lines (36 loc) · 987 Bytes
/
indexOfSubarraySum.java
File metadata and controls
44 lines (36 loc) · 987 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
38
39
40
41
42
43
44
class Solution {
public static ArrayList<Integer> subarraySum(int[] arr, int n, int s) {
// code here
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> returnList = new ArrayList<>();
int sum = 0;
int start = 0;
if(s == 0 ){
for ( int i =0 ; i< n; i ++){
if(sum==arr[i] ){
list.add(i+1);
list.add(i+1);
break;
}
}
}
else{
for ( int i =0 ; i< n; i ++){
sum = sum+arr[i];
while (sum > s && start <=i ){
sum = sum - arr[start];
start++;
}
if(sum==s ){
list.add(start+1);
list.add(i+1);
break;
}
}
}
if(list.isEmpty()){
list.add(-1) ;
}
return list;
}
}