Given a binary string consisting only of 1’s and 0’s. Find the bit (output is either 1 or 0)whose the minimum number of contiguous sequence flips can make all bits of the string same.
Here, contiguous sequence flip means flipping a substring or 0s or 1s. For Example, in the string “00011110001110”, in one flip we can change the string to “11111110001110”. The first three continuous zeros are changed to 1.
- In one flip we can change any continuous sequence of this string.
- If both 0s and 1s are possible, print the one which comes last.
- The task is to simply print the bit 1 or 0 for which minimum sequence flips will make all bits same.
Input : str = “00011110001110”
Output : 1
Explanation: There are two contiguous sequences of 1’s and three contiguous sequences of 0’s. So flipping 1’s would lead to minimum flips.
Input: str = “010101100011”
Explanation: Since the count of groups of 0s and 1s are same and 1 comes in last.
Naive Approach: Start traversing through the string and take variables named gropupOfOnes and another groupOfZeros to count the groups of 0s and 1s. Now, compare the count of groups of one’s and zero’s. The one with the lesser count will be the answer.
If both are equal, the answer will the last character of the string.
Time complexity: O(N)
- Observe the string carefully, the character at last index of the string will have more groups in the string (If the first and the last character of the string are equal). So, the answer will be another character.
- If the first and last characters are not equal, both the characters have the same number of groups. So, the answer will be the character at last index.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum flips required to keep all 1s together in a Binary string
- Count minimum right flips to set all values in an array
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Form minimum number from given sequence
- Minimum array elements to be changed to make it a Lucas Sequence
- Given two strings check which string makes a palindrome first
- Find all even length binary sequences with same sum of first and second half bits
- Find the Kth position element of the given sequence
- Find the lexicographical next balanced bracket sequence
- Find nth term of the Dragon Curve Sequence
- Find the final co-ordinates reached by following a sequence of directions
- Find a sequence of N prime numbers whose sum is a composite number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.