the link of the problem is http://www.codechef.com/problems/JOHNY/
#include
using namespace std;
void qsort(int arr[], int fst, int last);
int bins(int a[], int k,int size);
int main()
{
int t, n, i, j, temp, s[101], k, x, pos;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
if (n == 1)
{
scanf("%d", &s[0]);
scanf("%d", &k);
printf("1\n");
}
else
{
for (i = 0; i < n; i++)
scanf("%d", &s[i]);
scanf("%d", &k);
x = s[k - 1];
qsort(s, 0, n - 1);
pos = bins(s, x, n);
printf("%d\n", pos);
}
}
return 0;
}
void qsort(int arr[], int fst, int last)
{
int i, j, pivot, tmp;
if (fst<last)
{
pivot = fst;
i = fst;
j = last;
while (i<j)
{
while (arr[i] <= arr[pivot] && i<last)
i++;
while (arr[j]>arr[pivot])
j–;
if (i<j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;
qsort(arr, fst, j - 1);
qsort(arr, j + 1, last);
}
}
int bins(int a[], int k,int size)
{
int f = 0, l = size - 1, mid;
mid = (l + f) / 2;
while (f <= l)
{
if (k == a[mid])
return k;
else
if (k > a[mid])
f = mid + 1;
else
l = mid - 1;
mid = (l + f) / 2;
}
}