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