Why does this not pass all the test cases?


#1

Below is my code, which stands correct for ‘Test’. But when submitted, stands incorrect for 1 test case.
Please help.

int Solution::solve(vector<int> &A) {
int n=A.size();
 sort(A.begin(), A.end());
for(int i=0;i<A.size();i++)
{
    if(A[i]==(n-i-1))
    return 1;
}
return -1;

}


#2

consider the case [0, 1, 2, 2, 2]


#3

Ohhh. Right, thank you so much!
That surely was a dumb mistake on my end


#4

Haha, I too had the same doubt, thanks for this.


#5

you are not taking in consideration about duplicate
take
try for(int j=I;j<size;i++) if(A[j]>i) then use j instead of I in if condition