Lexicographically smallest permutation of size A having B integers exceeding all preceding integers
Last Updated :
23 Mar, 2023
Given two positive integers, A and B, the task is to generate the lexicographically smallest permutation of all integers up to A in which exactly B integers are greater than all its preceding elements.
Examples:
Input: A = 3, B = 2
Output : [1, 3, 2]
Explanation:
All possible permutations of integers [1, 3] are [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]. Out of these permutations, three permutations {(1, 3, 2), (2, 1, 3), (2, 3, 1)} satisfy the necessary condition. The lexicographically smallest permutation of the three is (1, 3, 2).
Input: A = 4, B = 4
Output: [1, 2, 3, 4]
Approach: Follow the steps below to solve the problem:
- Generate all possible permutations of integers from [1, A] using inbuilt function permutations() from the itertools library. Basically, it returns a tuple consisting of all possible permutations.
- Now, check if the array’s maximum element and the number of elements should satisfy the problem’s condition count is equal or not.
- If equal then there is only one possible list of elements that satisfy the condition. Logically they are simply the range of A number of integers starting from 1 in sorted order.
- If not, take each tuple from the permutations list and then calculate the number of integers that are present in the tuple which is greater than all the previous integers in that tuple.
- If the count is equal to B then load that tuple to another new list.
- Test the lists of elements that are loaded by a new list whether they ordered in lexicographically or not if not ordered then arrange them manually and return the minimum of it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > getSmallestArray( int A, int B)
{
if (A == B) {
vector< int > arr;
for ( int i = 1; i <= A; i++) {
arr.push_back(i);
}
return arr;
}
vector<vector< int >> pos_list;
vector< int > arr;
for ( int i = 1; i <= A; i++) {
arr.push_back(i);
}
do {
int c = 0;
int i = 0;
int j = 1;
while (j <= A - 1) {
if (arr[j] > arr[i]) {
i++;
} else if (i == j) {
c++;
j++;
i = 0;
} else {
j++;
i = 0;
}
}
if (c + 1 == B) {
pos_list.push_back(arr);
}
} while (next_permutation(arr.begin(), arr.end()));
if (pos_list.size() == 1) {
return pos_list[0];
}
int small = INT_MAX;
vector< int > ans;
for ( auto i : pos_list) {
int num = 0;
int mul = 1;
for ( int j = i.size() - 1; j >= 0; j--) {
num += mul * i[j];
mul *= 10;
}
if (num < small) {
small = num;
ans = i;
}
}
return ans;
}
int main()
{
int A = 3, B = 2;
vector< int > arr = getSmallestArray(A, B);
for ( int i : arr) {
cout << i << " " ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static List<Integer> getSmallestArray( int A, int B) {
if (A == B) {
List<Integer> arr = new ArrayList<>();
for ( int i = 1 ; i <= A; i++) {
arr.add(i);
}
return arr;
}
List<List<Integer>> pos_list = new ArrayList<>();
List<Integer> arr = new ArrayList<>();
for ( int i = 1 ; i <= A; i++) {
arr.add(i);
}
do {
int c = 0 ;
int i = 0 ;
int j = 1 ;
while (j <= A - 1 ) {
if (arr.get(j) > arr.get(i)) {
i++;
} else if (i == j) {
c++;
j++;
i = 0 ;
} else {
j++;
i = 0 ;
}
}
if (c + 1 == B) {
pos_list.add( new ArrayList<>(arr));
}
} while (nextPermutation(arr));
if (pos_list.size() == 1 ) {
return pos_list.get( 0 );
}
int small = Integer.MAX_VALUE;
List<Integer> ans = new ArrayList<>();
for (List<Integer> i : pos_list) {
int num = 0 ;
int mul = 1 ;
for ( int j = i.size() - 1 ; j >= 0 ; j--) {
num += mul * i.get(j);
mul *= 10 ;
}
if (num < small) {
small = num;
ans = new ArrayList<>(i);
}
}
return ans;
}
private static boolean nextPermutation(List<Integer> arr) {
int i = arr.size() - 2 ;
while (i >= 0 && arr.get(i) >= arr.get(i + 1 )) {
i--;
}
if (i < 0 ) {
return false ;
}
int j = arr.size() - 1 ;
while (j > i && arr.get(j) <= arr.get(i)) {
j--;
}
swap(arr, i, j);
reverse(arr, i + 1 , arr.size() - 1 );
return true ;
}
private static void swap(List<Integer> arr, int i, int j) {
int temp = arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, temp);
}
private static void reverse(List<Integer> arr, int i, int j) {
while (i < j) {
swap(arr, i++, j--);
}
}
public static void main(String[] args) {
int A = 3 , B = 2 ;
List<Integer> arr = getSmallestArray(A, B);
for ( int num : arr) {
System.out.print(num + " " );
}
}
}
|
Python3
from itertools import permutations
def getSmallestArray(A, B):
if A = = B:
return list ( range ( 1 , A + 1 ))
pos_list = []
Main_List = list (permutations( range ( 1 , A + 1 ), A))
for L in Main_List:
c = 0
i = 0
j = 1
while j < = ( len (L) - 1 ):
if L[j] > L[i]:
i + = 1
elif i = = j:
c + = 1
j + = 1
i = 0
else :
j + = 1
i = 0
if (c + 1 ) = = B:
pos_list.append( list (L))
if len (pos_list) = = 1 :
return pos_list[ 0 ]
satisfied_list = []
for i in pos_list:
array_test = ''.join( map ( str , i))
satisfied_list.append( int (array_test))
small = min (satisfied_list)
str_arr = list ( str (small))
ans = list ( map ( int , str_arr))
return ans
A = 3
B = 2
print (getSmallestArray(A, B))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static List< int > getSmallestArray( int A, int B)
{
if (A == B)
{
return Enumerable.Range(1, A).ToList();
}
List<List< int >> pos_list = new List<List< int >>();
List<List< int >> Main_List = getPermutations(Enumerable.Range(1, A).ToList());
foreach (List< int > L in Main_List)
{
int c = 0;
int i = 0;
int j = 1;
while (j <= (L.Count - 1))
{
if (L[j] > L[i])
{
i += 1;
}
else if (i == j)
{
c += 1;
j += 1;
i = 0;
}
else
{
j += 1;
i = 0;
}
}
if ((c + 1) == B)
{
pos_list.Add(L.ToList());
}
}
if (pos_list.Count == 1)
{
return pos_list[0];
}
List< int > satisfied_list = new List< int >();
foreach (List< int > i in pos_list)
{
string array_test = string .Join( "" , i);
satisfied_list.Add( int .Parse(array_test));
}
int small = satisfied_list.Min();
string str_arr = small.ToString();
List< int > ans = str_arr.Select(c => int .Parse(c.ToString())).ToList();
return ans;
}
public static List<List< int >> getPermutations(List< int > arr)
{
List<List< int >> result = new List<List< int >>();
if (arr.Count == 1)
{
result.Add(arr.ToList());
return result;
}
foreach ( int x in arr)
{
List< int > remaining = new List< int >(arr);
remaining.Remove(x);
foreach (List< int > L in getPermutations(remaining))
{
L.Insert(0, x);
result.Add(L.ToList());
}
}
return result;
}
public static void Main()
{
int A = 3;
int B = 2;
List< int > smallestArray = getSmallestArray(A, B);
Console.WriteLine( string .Join( ", " , smallestArray));
}
}
|
Javascript
const getSmallestArray = (A, B) => {
if (A === B) {
let arr = [];
for (let i = 1; i <= A; i++) {
arr.push(i);
}
return arr;
}
let pos_list = [];
let arr = [];
for (let i = 1; i <= A; i++) {
arr.push(i);
}
do {
let c = 0;
let i = 0;
let j = 1;
while (j <= A - 1) {
if (arr[j] > arr[i]) {
i++;
} else if (i === j) {
c++;
j++;
i = 0;
} else {
j++;
i = 0;
}
}
if (c + 1 === B) {
pos_list.push([...arr]);
}
} while (nextPermutation(arr));
if (pos_list.length === 1) {
return pos_list[0];
}
let small = Number.MAX_VALUE;
let ans = [];
for (let i of pos_list) {
let num = 0;
let mul = 1;
for (let j = i.length - 1; j >= 0; j--) {
num += mul * i[j];
mul *= 10;
}
if (num < small) {
small = num;
ans = [...i];
}
}
return ans;
};
const nextPermutation = (arr) => {
let i = arr.length - 2;
while (i >= 0 && arr[i] >= arr[i + 1]) {
i--;
}
if (i < 0) {
return false ;
}
let j = arr.length - 1;
while (j > i && arr[j] <= arr[i]) {
j--;
}
swap(arr, i, j);
reverse(arr, i + 1, arr.length - 1);
return true ;
};
const swap = (arr, i, j) => {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
const reverse = (arr, i, j) => {
while (i < j) {
swap(arr, i++, j--);
}
};
const main = () => {
let A = 3,
B = 2;
let arr = getSmallestArray(A, B); let ans= "" ;
for (let num of arr) {
ans = ans + num + ' ' ;
} console.log(ans);
};
main();
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: Follow the steps below to optimize the above approach:
- Initialize an array arr[] consisting of first A natural numbers sequentially.
- Create a resultant array ans[] and append the first (B – 1) elements from arr[].
- So the resultant array has (B – 1) elements that satisfy the given condition.
- Now insert the maximum element from arr[] into the resultant array. Remove the inserted elements arr[]
- Since we already have B elements, the resultant array has B elements that satisfy the given condition.
- Now, copy all remaining elements from arr[] one by one to the resultant array and print the resultant array.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
vector< int > getSmallestArray( int A, int B)
{
vector< int >arr(A);
for ( int i = 0; i < A; i++)
arr[i] = i + 1;
vector< int >ans(A);
for ( int i = 0; i < B - 1; i++)
ans[i] = arr[i];
ans[B - 1] = arr[A - 1];
for ( int i = B; i < A; i++)
ans[i] = arr[i - 1];
return ans;
}
int main()
{
int A = 3;
int B = 2;
vector< int >ans = getSmallestArray(A, B);
for ( auto i : ans)
cout << i << " " ;
}
|
Java
import java.util.*;
class GFG{
@SuppressWarnings ( "unchecked" )
static void getSmallestArray( int A, int B)
{
Vector arr = new Vector();
for ( int i = 0 ; i < A; i++)
arr.add(i + 1 );
Vector ans = new Vector();
for ( int i = 0 ; i < B - 1 ; i++)
ans.add(arr.get(i));
ans.add(arr.get(A - 1 ));
for ( int i = B; i < A; i++)
ans.add(arr.get(i - 1 ));
for ( int i = 0 ; i < ans.size(); i++)
System.out.print(ans.get(i) + " " );
}
public static void main(String args[])
{
int A = 3 ;
int B = 2 ;
getSmallestArray(A, B);
}
}
|
Python3
def getSmallestArray(A, B):
arr = ( list ( range ( 1 , (A + 1 ))))
ans = []
ans[ 0 :B - 1 ] = arr[ 0 :B - 1 ]
del arr[ 0 :B - 1 ]
ans.append(arr[ - 1 ])
del arr[ - 1 ]
ans[B:] = arr[:]
return ans
A = 3
B = 2
print (getSmallestArray(A, B))
|
C#
using System.Collections.Generic;
using System;
class GFG{
static void getSmallestArray( int A, int B)
{
List< int > arr = new List< int >();
for ( int i = 0; i < A; i++)
arr.Add(i + 1);
List< int > ans = new List< int >();
for ( int i = 0; i < B - 1; i++)
ans.Add(arr[i]);
ans.Add(arr[A - 1]);
for ( int i = B; i < A; i++)
ans.Add(arr[i - 1]);
for ( int i = 0; i < A; i++)
Console.Write(ans[i] + " " );
}
public static void Main()
{
int A = 3;
int B = 2;
getSmallestArray(A,B);
}
}
|
Javascript
<script>
function getSmallestArray( A, B)
{
let arr = [];
for (let i = 0; i < A; i++)
arr[i] = i + 1;
let ans = [];
for (let i = 0;i<A;i++)
ans.push(0);
for (let i = 0; i < B - 1; i++)
ans[i] = arr[i];
ans[B - 1] = arr[A - 1];
for (let i = B; i < A; i++)
ans[i] = arr[i - 1];
return ans;
}
let A = 3;
let B = 2;
let ans = getSmallestArray(A, B);
for (let i =0;i<ans.length;i++)
document.write( ans[i] , " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...