Lexicographically smallest permutation of first N natural numbers having K perfect indices
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++
// C++ program for the above approach #include <iostream> using namespace std; // Function to print the lexicographically // smallest permutation with K perfect indices void findPerfectIndex( int N, int K) { // Iterator to traverse the array int i = 0; // Traverse first K array indices for (; i < K; i++) { cout << (N - K + 1) + i << " " ; } // Traverse remaining indices for (; i < N; i++) { cout << i - K + 1 << " " ; } } // Driver Code int main() { int N = 10, K = 3; findPerfectIndex(N, K); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to print the lexicographically // smallest permutation with K perfect indices static void findPerfectIndex( int N, int K) { // Iterator to traverse the array int i = 0 ; // Traverse first K array indices for (; i < K; i++) { System.out.print((N - K + 1 ) + i+ " " ); } // Traverse remaining indices for (; i < N; i++) { System.out.print(i - K + 1 + " " ); } } // Driver Code public static void main(String[] args) { int N = 10 , K = 3 ; findPerfectIndex(N, K); } } // This code is contributed by shikhasingrajput |
Python3
# Python program for the above approach # Function to print the lexicographically # smallest permutation with K perfect indices def findPerfectIndex(N, K) : # Iterator to traverse the array i = 0 # Traverse first K array indices for i in range (K): print ((N - K + 1 ) + i, end = " " ) # Traverse remaining indices for i in range ( 3 , N): print ( i - K + 1 , end = " " ) # Driver Code N = 10 K = 3 findPerfectIndex(N, K) # This code is contributed by code_hunt. |
C#
// C# program for the above approach using System; class GFG { // Function to print the lexicographically // smallest permutation with K perfect indices static void findPerfectIndex( int N, int K) { // Iterator to traverse the array int i = 0; // Traverse first K array indices for (; i < K; i++) { Console.Write((N - K + 1) + i+ " " ); } // Traverse remaining indices for (; i < N; i++) { Console.Write(i - K + 1+ " " ); } } // Driver Code public static void Main(String[] args) { int N = 10, K = 3; findPerfectIndex(N, K); } } // This code is contributed by susmitakundugoaldanga. |
Javascript
<script> // javascript program for the above approach // Function to print the lexicographically // smallest permutation with K perfect indices function findPerfectIndex(N , K) { // Iterator to traverse the array var i = 0; // Traverse first K array indices for (; i < K; i++) { document.write((N - K + 1) + i+ " " ); } // Traverse remaining indices for (; i < N; i++) { document.write(i - K + 1+ " " ); } } // Driver Code var N = 10, K = 3; findPerfectIndex(N, K); // This code is contributed by 29AjayKumar </script> |
8 9 10 1 2 3 4 5 6 7
Time Complexity: O(N)
Auxiliary Space: O(1)
Contact Us