Simple brute force method using c++

interview-questions
Tags: #<Tag:0x00007f181f2875b0>

#1

int n=A.size();
for (int i = 0; i <n; i++ )
{
int count = 0;
for (int j = 0; j <n; j++)
if (A[i] <A[j])
count++;
if (count == A[i])
return 1;
}

return -1;

#2

Why we can’t just sort the array and pick an element from last for which n-1-i==A[i] is satisfied?