-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubarrays.java
More file actions
31 lines (30 loc) · 842 Bytes
/
Subarrays.java
File metadata and controls
31 lines (30 loc) · 842 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
import java.util.ArrayList;
import java.util.Scanner;
public class Subarrays {
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 currSum = 0;
int left = 0;
int right = 0;
int k = sc.nextInt();
ArrayList<Integer> res= new ArrayList<>();
while(right < arr.length){
currSum += arr[right];
while(currSum > 0){
currSum -= arr[left];
left++;
}
if(currSum == k){
for(int i = left; i <= right; i++){
res.add(arr[i]);
}
}
right++;
}
}
}