Kindly help me for SPOJ ambiguous permutation

Sir,This is my code for http://www.spoj.com/problems/PERMUT2/

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

I tried to debug it but unable to,PLEASE HELP

You have done a really funny error.

cin>>in;
    cout<<"in is "<<in<<endl;
    flag=1;
    if(in==0)
        break;
   for(int i=0;i<in;i++)//receiving the array
   {
    cin>>in;//THIS IS THE BUG
    a[i]=in;
   }

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-

4
1 4 3 2
5
2 3 4 5 1
1
1
0

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

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

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

http://ideone.com/zm5oLU Have a look, and then debug yourself