Minimum number whose binary form is not a subsequence of given binary string
Input: S = “0000”
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”
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:
- Store the decimal number of the binary string, S in a variable R.
- Initialize a variable ans as R+1 to store the required result.
- Iterate in the range [0, R] using the variable i
- Convert the given integer i into its binary string, and store it in string P.
- Check if string P is a subsequence of string S or not. If it is not, then update ans to i and break out of the loop.
- Print the value of ans as the result.
Below is the implementation of the above approach:
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.