Can't find the error in question but codechef in showing wrong answer

I am submitting this code for beginner level question QUARLPREL (selecting teams after ranking them) this code is working in codeblocks but not in codechef could someone please tell me what’s wrong in the code.

#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
long long n,k;
int i;
cin>>n>>k;
long long a[n],ran=1,counter=1;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,greater());
i=1;
while(i<n && ran<=k)
{
if(a[i]==a[i-1])
{
counter++;
i++;
}
else
{
counter++;
ran++;
i++;
}
}
if(ran==k+1)
counter–;
else if(i==n-1 && ran<=k)
counter=n;
cout<<counter<<endl;
}
}