Lexicographically smallest permutation number up to K having given array as a subsequence
Last Updated :
09 Mar, 2022
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 permutation 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++
#include <bits/stdc++.h>
using namespace std;
void findPermutation( int K, vector< int > arr)
{
vector< int > missing;
vector< bool > visited(K + 1, 0);
for ( int i = 0; i < arr.size(); i++) {
visited[arr[i]] = 1;
}
for ( int i = 1; i <= K; i++) {
if (!visited[i]) {
missing.push_back(i);
}
}
arr.push_back(INT_MAX);
missing.push_back(INT_MAX);
int p1 = 0;
int p2 = 0;
vector< int > ans;
while (ans.size() < K) {
if (arr[p1] < missing[p2]) {
ans.push_back(arr[p1]);
p1++;
}
else {
ans.push_back(missing[p2]);
p2++;
}
}
for ( int i = 0; i < K; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
int K = 7;
vector< int > arr = { 6, 4, 2, 1 };
findPermutation(K, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findPermutation( int K, Vector<Integer> arr)
{
Vector<Integer> missing = new Vector<Integer>();
boolean visited[] = new boolean [K + 1 ];
for ( int i = 0 ; i < arr.size(); i++) {
visited[arr.get(i)] = true ;
}
for ( int i = 1 ; i <= K; i++) {
if (!visited[i]) {
missing.add(i);
}
}
arr.add(Integer.MAX_VALUE);
missing.add(Integer.MAX_VALUE);
int p1 = 0 ;
int p2 = 0 ;
Vector<Integer> ans = new Vector<Integer>();
while (ans.size() < K) {
if (arr.get(p1) < missing.get(p2)) {
ans.add(arr.get(p1));
p1++;
}
else {
ans.add(missing.get(p2));
p2++;
}
}
for ( int i = 0 ; i < K; i++) {
System.out.print(ans.get(i)+ " " );
}
}
public static void main(String[] args)
{
int K = 7 ;
Integer []a = { 6 , 4 , 2 , 1 };
Vector<Integer> arr = new Vector<>(Arrays.asList(a));
findPermutation(K, arr);
}
}
|
Python3
def findPermutation(K, arr):
missing = []
visited = [ 0 ] * (K + 1 )
for i in range ( 4 ):
visited[arr[i]] = 1
for i in range ( 1 , K + 1 ):
if ( not visited[i]):
missing.append(i)
INT_MAX = 2147483647
arr.append(INT_MAX)
missing.append(INT_MAX)
p1 = 0
p2 = 0
ans = []
while ( len (ans) < K):
if (arr[p1] < missing[p2]):
ans.append(arr[p1])
p1 = p1 + 1
else :
ans.append(missing[p2])
p2 = p2 + 1
for i in range ( 0 , K):
print (ans[i], end = " " )
if __name__ = = "__main__" :
K = 7
arr = [ 6 , 4 , 2 , 1 ]
findPermutation(K, arr)
|
C#
using System;
using System.Collections;
class GFG{
static void findPermutation( int K, ArrayList arr)
{
ArrayList missing = new ArrayList();
bool [] visited = new bool [K + 1];
for ( int i = 0; i < arr.Count; i++) {
visited[( int )arr[i]] = true ;
}
for ( int i = 1; i <= K; i++) {
if (!visited[i]) {
missing.Add(i);
}
}
arr.Add(Int32.MaxValue);
missing.Add(Int32.MaxValue);
int p1 = 0;
int p2 = 0;
ArrayList ans = new ArrayList();
while (ans.Count < K) {
if (( int )arr[p1] < ( int )missing[p2]) {
ans.Add(arr[p1]);
p1++;
}
else {
ans.Add(missing[p2]);
p2++;
}
}
for ( int i = 0; i < K; i++) {
Console.Write(ans[i]+ " " );
}
}
public static void Main()
{
int K = 7;
int [] a = {6, 4, 2, 1};
ArrayList arr = new ArrayList(a);
findPermutation(K, arr);
}
}
|
Javascript
<script>
const findPermutation = (K, arr) => {
let missing = [];
let visited = new Array(K + 1).fill(0);
for (let i = 0; i < arr.length; i++) {
visited[arr[i]] = 1;
}
for (let i = 1; i <= K; i++) {
if (!visited[i]) {
missing.push(i);
}
}
const INT_MAX = 2147483647;
arr.push(INT_MAX);
missing.push(INT_MAX);
let p1 = 0;
let p2 = 0;
let ans = [];
while (ans.length < K) {
if (arr[p1] < missing[p2]) {
ans.push(arr[p1]);
p1++;
}
else {
ans.push(missing[p2]);
p2++;
}
}
for (let i = 0; i < K; i++) {
document.write(`${ans[i]} `);
}
}
let K = 7;
let arr = [6, 4, 2, 1];
findPermutation(K, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...