Question
Given an array of non negative integers
A, and a range(B, C), find the number of continuous subsequences in the array which have sumSin the range[B, C]orB <= S <= CContinuous subsequence is defined as all the numbers
A[i],A[i + 1], ....A[j]where0 <= i <= j < size(A)Example :
A : [10, 5, 1, 0, 2] (B, C) : (6, 8) ans = 3
[5, 1],[5, 1, 0],[5, 1, 0, 2]are the only 3 continuous subsequence with their sum in the range[6, 8]
My code
public int numRange(ArrayList<Integer> a, int b, int c) { int curr_sum = a.get(0), start = 0; int i = 0, count = 0; for (i = 1; i < a.size(); i++) { // If curr_sum exceeds the sum, then remove the starting elements while (curr_sum > c) { curr_sum = curr_sum - a.get(start); start++; } // If curr_sum becomes equal to sum, then return true if (b <= curr_sum && curr_sum <= c) { count++; } // Add this element to curr_sum if (i < a.size()) curr_sum = curr_sum + a.get(i); } return count; } Problem : Wrong Answer.
What are the cases I am missing ? How do I improve the efficiency of this code after rectifying it ?