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++
#include <bits/stdc++.h>
using namespace std;
void constructPermutation(string S, int N)
{
int ans[N];
ans[0] = 1;
for ( int i = 1; i < N; ++i) {
if (S[i - 1] == '0' ) {
ans[i] = i + 1;
}
else {
ans[i] = ans[i - 1];
}
for ( int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
for ( int i = 0; i < N; i++) {
cout << ans[i];
if (i != N - 1) {
cout << " " ;
}
}
}
int main()
{
string S = "100101" ;
constructPermutation(S, S.length() + 1);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void constructPermutation(String S, int N)
{
int [] ans = new int [N];
ans[ 0 ] = 1 ;
for ( int i = 1 ; i < N; ++i) {
if (S.charAt(i - 1 ) == '0' ) {
ans[i] = i + 1 ;
}
else {
ans[i] = ans[i - 1 ];
}
for ( int j = 0 ; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(ans[i]);
if (i != N - 1 ) {
System.out.print( " " );
}
}
}
public static void main(String[] args) {
String S = "100101" ;
constructPermutation(S, S.length() + 1 );
}
}
|
Python3
def constructPermutation(S, N):
ans = [ 0 ] * N
ans[ 0 ] = 1
for i in range ( 1 , N):
if (S[i - 1 ] = = '0' ):
ans[i] = i + 1
else :
ans[i] = ans[i - 1 ]
for j in range (i):
if (ans[j] > = ans[i]):
ans[j] + = 1
for i in range (N):
print (ans[i], end = "")
if (i ! = N - 1 ):
print ( " " , end = "")
S = "100101"
constructPermutation(S, len (S) + 1 )
|
C#
using System;
class GFG {
static void constructPermutation( string S, int N)
{
int [] ans = new int [N];
ans[0] = 1;
for ( int i = 1; i < N; ++i) {
if (S[i - 1] == '0' ) {
ans[i] = i + 1;
}
else {
ans[i] = ans[i - 1];
}
for ( int j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
for ( int i = 0; i < N; i++) {
Console.Write(ans[i]);
if (i != N - 1) {
Console.Write( " " );
}
}
}
public static void Main()
{
string S = "100101" ;
constructPermutation(S, S.Length + 1);
}
}
|
Javascript
<script>
function constructPermutation(S, N) {
let ans = new Array(N);
ans[0] = 1;
for (let i = 1; i < N; ++i) {
if (S[i - 1] == '0' ) {
ans[i] = i + 1;
}
else {
ans[i] = ans[i - 1];
}
for (let j = 0; j < i; ++j) {
if (ans[j] >= ans[i]) {
ans[j]++;
}
}
}
for (let i = 0; i < N; i++) {
document.write(ans[i]);
if (i != N - 1) {
document.write( " " );
}
}
}
let S = "100101" ;
constructPermutation(S, S.length + 1);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...