text: Permutation present at the middle of lexicographic ordering of permutations of at most length N made up integers up to K | C # program for the above approach ; Function that finds the middle the lexicographical smallest sequence ; If K is even ; First element is K / 2 ; Remaining elements of the sequence are all integer K ; Stores the sequence when K is odd ; Iterate over the range [ 0 , N / 2 ] ; Check if the sequence ends with in 1 or not ; Remove the sequence ending in 1 ; If it doesn 't end in 1 ; Decrement by 1 ; Insert K to the sequence till its size is N ; Print the sequence stored in the vector ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static void lexiMiddleSmallest ( int K , int N ) { if ( K % 2 == 0 ) { Console . Write ( K / 2 + " ▁ " ) ; for ( int i = 0 ; i < N - 1 ; ++ i ) { Console . Write ( K + " ▁ " ) ; } Console . WriteLine ( ) ; return ; } List < int > a = new List < int > ( ) ; for ( int i = 0 ; i < N / 2 ; ++ i ) { if ( a [ a . Count - 1 ] == 1 ) { a . Remove ( a . Count - 1 ) ; } else { a [ a . Count - 1 ] -= 1 ; while ( ( int ) a . Count < N ) { a . Add ( K ) ; } } } foreach ( int i in a ) { Console . Write ( i + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( ) { int K = 2 , N = 4 ; lexiMiddleSmallest ( K , N ) ; } }