Can be solved in O(n) in much simpler way


#1

public class Solution {
int count(ArrayList A, int n, int k){
int temp = 0;
int left = 0;
int right = 0;
int res = 0;
while(left < A.size() && right < A.size()) {
if(temp + A.get(right) <= k){
temp += A.get(right);
right++;
if(right > left){
res += right - left;
}
}
else {
temp -= A.get(left);
left++;
}
}
return res;
}
public int numRange(ArrayList A, int B, int C) {
return count(A, A.size(), C) - count(A, A.size(), B-1);
}
}