Uncle johny

//
https://www.codechef.com/submit/complete/18917369
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int a[100];
int b[100];
for(int i=1;i<=n;i++){

		cin>>a[i];
	}
	int d;//johnny position intial
	cin>>d;
	for(int i=1;i<=n;i++){
		b[i]=a[i];
	}
		sort(a, a+n+1);
	int m=b[d];
int count=0;
for(int j=0;j<n;j++){
	if(a[j]!=m){
		count++;
	}
	else{
		break;
	}
}

cout<<count+1<<endl;
}
	return 0;
	
}

You have considered the starting index of the arrays a and b to be 1 in your code. Thus the sort function should be:

sort(a+1, a+n+1);

where,

a+1

is an iterator pointing to the element

a[1]

. Also, the for loop where you are checking whether the value of

a[j]

is equal to

m

or not should be:

for(int j=1;j<=n;j++)

since you are the considering the starting index of array to be 1 in your code.

Here’s my optimized AC Solution!

https://www.codechef.com/viewsolution/18919451