Input format ;
enter N(<1000) enter K(<=10)
the next k lines each contain a permutaion of 1 to n ;
Output format;
K lines each containg the lexicographically next permutation (entered in input)
There seems to be some small error in my code (using Dev C++ )
Please help me to find it out.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int ar[1000][10],N,K,x,i,j,k,max,trig,replace=1001;
cin >> N;
cin >> K;
for(i=0;i<K;i++)
{
for(j=0;j<N;j++)
{
cin >> ar[i][j];
}
}//end of outer most for
for(i=0;i<K;i++)
{
replace=1001;
max=-1; //made the change
for(j=N-1;j>=0;j--)
{
if(ar[i][j]>max)
{
max=ar[i][j];//finding a max to whom a minimum can be found
}
else
{
trig=ar[i][j];
for(k=j+1;k<N;k++)
{
if(ar[i][k]>trig && ar[i][k]<replace) //made the change
{
replace=ar[i][k];
x=k;
}
}
ar[i][j]=replace;
ar[i][x]=trig;
sort (ar[i]+ j+1,ar[i]+N);
break;
}//end of else;
}//end of inner for
}//end of outer for
cout << endl;
for(i=0;i<K;i++)
{
for(j=0;j<N;j++)
{
cout << ar[i][j] << " ";
}
cout << endl;
}
return 0;
} // end of main
the programme seems to be working fine for small values of N , but for large value of N , it displays incorrect output.
Heres an input for which the code doesn’t work.
50 3
50 33 36 47 17 49 31 4 5 48 42 7 46 23 21 44 3 18 6 43 41 39 19 9 26 1 25 34 13 8 14 29 28 22 38 27 40 37 12 11 16 45 10 20 32 24 35 30 2 15
14 7 12 38 35 10 20 3 13 46 50 37 43 45 30 2 42 41 25 39 5 26 48 27 49 21 32 23 28 40 34 15 6 17 36 16 19 33 24 11 29 9 1 44 22 4 8 31 47 18
25 9 14 47 24 10 8 43 40 20 15 22 26 37 36 44 42 30 32 34 38 19 33 5 28 12 31 50 1 7 13 46 41 49 3 39 27 48 18 29 17 16 35 21 11 23 4 6 2 45
And the correct output for the above input is :-
50 33 36 47 17 49 31 4 5 48 42 7 46 23 21 44 3 18 6 43 41 39 19 9 26 1 25 34 13 8 14 29 28 22 38 27 40 37 12 11 16 45 10 20 32 24 35 30 15 2
14 7 12 38 35 10 20 3 13 46 50 37 43 45 30 2 42 41 25 39 5 26 48 27 49 21 32 23 28 40 34 15 6 17 36 16 19 33 24 11 29 9 1 44 22 4 8 47 18 31
25 9 14 47 24 10 8 43 40 20 15 22 26 37 36 44 42 30 32 34 38 19 33 5 28 12 31 50 1 7 13 46 41 49 3 39 27 48 18 29 17 16 35 21 11 23 4 6 45 2
But my code displays incorrect output for the above mentioned test case.
Thanks.
Thanks.