Lexicographically smallest permutation of first N natural numbers having K perfect indices
Last Updated :
25 Jan, 2022
Given two positive integers N and K, the task is to find lexicographically the smallest permutation of first N natural numbers such that there are exactly K perfect indices.
An index i in an array is said to be perfect if all the elements at indices smaller than i are smaller than the element at index i.
Examples:
Input: N = 10, K = 3
Output: 8 9 10 1 2 3 4 5 6 7
Explanation: There are exactly 3 perfect indices 0, 1 and 2.
Input: N = 12, K = 4
Output: 9 10 11 12 1 2 3 4 5 6 7 8
Naive Approach: The idea is to generate all the possible permutations of first N natural numbers and print that permutation which is lexicographically smallest and has K perfect indices.
Time Complexity: O(N*N!)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe that the smallest permutation will have the last K elements of the range [1, N] in the front in increasing order. The remaining elements can be appended in increasing order. Follow the steps below to solve the problem:
- Initialize an array A[] to store the lexicographically smallest permutation of first N natural numbers.
- Iterate over the range [0, K – 1] using a variable, say i, and update the array element A[i] to store (N – K + 1) + i.
- Iterate over the range [K, N – 1] using the variable i and update the array element A[i] to (i – K + 1).
- After completing the above steps, print the array A[] that contains lexicographically the smallest permutation with K perfect indices.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void findPerfectIndex( int N, int K)
{
int i = 0;
for (; i < K; i++) {
cout << (N - K + 1) + i << " " ;
}
for (; i < N; i++) {
cout << i - K + 1 << " " ;
}
}
int main()
{
int N = 10, K = 3;
findPerfectIndex(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findPerfectIndex( int N, int K)
{
int i = 0 ;
for (; i < K; i++)
{
System.out.print((N - K + 1 ) + i+ " " );
}
for (; i < N; i++)
{
System.out.print(i - K + 1 + " " );
}
}
public static void main(String[] args)
{
int N = 10 , K = 3 ;
findPerfectIndex(N, K);
}
}
|
Python3
def findPerfectIndex(N, K) :
i = 0
for i in range (K):
print ((N - K + 1 ) + i, end = " " )
for i in range ( 3 , N):
print ( i - K + 1 , end = " " )
N = 10
K = 3
findPerfectIndex(N, K)
|
C#
using System;
class GFG
{
static void findPerfectIndex( int N, int K)
{
int i = 0;
for (; i < K; i++)
{
Console.Write((N - K + 1) + i+ " " );
}
for (; i < N; i++)
{
Console.Write(i - K + 1+ " " );
}
}
public static void Main(String[] args)
{
int N = 10, K = 3;
findPerfectIndex(N, K);
}
}
|
Javascript
<script>
function findPerfectIndex(N , K)
{
var i = 0;
for (; i < K; i++)
{
document.write((N - K + 1) + i+ " " );
}
for (; i < N; i++)
{
document.write(i - K + 1+ " " );
}
}
var N = 10, K = 3;
findPerfectIndex(N, K);
</script>
|
Output:
8 9 10 1 2 3 4 5 6 7
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...