Different result during test and submit

programming
Tags: #<Tag:0x00007f241b1e6518>

#1

int Solution::findCount(const vector &A, int B) {
long low=0;
long high = A.size()-1;
//cout << A.size();
//cout << high;
long mid;
long li=1, hi=0;
while(low<=high){
mid= (low+high)/2;
if(A[mid]<B){
low=mid+1;
//cout << low << high;
}
else if(A[mid]>B || (A[mid]==B && A[mid-1]==B)){
high = mid-1;
//cout << low << high;
}
else if(A[mid]==B){
li = mid;
//cout << low << high;
break;
}
}
low=0;
high = A.size()-1;
while(low<=high){
mid= (low+high)/2;
if(A[mid]<B || (A[mid]==B && A[mid+1]==B)){
low=mid+1;
}
else if(A[mid]>B ){
high = mid-1;
}
else if(A[mid]==B){
hi = mid;
//cout << mid;
break;
}
}
// cout << hi << “” << li;
return (int)hi-li+1;

}

When I am testing my code for input
763 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
10

it give 81

But while submitting it gives -681.

How is this happening?


#2

You should really check your conditions designed to acquire the higher and upper limit.
You are not storing the mid+1 value in hi in the second binary search algo in the first if case.
Imagine you have an input like really long with many repeated occurrences of your number then it is sure to give a wrong answer.


#3

SAME PROBLEM WITH ME

i am getting 81 as answer for that test case which show error when testing
but when i submit it show 84 as answer.whats wrong>?

MY CODE

int findCount(const int* arr, int n1, int B) {
int count=0;
int l=0;
int r=n1-1;
int x=B;
while (l <= r)
{
int m = l + (r-l)/2;
if (arr[m] == x)
{
count++;
int a=m;
while(arr[–a]==x)
count++;
a=m;
while(arr[++a]==x)
count++;
return count;
}
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}

return 0;

}