Uncle Johny wrong answer

I submitted the following code for the problem Uncle Johny.I know the other approach but I am unable to spot an error in this one…i am getting wrong answer everytime.It runs well on all the cases I typed in.Please help.

#include

using namespace std;

int main()
{
int t,n,j,k,up,down,i,tmp;
cin>>t;
while(t–)
{
cin>>n;
int a[n+1];
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cin>>k;
tmp=a[k];
a[k]=a[1];
a[1]=tmp;
up=n;
down=2;
while(up>down && up>0 && down<=n)
{
while(a[up]>a[1])
up–;
while(a[down]<a[1])
down++;
if(up>down)
{
tmp=a[up];
a[up]=a[down];
a[down]=tmp;

    }
}
cout<<up<<endl;

}
return 0;
}

As @damn_me said, please post your code in proper blocks, this is a little difficult to read.

My solution, I stored the kth element of the array in some temporary variable and then sorted the array in ascending order. The rest is just going through the array and printing the index of the array, when the element matches our temporary variable.

OR

You could simply iterate through through the array and count the number of elements smaller than the kth element, and then add 1 to know its position.

Here is a solution using the first method: RqjBJQ - Online C++ Compiler & Debugging Tool - Ideone.com

Please, put your code in proper blocks or ideone link. Can you also read it? It’s very tough to test this manner. :slight_smile: (No offense please)