Related Articles

# Minimum number whose binary form is not a subsequence of given binary string

• Last Updated : 16 Jun, 2021

Given a binary string S of size N, the task is to find the minimum non-negative integer which is not a subsequence of the given string S in its binary form.

Examples:

Input: S = “0000”
Output:1
Explanation: 1 whose binary representation is “1” is the smallest non-negative integer which is not a subsequence of the given string in its binary form.

Input: S = “10101”
Output: 8

Approach: The idea is to convert the given string into its decimal representation, say R. Then iterate in the range [0, R] to check for each integer whether it exists or not as a subsequence in its binary form in the given string, S. If not, then break the loop and print the required result.
Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to check if string str1 is a``// subsequence of string str2``bool` `isSubsequence(string str1, string str2, ``int` `m, ``int` `n)``{``    ``// Store index of str1``    ``int` `j = 0;` `    ``// Traverse str2 and str1, and compare``    ``// current character of str2 with first``    ``// unmatched char of str1``    ``for` `(``int` `i = 0; i < n && j < m; i++)` `        ``// If matched, move ahead in str1``        ``if` `(str1[j] == str2[i])``            ``j++;` `    ``// If all characters of str1 were``    ``// found in str2``    ``return` `(j == m);``}` `// Function to find the minimum number which``// is not a subsequence of the given binary``// string in its binary form``void` `findMinimumNumber(string s)``{``    ``// Store the decimal number of string, S``    ``int` `r = stoi(s, 0, 2);``    ``// Initialize the required result``    ``int` `ans = r + 1;` `    ``// Iterate in the range [0, R]``    ``for` `(``int` `i = 0; i <= r; i++) {` `        ``// Convert integer i to binary string``        ``string p = ``""``;``        ``int` `j = i;``        ``while` `(j > 0) {``            ``p += to_string(j % 2);``            ``j = j / 2;``        ``}` `        ``int` `m = p.length();``        ``int` `n = s.length();``        ``reverse(p.begin(), p.end());` `        ``// Check if the string is not a subsequence``        ``if` `(!isSubsequence(p, s, m, n)) {` `            ``// Update ans and break``            ``ans = i;``            ``break``;``        ``}``    ``}` `    ``// Print the required result``    ``cout << ans;``}` `// Driver Code``int` `main()``{` `    ``// Function Call``    ``string s = ``"10101"``;` `    ``// Function Call``    ``findMinimumNumber(s);` `    ``return` `0;``}`

## Python3

 `# python 3 program for the above approach` `# Function to check if string str1 is a``# subsequence of string str2``def` `isSubsequence(str1, str2, m, n):``    ``# Store index of str1``    ``j ``=` `0` `    ``# Traverse str2 and str1, and compare``    ``# current character of str2 with first``    ``# unmatched char of str1``    ``i ``=` `0``    ``while``(i < n ``and` `j < m):``        ``# If matched, move ahead in str1``        ``if` `(str1[j] ``=``=` `str2[i]):``            ``j ``+``=` `1``        ``i ``+``=` `1` `    ``# If all characters of str1 were``    ``# found in str2``    ``return` `(j ``=``=` `m)` `# Function to find the minimum number which``# is not a subsequence of the given binary``# string in its binary form``def` `findMinimumNumber(s):``    ``# Store the decimal number of string, S``    ``r ``=` `int``(s,``2``)` `    ``# Initialize the required result``    ``ans ``=` `r ``+` `1` `    ``# Iterate in the range [0, R]``    ``for` `i ``in` `range``(r``+``1``):``        ``# Convert integer i to binary string``        ``p ``=` `""``        ``j ``=` `i``        ``while` `(j > ``0``):``            ``p ``+``=` `str``(j ``%` `2``)``            ``j ``=` `j ``/``/` `2` `        ``m ``=` `len``(p)``        ``n ``=` `len``(s)``        ``p ``=` `p[::``-``1``]` `        ``# Check if the string is not a subsequence``        ``if` `(isSubsequence(p, s, m, n) ``=``=` `False``):``            ``# Update ans and break``            ``ans ``=` `i``            ``break` `    ``# Print the required result``    ``print``(ans)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `    ``# Function Call``    ``s ``=` `"10101"``    ` `    ``# Function Call``    ``findMinimumNumber(s)``    ` `    ``# This code is contributed by SURENDRA_GANGWAR.`

Output
`8`

Time Complexity: O(N*R), where R is the decimal representation of the given binary string, S
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up