A small error is coming for the count. Need help


#1

int Solution::numRange(vector &A, int B, int C) {
int i,j,sum=0,count=0;
for(i=0;i<A.size();i++){
sum=A[i];
for(j=i+1;j<A.size();j++){
sum+=A[j];
if(sum>=B && sum<=C)
count++;
}
}
return count;
}

INPUT: A : [ 76, 22, 81, 77, 95, 23, 27, 35, 24, 38, 15, 90, 19, 46, 53, 6, 77, 96, 100, 85, 43, 16, 73, 18, 7, 66 ]
B : 98
C : 290
MY OUTPUT: 83
Expected OUTPUT: 84

I am not able to figure out the error. Help would be appreciated.


#2

same, if someone figure this out, it will be helpful


#3

You’re missing the contiguous array with single element, because you’re atleast taking 2 elements to check the condition, in this case, 100 is the element you’re missing.
Hope this helps!


#4

as imsach46 said your have to check for sub array containing only one element that is the element it self and one more thing you can use if condition to break out of the second for loop if sum>C.
Happy Coding.