I was trying to solve this using 2 pointers but my solution fails to pass some solutions. The idea is to have a sliding window and track the window using start and end pointers which are the indices of the start of the subarray and the end of the subarray respectively.

public class Solution {

public int subarraySum(int[] nums, int k) {

```
if(nums.length == 1)
return (nums[0] == k) ? 1 : 0;
int start = 0, end = 1;
int sum = nums[0], ways = 0;
while(start <= end){ //end < nums.length && start < nums.length){
if(sum == k){
ways++;
if(start < nums.length){
sum -= nums[start];
}
start++;
if(end < nums.length){
sum += nums[end];
end++;
}
}
else if(sum < k && end < nums.length){
sum += nums[end];
end++;
}
else if(start < nums.length){
sum -= nums[start];
start++;
}
}
return ways;
}
```

}

It does not pass all scenarios currently. But I am curious to know if anyone has solved it using the approach of two pointers and sliding window.

Thanks.