Lexicographically smallest permutation number up to K having given array as a subsequence
Given an integer K and an array arr[] having N pairwise distinct integers in the range [1, K], the task is to find the lexicographically smallest permutation of the first K positive integers such that the given array arr[] is a subsequence of the permutation.
Examples:
Input: arr[] = {1, 3, 5, 7}, K = 8
Output: 1 2 3 4 5 6 7 8
Explanation: {1, 2, 3, 4, 5, 6, 7, 8} is the lexicographically smallest permutatuion of the first 8 positive integers such that the given array {1, 3, 5, 7} is also a subsequence of the permutation.Input: arr[] = {6, 4, 2, 1}, K=7
Output: 3 5 6 4 2 1 7
Approach: The given problem can be solved by using a greedy approach. Below are the steps to follow:
- Create a vector missing[] that stores the integers in the range [1, K] in increasing order that are not present in the given array arr[] using the approach discussed in this article.
- Create two pointers p1 and p2 which store the current index in arr[] and missing[] respectively. Initially, both are equal to 0.
- Greedily take and store the minimum of arr[p1] and missing [p2] into a vector, say ans[] and increment the respective pointer to the next position till the count of the stored integers is less than K.
- In order to make things easier, append INT_MAX at the end of the array missing[] and arr[] which will avoid getting out of bounds.
- After completing the above steps, all the values stored in the ans[] is the required result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the lexicographically// smallest permutation such that the// given array is a subsequence of itvoid findPermutation(int K, vector<int> arr){ // Stores the missing elements in // arr in the range [1, K] vector<int> missing; // Stores if the ith element is // present in arr or not vector<bool> visited(K + 1, 0); // Loop to mark all integers present // in the array as visited for (int i = 0; i < arr.size(); i++) { visited[arr[i]] = 1; } // Loop to insert all the integers // not visited into missing for (int i = 1; i <= K; i++) { if (!visited[i]) { missing.push_back(i); } } // Append INT_MAX at end in order // to prevent going out of bounds arr.push_back(INT_MAX); missing.push_back(INT_MAX); // Pointer to the current element int p1 = 0; // Pointer to the missing element int p2 = 0; // Stores the required permutation vector<int> ans; // Loop to construct the permutation // using greedy approach while (ans.size() < K) { // If missing element is smaller // that the current element insert // missing element if (arr[p1] < missing[p2]) { ans.push_back(arr[p1]); p1++; } // Insert current element else { ans.push_back(missing[p2]); p2++; } } // Print the required Permutation for (int i = 0; i < K; i++) { cout << ans[i] << " "; }} // Driver Codeint main(){ int K = 7; vector<int> arr = { 6, 4, 2, 1 }; // Function Call findPermutation(K, arr); return 0;} |
3 5 6 4 2 1 7
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


