Check if all bits can be made same by flipping two consecutive bits

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.
Examples:

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.

Approach:
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:

C++

 `// C++ program for the` `// above approach`   `#include ` `using` `namespace` `std;`   `// Function to check if` `// Binary string can be` `// made equal` `string canMake(string& s)` `{`   `    ``int` `o = 0, z = 0;`   `    ``// Counting occurrence of` `    ``// zero and one in binary` `    ``// string` `    ``for` `(``int` `i = 0; i < s.size(); i++) {` `        ``if` `(s[i] - ``'0'` `== 1)` `            ``o++;` `        ``else` `            ``z++;` `    ``}`   `    ``// From above observation` `    ``if` `(o % 2 == 1 && z % 2 == 1)` `        ``return` `"NO"``;` `    ``else` `        ``return` `"YES"``;` `}`   `// Driver code` `int` `main()` `{`   `    ``string s = ``"01011"``;` `    ``cout << canMake(s) << ``'\n'``;` `    ``return` `0;` `}`

Java

 `// Java program for the above approach ` `import` `java.io.*;` `public` `class` `GFG ` `{` `    `  `    ``// Function to check if ` `    ``// Binary string can be ` `    ``// made equal ` `    ``static` `String canMake(String s) ` `    ``{ ` `    `  `        ``int` `o = ``0``, z = ``0``; ` `    `  `        ``// Counting occurrence of ` `        ``// zero and one in binary ` `        ``// string ` `        ``for` `(``int` `i = ``0``; i < s.length(); i++) ` `        ``{ ` `            ``if` `(s.charAt(i) - ``'0'` `== ``1``) ` `                ``o++; ` `            ``else` `                ``z++; ` `        ``} ` `    `  `        ``// From above observation ` `        ``if` `(o % ``2` `== ``1` `&& z % ``2` `== ``1``) ` `            ``return` `"NO"``; ` `        ``else` `            ``return` `"YES"``; ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)` `    ``{ ` `    `  `        ``String s = ``"01011"``; ` `        ``System.out.println(canMake(s)) ; ` `        `  `    ``} ` `}`   `// This code is contributed by AnkitRai01`

Python3

 `# Python3 program for the above approach `   `# Function to check if ` `# Binary string can be ` `# made equal ` `def` `canMake(s) :`   `    ``o ``=` `0``; z ``=` `0``; `   `    ``# Counting occurrence of ` `    ``# zero and one in binary ` `    ``# string ` `    ``for` `i ``in` `range``(``len``(s)) :` `        ``if` `(``ord``(s[i]) ``-` `ord``(``'0'``) ``=``=` `1``) :` `            ``o ``+``=` `1``; ` `        ``else` `:` `            ``z ``+``=` `1``; `   `    ``# From above observation ` `    ``if` `(o ``%` `2` `=``=` `1` `and` `z ``%` `2` `=``=` `1``) :` `        ``return` `"NO"``; ` `    ``else` `:` `        ``return` `"YES"``; `   `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: `   `    ``s ``=` `"01011"``; ` `    ``print``(canMake(s)); `   `# This code is contributed by AnkitRai01`

C#

 `// C# program for the above approach ` `using` `System;`   `class` `GFG ` `{` `    `  `    ``// Function to check if ` `    ``// Binary string can be ` `    ``// made equal ` `    ``static` `string` `canMake(``string` `s) ` `    ``{ ` `    `  `        ``int` `o = 0, z = 0; ` `    `  `        ``// Counting occurrence of ` `        ``// zero and one in binary ` `        ``// string ` `        ``for` `(``int` `i = 0; i < s.Length; i++) ` `        ``{ ` `            ``if` `(s[i] - ``'0'` `== 1) ` `                ``o++; ` `            ``else` `                ``z++; ` `        ``} ` `    `  `        ``// From above observation ` `        ``if` `(o % 2 == 1 && z % 2 == 1) ` `            ``return` `"NO"``; ` `        ``else` `            ``return` `"YES"``; ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `Main()` `    ``{ ` `        ``string` `s = ``"01011"``; ` `        ``Console.WriteLine(canMake(s)) ; ` `    ``} ` `}`   `// This code is contributed by AnkitRai01`

Javascript

 ``

PHP

 ``

Output:

`YES`

Time Complexity: O(n), where n is the length of the given Binary number
Auxiliary space: O(1) since it is using constant space for variables

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next