Getting wa on submission but test case runs manually C++


#1

Comment body goes here.

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


#2

Take the below test case to understand the wrong approach.
8,3,3,3,2,1

Your code will return 1 because when it will be checking index 3 it will get true and return 1.
But it should return -1 actually.Check the question .It asked for the number of elements greater than the current element and not the number of elements greater or equal to the current element.You are making a mistake by takiing the equal elements also in your count for checking the truth of the problem.

Hope you understood the explanation.