Open In App

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

Last Updated : 29 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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




<script>
 
// javascript program for the above approach
 
// Function to check if
// Binary string can be
// made equal
function canMake(s)
{
 
    var o = 0, z = 0;
 
    // Counting occurrence of
    // zero and one in binary
    // string
    for (i = 0; i < s.length; i++)
    {
        if (s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0) == 1)
            o++;
        else
            z++;
    }
 
    // From above observation
    if (o % 2 == 1 && z % 2 == 1)
        return "NO";
    else
        return "YES";
}
 
// Driver code
var s = "01011";
document.write(canMake(s)) ;
 
// This code is contributed by Rajput-Ji
 
</script>


PHP




<?php
// Function to check if
// Binary string can be
// made equal
function canMake($s) {
    $o = 0;
    $z = 0;
 
    // Counting occurrence of
    // zero and one in binary
    // string
    for ($i = 0; $i < strlen($s); $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
$s = "01011";
echo canMake($s) . "\n";
?>


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



Similar Reads

Check if Binary Array can be made Palindrome by flipping two bits at a time
Given a binary array A[] of size N, the task is to check whether the array can be converted into a palindrome by flipping two bits in each operation. Note: The operation can be performed any number of times. Examples: Input: A[] = {1, 0, 1, 0, 1, 1}Output: Yes?Explanation: We can perform the following operation:Select i = 2 and j = 4. Then {1, 0, 1
7 min read
Queries to check whether all the elements can be made positive by flipping signs exactly K times
Given an integer array arr[], and some queries consisting of an integer K, the task is to determine if its possible to make all the integers positive by flipping signs of integers exactly K times. We can flip the sign of an integer more than once. If possible, then print Yes else print No.Examples: Input: arr[] = {-1, 2, -3, 4, 5}, q[] = {1, 2} Out
8 min read
Check if given strings can be made same by swapping two characters of same or different strings
Given an array of equal-length strings, arr[] of size N, the task is to check if all the strings can be made equal by repeatedly swapping any pair of characters of same or different strings from the given array. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: arr[] = { "acbdd", "abcee" } Output: YES Explanation: Swapp
11 min read
Check if all bits can be made same by single flip
Given a binary string, find if it is possible to make all its digits equal (either all 0's or all 1's) by flipping exactly one bit. Input: 101Output: YeExplanation: In 101, the 0 can be flipped to make it all 1 Input: 11Output: NoExplanation: No matter whichever digit you flip, you will not get the desired string. Input: 1Output: YesExplanation: We
5 min read
Python Program to check if two sentences can be made the same by rearranging the words
Given two strings S1 and S2 representing two sentences, the task is to check if two sentences can be made same by rearranging the words of any of the strings. Examples: Input: S1 = "please select a category", S2 = "category a please select"Output: Yes Input: S1 = "hello world this is python language", S2 = "python language is this modern world" Out
5 min read
Check if all the elements can be made of same parity by inverting adjacent elements
Given a binary matrix. In a single operation, you are allowed to choose two adjacent elements and invert their parity. The operation can be performed any number of times. Write a program to check if all the elements of the array can be converted into a single parity. Examples: Input: a[] = {1, 0, 1, 1, 0, 1} Output: Yes Invert 2nd and 3rd elements
4 min read
Check if all strings of an array can be made same by interchanging characters
Given an array arr[] of size N consisting of equal length strings, the task is to check if it is possible to make all strings of the array can be equal or not by swapping any character of one string with any character of the same string or another string. Note: Perform the operation 0 or more times. Examples: Input : arr[] = {“fdd”, “fhh”}Output: Y
12 min read
Maximize distance between any two consecutive 1's after flipping M 0's
Given the size of a binary array consisting of 0's only as n and an integer m which is the number of flips allowed from 0's o 1's; the task is to maximize the distance between any two consecutive 1's after flipping m 0's to 1's. Examples: Input: n = 5, m = 3 Output: 2Explanation: The initial array is arr = {0, 0, 0, 0, 0}, The final array is arr =
7 min read
Check if Array elements can be made consecutive by performing given operations
Given an array arr[] of size N, the task is to check if it is possible to make all array elements consecutive by performing any of the following operations: Leave the element unchanged (arr[i] = arr[i])Increment the element by 1Decrement the element by 1 Note: For every array element, one of the above operations can be applied only once. Examples:
11 min read
Check if Array can be made strictly increasing by merging three consecutive elements M times
Given an array arr[] of length N and an integer M, the task is to determine if a strictly increasing sequence can be formed by merging three consecutive elements exactly M times. Note: Merging three elements means removing all three of them and inserting a single element having value same as the sum of the three at the same position. Examples: Inpu
11 min read
Practice Tags :