Permutation present at the middle of lexicographic ordering of permutations of at most length N made up integers up to K
Given two positive integers K and N, the task is to find the permutation present at the middle of all permutations of at most length N, consisting of integers from the range [1, K], arranged lexicographically.
Examples:
Input: K = 3, N = 2
Output: 2 1
Explanation: Lexicographical order of all possible permutations are:
- {1}.
- {1, 1}
- {1, 2}
- {1, 3}
- {2}
- {2, 1}
- {2, 2}
- {2, 3}
- {3}
- {3, 1}
- {3, 2}
- {3, 3}
Therefore, the middle lexicographically the smallest sequence is (N/2)th(= 6th) sequence, which is {2, 1}.
Input: K = 2, N = 4
Output: 1 2 2 2
Naive Approach: The simplest approach to solve the given problem is to generate all possible subsequences of a length [1, N], consisting of integers from [1, K]. Store these elements in an array. After generating all the subsequences, sort the stored list of subsequences and print the middle element of the list.
Time Complexity: O(NK)
Auxiliary Space: O(NK)
Efficient Approach: The above approach can be optimized by checking the parity of K whether K is odd or even and then find the middle lexicographically the smallest sequence accordingly. Follow the steps below to solve the problem:
- If the value of K is even, then exactly half of the sequences start with an integer K/2 or less. Therefore, the resultant sequence is K/2 followed by (N – 1) occurrence of K.
- If the value K is odd, then consider B to be a sequence that contains N occurrences of (K + 1)/2.
- For a sequence X, let f(X) be a sequence such that Xi in X is replaced with (K + 1 ? Xi).
- The only exception happens for prefixes of B.
- Start with the sequence B, and perform the following steps (N – 1)/2 times:
- If the last element of the current sequence is 1, then remove it.
- Otherwise, decrement the last element by 1, and while the sequence contains less than N elements, and insert K at the end of sequence B.
- After completing the above steps, print the sequence obtained in the array B[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void lexiMiddleSmallest( int K, int N)
{
if (K % 2 == 0) {
cout << K / 2 << " " ;
for ( int i = 0; i < N - 1; ++i) {
cout << K << " " ;
}
cout << "\n" ;
exit (0);
}
vector< int > a(N, (K + 1) / 2);
for ( int i = 0; i < N / 2; ++i) {
if (a.back() == 1) {
a.pop_back();
}
else {
--a.back();
while (( int )a.size() < N) {
a.push_back(K);
}
}
}
for ( auto i : a) {
cout << i << " " ;
}
cout << "\n" ;
}
int main()
{
int K = 2, N = 4;
lexiMiddleSmallest(K, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void lexiMiddleSmallest( int K, int N)
{
if (K % 2 == 0 ) {
System.out.print(K / 2 + " " );
for ( int i = 0 ; i < N - 1 ; ++i) {
System.out.print(K + " " );
}
System.out.println();
return ;
}
ArrayList<Integer> a = new ArrayList<Integer>();
for ( int i = 0 ; i < N / 2 ; ++i) {
if (a.get(a.size() - 1 ) == 1 ) {
a.remove(a.size() - 1 );
}
else {
int t = a.get(a.size() - 1 ) - 1 ;
a.set(a.get(a.size() - 1 ), t);
while (a.size() < N) {
a.add(K);
}
}
}
for ( int i : a) {
System.out.print(i + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int K = 2 , N = 4 ;
lexiMiddleSmallest(K, N);
}
}
|
Python3
def lexiMiddleSmallest(K, N):
if (K % 2 = = 0 ):
print (K / / 2 ,end = " " )
for i in range (N - 1 ):
print (K, end = " " )
print ()
return
a = [(K + 1 ) / / 2 ] * (N)
for i in range (N / / 2 ):
if (a[ - 1 ] = = 1 ):
del a[ - 1 ]
else :
a[ - 1 ] - = 1
while ( len (a) < N):
a.append(K)
for i in a:
print (i, end = " " )
print ()
if __name__ = = '__main__' :
K, N = 2 , 4
lexiMiddleSmallest(K, N)
|
C#
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);
}
}
|
Javascript
<script>
function lexiMiddleSmallest(K, N)
{
if (K % 2 == 0) {
document.write(K / 2 + " " );
for (let i = 0; i < N - 1; ++i) {
document.write(K + " " );
}
document.write( "<br/>" );
return ;
}
let a = [];
for (let i = 0; i < N / 2; ++i) {
if (a[a.length - 1] == 1) {
a.pop(a.length - 1);
}
else {
a[a.length - 1] -= 1;
while (a.length < N) {
a.push(K);
}
}
}
for (let i in a) { document.write(i + " " ); }
document.write( "<br/>" );
}
let K = 2, N = 4;
lexiMiddleSmallest(K, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
05 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...