-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAl.java
More file actions
36 lines (32 loc) · 1.01 KB
/
Al.java
File metadata and controls
36 lines (32 loc) · 1.01 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
import java.util.*;
class Al {
static ArrayList<Integer> subarraySum(int[] arr, int target) {
int start = 0, sum = 0;
ArrayList<Integer> result = new ArrayList<>();
for (int end = 0; end < arr.length; end++) {
sum += arr[end];
while (sum > target && start <= end) {
sum -= arr[start];
start++;
}
if (sum == target) {
result.add(start + 1);
result.add(end + 1);
return result;
}
}
result.add(-1);
return result;
}
public static void main(String args[]) {
int[] arr = {1, 2, 3, 7, 5};
int target = 12;
System.out.println(subarraySum(arr, target));
int[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target2 = 15;
System.out.println(subarraySum(arr2, target2));
int[] arr3 = {5, 3, 4};
int target3 = 2;
System.out.println(subarraySum(arr3, target3));
}
}