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++
#include <bits/stdc++.h>
using namespace std;
bool isSubsequence(string str1, string str2, int m, int n)
{
int j = 0;
for ( int i = 0; i < n && j < m; i++)
if (str1[j] == str2[i])
j++;
return (j == m);
}
void findMinimumNumber(string s)
{
int r = stoi(s, 0, 2);
int ans = r + 1;
for ( int i = 0; i <= r; i++) {
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());
if (!isSubsequence(p, s, m, n)) {
ans = i;
break ;
}
}
cout << ans;
}
int main()
{
string s = "10101" ;
findMinimumNumber(s);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isSubsequence(String str1, String str2,
int m, int n)
{
int j = 0 ;
for ( int i = 0 ; i < n && j < m; i++)
if (str1.charAt(j) == str2.charAt(i))
j++;
return (j == m);
}
static void findMinimumNumber(String s)
{
int r = Integer.parseInt(s, 2 );
int ans = r + 1 ;
for ( int i = 0 ; i <= r; i++) {
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();
if (!isSubsequence(p, s, m, n)) {
ans = i;
break ;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
String s = "10101" ;
findMinimumNumber(s);
}
}
|
Python3
def isSubsequence(str1, str2, m, n):
j = 0
i = 0
while (i < n and j < m):
if (str1[j] = = str2[i]):
j + = 1
i + = 1
return (j = = m)
def findMinimumNumber(s):
r = int (s, 2 )
ans = r + 1
for i in range (r + 1 ):
p = ""
j = i
while (j > 0 ):
p + = str (j % 2 )
j = j / / 2
m = len (p)
n = len (s)
p = p[:: - 1 ]
if (isSubsequence(p, s, m, n) = = False ):
ans = i
break
print (ans)
if __name__ = = '__main__' :
s = "10101"
findMinimumNumber(s)
|
C#
using System;
class GFG {
static bool isSubsequence( string str1, string str2,
int m, int n)
{
int j = 0;
for ( int i = 0; i < n && j < m; i++)
if (str1[j] == str2[i])
j++;
return (j == m);
}
static void findMinimumNumber( string s)
{
int r = Int32.Parse(s);
int ans = r + 1;
for ( int i = 0; i <= r; i++) {
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);
if (!isSubsequence(p, s, m, n)) {
ans = i;
break ;
}
}
Console.WriteLine(ans);
}
public static void Main()
{
string s = "10101" ;
findMinimumNumber(s);
}
}
|
Javascript
function isSubsequence(str1, str2, m, n)
{
let j = 0;
let i = 0;
while (i < n && j < m)
{
if (str1[j] == str2[i]) {
j++;
}
i++;
}
return j == m;
}
function findMinimumNumber(s)
{
let r = parseInt(s, 2);
let ans = r + 1;
for (let i = 0; i <= r; i++)
{
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( "" );
if (isSubsequence(p, s, m, n) == false ) {
ans = i;
break ;
}
}
console.log(ans);
}
( function main()
{
let s = "10101" ;
findMinimumNumber(s);
})();
|
Time Complexity: O(N*R), where R is the decimal representation of the given binary string, S
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...