Lexicographically smallest permutation of [1, N] based on given Binary string
Last Updated :
13 Sep, 2021
Given a binary string S of size (N - 1), the task is to find the lexicographically smallest permutation P of the first N natural numbers such that for every index i, if S[i] equals '0' then P[i + 1] must be greater than P[i] and if S[i] equals '1' then P[i + 1] must be less than P[i].
Examples:
Input: N = 7, S = 100101
Output: 2 1 3 5 4 7 6
Explanation:
Consider the permutation as {2, 1, 3, 5, 4, 7, 6} that satisfy the given criteria as:
For index 0, S[0] = 1, P[1] < P[0], i.e. 1 < 2
For index 1, S[1] = 0, P[2] < P[1], i.e. 3 > 1
For index 2, S[2] = 0, P[3] < P[2], i.e. 5 > 3
For index 3, S[3] = 1, P[4] < P[3], i.e. 4 < 5
For index 4, S[4] = 0, P[5] < P[4], i.e. 7 > 4
For index 5, S[5] = 1, P[6] < P[5], i.e. 6 < 7
Input: N = 4, S = 000
Output: 1 2 3 4
Approach: The given problem can be solved by using the Greedy Approach by using smaller numbers at lower indices as much as possible will create the lexicographically smallest permutations. Follow the steps below to solve the problem:
- Initialize an array, say ans[] of size N that stores the resultant permutation.
- Traverse the given string S and perform the following steps:
- If the value of S[i] equals '0' then assign the number greater than the last assigned number.
- Otherwise, assign the smallest number which is larger than all currently used numbers.
- After completing the above steps, print the resultant permutation formed in the array ans[].
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
void constructPermutation(string S, int N)
{
// Stores the resultant permutation
int ans[N];
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (int i = 1; i < N; ++i) {
if (S[i - 1] == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (int i = 0; i < N; i++) {
cout << ans[i];
if (i != N - 1) {
cout << " ";
}
}
}
// Driver Code
int main()
{
string S = "100101";
constructPermutation(S, S.length() + 1);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
static void constructPermutation(String S, int N)
{
// Stores the resultant permutation
int[] ans = new int[N];
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (int i = 1; i < N; ++i) {
if (S.charAt(i - 1) == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (int i = 0; i < N; i++) {
System.out.print(ans[i]);
if (i != N - 1) {
System.out.print(" ");
}
}
}
// Driver Code
public static void main(String[] args) {
String S = "100101";
constructPermutation(S, S.length() + 1);
}
}
// This code is contributed by code_hunt.
Python3
# Python Program to implement
# the above approach
# Function to generate the lexicographically
# smallest permutation according to the
# given criteria
def constructPermutation(S, N):
# Stores the resultant permutation
ans = [0] * N
# Initialize the first elements to 1
ans[0] = 1
# Traverse the given string S
for i in range(1, N):
if (S[i - 1] == '0'):
# Number greater than last
# number
ans[i] = i + 1
else :
# Number equal to the last
# number
ans[i] = ans[i - 1]
# Correct all numbers to the left
# of the current index
for j in range(i):
if (ans[j] >= ans[i]):
ans[j] += 1
# Printing the permutation
for i in range(N):
print(ans[i], end="")
if (i != N - 1):
print(" ", end="")
# Driver Code
S = "100101"
constructPermutation(S, len(S) + 1)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG {
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
static void constructPermutation(string S, int N)
{
// Stores the resultant permutation
int[] ans = new int[N];
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (int i = 1; i < N; ++i) {
if (S[i - 1] == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (int i = 0; i < N; i++) {
Console.Write(ans[i]);
if (i != N - 1) {
Console.Write(" ");
}
}
}
// Driver Code
public static void Main()
{
string S = "100101";
constructPermutation(S, S.Length + 1);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to generate the lexicographically
// smallest permutation according to the
// given criteria
function constructPermutation(S, N) {
// Stores the resultant permutation
let ans = new Array(N);
// Initialize the first elements to 1
ans[0] = 1;
// Traverse the given string S
for (let i = 1; i < N; ++i) {
if (S[i - 1] == '0') {
// Number greater than last
// number
ans[i] = i + 1;
}
else {
// Number equal to the last
// number
ans[i] = ans[i - 1];
}
// Correct all numbers to the left
// of the current index
for (let j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
// Printing the permutation
for (let i = 0; i < N; i++) {
document.write(ans[i]);
if (i != N - 1) {
document.write(" ");
}
}
}
// Driver Code
let S = "100101";
constructPermutation(S, S.length + 1);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N)
Similar Reads
Find n-th lexicographically permutation of a string | Set 2 Given a string of length m containing lowercase alphabets only. We need to find the n-th permutation of string lexicographic ally. Examples: Input: str[] = "abc", n = 3 Output: Result = "bac" All possible permutation in sorted order: abc, acb, bac, bca, cab, cba Input: str[] = "aba", n = 2 Output: R
11 min read
Lexicographically Next Permutation of given String All the permutations of a word when arranged in a dictionary, the order of words so obtained is called lexicographical order. in Simple words, it is the one that has all its elements sorted in ascending order, and the largest has all its elements sorted in descending order. lexicographically is noth
8 min read
Lexicographically smallest permutation with no digits at Original Index Given an integer N. The task is to find the lexicographically smallest permutation of integer of the form: 12345...N such that no digit occurs at the index as in the original number, i.e. if P1P2P3...PN is our permutation then Pi must not be equal to i. Note : N is greater than 1 and less than 10. E
7 min read
Lexicographically n-th permutation of a string Given a string of length m containing lowercase alphabets only. You have to find the n-th permutation of string lexicographically. Examples: Input : str[] = "abc", n = 3 Output : Result = "bac" Explanation : All possible permutation in sorted order: abc, acb, bac, bca, cab, cba Input : str[] = "aba"
6 min read
Find Mth lexicographically smallest Binary String with no two adjacent 1 Given two integers N and M, the task is to find the Mth lexicographically smallest binary string (have only characters 1 and 0) of length N where there cannot be two consecutive 1s. Examples: Input: N = 2, M = 3.Output: 10Explanation: The only strings that can be made of size 2 are ["00", "01", "10"
6 min read
Find lexicographically smallest string in at most one swaps Given a string str of length N. The task is to find out the lexicographically smallest string when at most only one swap is allowed. That is, two indices 1 <= i, j <= n can be chosen and swapped. This operation can be performed at most one time. Examples: Input: str = "string" Output: gtrins E
14 min read
Generate lexicographically smallest Permutation of 1 to N where elements follow given relation Given an integer N and an array arr[] of M pairs of type (Ai, Bi), the task is to generate the lexicographically smallest possible permutation of 1 to N such that every Ai, occurs before every Bi. Examples: Input: N = 4, arr[] = { {2, 1}, {3, 4}, {2, 4} }Output: 2 1 3 4Explanation: The following fiv
10 min read
Lexicographically smallest permutation of the array possible by at most one swap Given an array arr[] representing a permutation of first N natural numbers, the task is to find the lexicographically smallest permutation of the given array arr[] possible by swapping at most one pair of array elements. If it is not possible to make the array lexicographically smaller, then print "
8 min read
Lexicographically smallest permutation where no element is in original position Given a permutation of first N positive integers, the task is to form the lexicographically smallest permutation such that the new permutation does not have any element which is at the same index as of the old one. If it is impossible to make such permutation then print -1. Examples: Input: N = 5, a
13 min read
Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1 Given two integers N and K, the task is to generate a permutation of N numbers (Every number from 1 to N occurs exactly once) such that the number of indices where a[i]>a[i+1] is exactly K. Print "Not possible" if no such permutation is possible. Examples: Input: N = 5, K = 3 Output: 5 4 3 1 2 St
4 min read