Minimum number whose binary form is not a subsequence of given binary string
Last Updated :
31 Jan, 2023
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 <bits/stdc++.h>
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;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to check if string str1 is a
// subsequence of string str2
static boolean 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.charAt(j) == str2.charAt(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
static void findMinimumNumber(String s)
{
// Store the decimal number of string, S
int r = Integer.parseInt(s, 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 += (j % 2) + "";
j = j / 2;
}
int m = p.length();
int n = s.length();
StringBuilder sb = new StringBuilder(p);
sb.reverse();
p = sb.toString();
// 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
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Function Call
String s = "10101";
// Function Call
findMinimumNumber(s);
}
}
// This code is contributed by Karandeep1234
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.
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if string str1 is a
// subsequence of string str2
static 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
static void findMinimumNumber(string s)
{
// Store the decimal number of string, S
int r = Int32.Parse(s);
// 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 += (j % 2).ToString();
j = j / 2;
}
int m = p.Length;
int n = s.Length;
char[] stringArray = p.ToCharArray();
Array.Reverse(stringArray);
p = new string(stringArray);
// 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
Console.WriteLine(ans);
}
// Driver Code
public static void Main()
{
// Function Call
string s = "10101";
// Function Call
findMinimumNumber(s);
}
}
// This code is contributed by ukasp.
JavaScript
// JavaScript implementation of the above approach
// Function to check if string str1 is a
// subsequence of string str2
function isSubsequence(str1, str2, m, n)
{
// Store index of str1
let j = 0;
// Traverse str2 and str1, and compare
// current character of str2 with first
// unmatched char of str1
let i = 0;
while (i < n && j < m)
{
// If matched, move ahead in str1
if (str1[j] == str2[i]) {
j++;
}
i++;
}
// 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
function findMinimumNumber(s)
{
// Store the decimal number of string, S
let r = parseInt(s, 2);
// Initialize the required result
let ans = r + 1;
// Iterate in the range [0, R]
for (let i = 0; i <= r; i++)
{
// Convert integer i to binary string
let p = "";
let j = i;
while (j > 0) {
p += j % 2;
j = Math.floor(j / 2);
}
let m = p.length;
let n = s.length;
p = p.split("").reverse().join("");
// 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
console.log(ans);
}
// Driver Code
(function main()
{
// Function Call
let s = "10101";
// Function Call
findMinimumNumber(s);
})();
// This code is contributed by phasing17
Time Complexity: O(N*R), where R is the decimal representation of the given binary string, S
Auxiliary Space: O(N)
Similar Reads
Number of subsequences in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of subsequences of str which are divisible by 2. Leading zeros in a sub-sequence are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only subsequences which are divisible by 2.Input: str = "10010" Output: 22 Naiv
4 min read
Periodic Binary String With Minimum Period and a Given Binary String as Subsequence. Periodic Binary String: A Binary string is called periodic if it can be written as a repetition of a binary string of smaller or same length. For example, 101010 is a periodic binary string with period 10 as we can get the string by repeatedly appending 10 to itself. In general, the string S with pe
5 min read
Find the minimum number of distinct Substrings formed by 0s in Binary String Given two integers N and M, the task is to output the minimum number of different substrings that can be formed using the Binary String of length N having M number of set bits. Note: Two substrings let say S[x, y] and S[a, b] are different. If either x != a or y != b. Examples: Input: N = 3, M = 1Ou
6 min read
Generate Binary String with equal number of 01 and 10 Subsequence Given an integer N (N > 2), the task is to generate a binary string of size N that consists of equal numbers of "10" & "01" subsequences and also the string should contain at least one '0' and one '1' Note: If multiple such strings exist, print any. Examples: Input: 4Output: 0110Explanation :
7 min read
Minimize deletions in a Binary String to remove all subsequences of the form "0101" Given a binary string S of length N, the task is to find the minimum number of characters required to be deleted from the string such that no subsequence of the form â0101â exists in the string. Examples: Input: S = "0101101"Output: 2Explanation: Removing S[1] and S[5] modifies the string to 00111.
6 min read
Maximum count of â010..â subsequences that can be removed from given Binary String Given a binary string S consisting of size N, the task is to find the maximum number of binary subsequences of the form "010.." of length at least 2 that can be removed from the given string S. Examples: Input: S = "110011010"Output: 3Explanation:Following are the subsequence removed:Operation 1: Ch
6 min read
Minimize count of alternating subsequences to divide given Binary String with subsequence number Given a binary string S of length N. The task is to find the following: The minimum number of subsequences, string S can be divided into, such that the subsequence does not contain adjacent zeroes or ones.Subsequence number to which each character of string S belongs. If there are many answers, outp
11 min read
Make a given Binary String non-decreasing by removing the smallest subsequence Given a binary string str of size N, the task is to find the length of the smallest subsequence such that after erasing the subsequence the resulting string will be the longest continuous non-decreasing string. Example : Input: str = "10011"Output: 1Explanation: Removal of the first occurrence of '1
8 min read
Minimum number of palindromic subsequences to be removed to empty a binary string Given a binary string, count minimum number of subsequences to be removed to make it an empty string. Examples : Input: str[] = "10001" Output: 1 Since the whole string is palindrome, we need only one removal. Input: str[] = "10001001" Output: 2 We can remove the middle 1 as first removal, after fir
6 min read