Kindly help me for SPOJ ambiguous permutation

Sir,This is my code for

I tried to debug it but unable to,PLEASE HELP

You have done a really funny error.

    cout<<"in is "<<in<<endl;
   for(int i=0;i<in;i++)//receiving the array
    cin>>in;//THIS IS THE BUG

You have condition that β€œi<in”, but you are CHANGING value of in every time you take input. This also changes condition and that for loop runs for unexpected number of turns.

Eg- For-

1 4 3 2
2 3 4 5 1

First in is 4. It now enters the loop,

For i =0, in is 4. Loop runs, inputs 1 and puts in array.
Now i=1, and in IS ALSO 1 as we overwrote the previous value. now 1<1 is false and it exits the for loop. It prints answer for it.

Now for second time we ask input of in, it takes 4 from β€œ1 4 3 2” and similarly your loop runs.

Change the variable for input!!

2 Likes Have a look, and then debug yourself

Thank you very much sir,for helping me out.I do some stupid mistake everytime.

Sir,I am getting Time limit exceeded,is there any other mistake?

Let me have another look. Please give link to your new, corrected code :slight_smile:

Here is the link sir