Next Permutation
PROBLEM :
Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers.
If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order.
For example:
1,2,3 -> 1,3,2
3,2,1 -> 1,2,3
Input:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an integer N depicting the size of array and next line followed by the value of array.
Output:
Print the array of next permutation in a separate line.
Constraints:
1 = T = 40
1 = N = 100
0 = A[i] = 100
Example:
Input
1
6
1 2 3 6 5 4
Output
1 2 4 3 5 6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
#include<algorithm>
using namespace std;
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void NextPermutation(int str[],int no)
{
int i ;
for(i=no-1;i>0;i--)
if(str[i-1]<str[i])
break ;
if(i==0)
return ;
int small=i-1 ;
int index=i ;
for(i=i+1;i<no;i++)
if(str[i]>str[small]&&str[i]<str[index])
index=i ;
swap(&str[small],&str[index]) ;
sort(str+(small+1),str+no);
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int str[no] ;
for(int i=0;i<no;i++)
cin>>str[i] ;
NextPermutation(str,no) ;
for(int i=0;i<no;i++)
cout<<str[i]<<" " ;
cout<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers.
If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order.
For example:
1,2,3 -> 1,3,2
3,2,1 -> 1,2,3
Input:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an integer N depicting the size of array and next line followed by the value of array.
Output:
Print the array of next permutation in a separate line.
Constraints:
1 = T = 40
1 = N = 100
0 = A[i] = 100
Example:
Input
1
6
1 2 3 6 5 4
Output
1 2 4 3 5 6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
#include<algorithm>
using namespace std;
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void NextPermutation(int str[],int no)
{
int i ;
for(i=no-1;i>0;i--)
if(str[i-1]<str[i])
break ;
if(i==0)
return ;
int small=i-1 ;
int index=i ;
for(i=i+1;i<no;i++)
if(str[i]>str[small]&&str[i]<str[index])
index=i ;
swap(&str[small],&str[index]) ;
sort(str+(small+1),str+no);
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int str[no] ;
for(int i=0;i<no;i++)
cin>>str[i] ;
NextPermutation(str,no) ;
for(int i=0;i<no;i++)
cout<<str[i]<<" " ;
cout<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment