Skip to content
Related Articles

Related Articles

Find Mth lexicographically smallest Binary String with no two adjacent 1

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 09 May, 2022

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: 10
Explanation: The only strings that can be made of size 2 are [“00”, “01”, “10”] and the 3rd string is “10”.

Input: N = 3, M = 2.
Output: 001

 

Approach: The problem can be solved based on the following approach:

Form all the N sized strings and find the Mth smallest among them. 

Follow the steps mentioned below to implement the idea.

  • For each character, there are two choices:
    • Make the character 0.
    • If the last character of the string formed till now is not 1, then the current character can also be 1.
  • To implement this use recursion.
  • As the target is to find the Mth smallest, for any character call the recursive function for 0 first and for 1 after that (if 1 can be used).
  • Each time a string of length N is formed increase the count of strings
  • If count = M, that string is the required lexicographically Mth smallest string.

Below is the implementation of above approach:

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Declared 2 global variable
// one is the answer string and
// the other is the count of created string
string ans = "";
int Count = 0;
 
// Function to find the mth string.
void findString(int idx, int n,
                int m, string curr)
{
    // When size of string is equal to n
    if (idx == n) {
 
        // If count of strings created
        // is equal to m-1
        if (Count == m - 1) {
            ans = curr;
        }
        else {
            Count += 1;
        }
        return;
    }
 
    // Call the function to recurse for
    // currentstring + "0"
    curr += "0";
    findString(idx + 1, n, m, curr);
    curr.pop_back();
 
    // If the last character of curr is not 1
    // then similarly recurse for "1".
    if (curr[curr.length() - 1] != '1') {
        curr += "1";
        findString(idx + 1, n, m, curr);
        curr.pop_back();
    }
}
 
// Driver Code
int main()
{
    int N = 2, M = 3;
 
    // Function call
    findString(0, N, M, "");
    cout << ans << endl;
    return 0;
}

Java




// Java code to implement the approach
import java.io.*;
 
class GFG {
 
  // Declared 2 global variable
  // one is the answer string and
  // the other is the count of created string
  static String ans = "";
  public static int Count = 0;
 
  // Function to find the mth string.
  public static void findString(int idx, int n,
                                int m, String curr)
  {
    // When size of string is equal to n
    if (idx == n) {
 
      // If count of strings created
      // is equal to m-1
      if (Count == m - 1) {
        ans = curr;
      }
      else {
        Count += 1;
      }
      return;
    }
 
    // Call the function to recurse for
    // currentstring + "0"
    curr += "0";
    findString(idx + 1, n, m, curr);
    curr=curr.substring(0,curr.length()-1);
 
    // If the last character of curr is not 1
    // then similarly recurse for "1".
    if (curr.length()==0|| curr.charAt(curr.length() - 1) != '1') {
      curr += "1";
      findString(idx + 1, n, m, curr);
      curr=curr.substring(0,curr.length()-1);
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int N = 2, M = 3;
 
    // Function call
    findString(0, N, M, "");
    System.out.println(ans);
  }
}
 
// This code is contributed by jana_sayantan.

Python3




# Python code to implement the approach
 
# Declared 2 global variable
# one is the answer string and
# the other is the count of created string
ans = ""
Count = 0
 
# Function to find the mth string.
def findString(idx, n, m, curr):
    global ans,Count
 
    # When size of string is equal to n
    if (idx == n):
 
        # If count of strings created
        # is equal to m-1
        if (Count == m - 1):
            ans = curr
        else:
            Count += 1
        return
 
 
    # Call the function to recurse for
    # currentstring + "0"
    curr += "0"
    findString(idx + 1, n, m, curr)
    curr = curr[0:len(curr) - 1]
 
    # If the last character of curr is not 1
    # then similarly recurse for "1".
    if (len(curr) == 0 or curr[len(curr) - 1] != '1'):
        curr += "1"
        findString(idx + 1, n, m, curr)
        curr = curr[0:len(curr) - 1]
 
# Driver Code
N,M = 2,3
 
# Function call
findString(0, N, M, "")
print(ans)
 
# This code is contributed by shinjanpatra

Javascript




<script>
 
// JavaScript code to implement the approach
 
// Declared 2 global variable
// one is the answer string and
// the other is the count of created string
let ans = ""
let Count = 0
 
// Function to find the mth string.
function findString(idx, n, m, curr){
 
    // When size of string is equal to n
    if (idx == n){
 
        // If count of strings created
        // is equal to m-1
        if (Count == m - 1)
            ans = curr
        else
            Count += 1
        return
    }
 
    // Call the function to recurse for
    // currentstring + "0"
    curr += "0"
    findString(idx + 1, n, m, curr)
    curr = curr.substring(0,curr.length - 1)
 
    // If the last character of curr is not 1
    // then similarly recurse for "1".
    if (curr.length == 0 || curr[curr.length - 1] != '1'){
        curr += "1"
        findString(idx + 1, n, m, curr)
        curr = curr.substring(0,curr.length - 1)
    }
}
 
// Driver Code
let N = 2,M = 3
 
// Function call
findString(0, N, M, "")
document.write(ans)
 
// This code is contributed by shinjanpatra
 
</script>

C#




// C# code for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
  // Declared 2 global variable
  // one is the answer string and
  // the other is the count of created string
  static String ans = "";
  public static int Count = 0;
  
  // Function to find the mth string.
  public static void findString(int idx, int n,
                                int m, string curr)
  {
    // When size of string is equal to n
    if (idx == n) {
  
      // If count of strings created
      // is equal to m-1
      if (Count == m - 1) {
        ans = curr;
      }
      else {
        Count += 1;
      }
      return;
    }
  
    // Call the function to recurse for
    // currentstring + "0"
    curr += "0";
    findString(idx + 1, n, m, curr);
    curr=curr.Substring(0,curr.Length-1);
  
    // If the last character of curr is not 1
    // then similarly recurse for "1".
    if (curr.Length==0|| curr[(curr.Length - 1)] != '1') {
      curr += "1";
      findString(idx + 1, n, m, curr);
      curr=curr.Substring(0,curr.Length-1);
    }
  }
 
// Driver Code
public static void Main()
{
     int N = 2, M = 3;
  
    // Function call
    findString(0, N, M, "");
    Console.WriteLine(ans);
}
}

Output

10

Time Complexity: O(2N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!