Lexicographically n | C ++ program to print nth permutation with using next_permute ( ) ; Function to print nth permutation using next_permute ( ) ; Sort the string in lexicographically ascending order ; Keep iterating until we reach nth position ; check for nth iteration ; print string after nth iteration ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nPermute ( string str , long int n ) { sort ( str . begin ( ) , str . end ( ) ) ; long int i = 1 ; do { if ( i == n ) break ; i ++ ; } while ( next_permutation ( str . begin ( ) , str . end ( ) ) ) ; cout << str ; } int main ( ) { string str = " GEEKSFORGEEKS " ; long int n = 100 ; nPermute ( str , n ) ; return 0 ; }