Given a binary string, the task is to find whether all the digits of the string can be made equal i.e either 0 or 1 by flipping two consecutive bits any number of times.
Input: 01011 Output: YES Explanation: Flip 2nd and 3rd bit -> 00111, again flipping 1'st and 2'nd bit -> 11111 Input: 100011 Output: NO Explanation: No number of moves can ever equalize all elements of the array.
On careful observation, toggling of i’th and j’th bit can be done by toggling from i’th bit like (i, i+1), (i+1, i+2) …. (j-1, j) here every bit is toggling twice (if bit is toggle twice then its come to its initial value) except i and j then ultimately i’th and j’th bits toggle. Therefore, it can be said that it is only not possible to make all digits in binary string equal when the count of both 1 and 0 is odd.
Below is the implementation of the above approach:
Time Complexity: O(n), where n is the length of the given Binary number
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if bits of a number has count of consecutive set bits in increasing order
- Check if all bits can be made same by single flip
- Maximize the number by flipping at most K bits
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Check if all bits of a number are set
- Check whether the number has only first and last bits set
- Check whether the number has only first and last bits set | Set 2
- Check whether all the bits are set in the given range
- Check whether all the bits are unset in the given range
- Check if a number has two adjacent set bits
- Check whether all the bits are unset in the given range or not
- Check if all the set bits of the binary representation of N are at least K places away
- Check if a number has bits in alternate pattern | Set 1
- Check whether bits are in alternate pattern in the given range | Set-2
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.
Improved By : AnkitRai01