# WA in "Uncle Johny"

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;
}
``````

}

Try this (unofficial) test case - work out what answer you expect first according to the problem statement and then see if your code produces the same answer.

``````1
4
111 333 444 222
2
``````

I think it’ll be self-explanatory where it’s going wrong.

also there’s a qsort library function available if you need it - you don’t need to write your own.

also your code has formatted very ugly in the forum - try highlighting the code and hitting ctrl+K or posting a link in future

//