Find Bit whose minimum sequence flips makes all bits same

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.

Note:



  • 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.

Examples:

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”
Output: 1
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)

Efficient Approach:

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find which bit sequence 
// to be flipped
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check which bit is to be flipped
char bitToBeFlipped(string s)
{
    // variable to store first and
    // last character of string
    char last = s[s.length() - 1];
    char first = s[0];
  
    // Check if first and last characters
    // are equal, if yes, then return
    // the character which is not at last
    if (last == first) {
        if (last == '0') {
            return '1';
        }
        else {
            return '0';
        }
    }
  
    // else return last
    else if (last != first) {
        return last;
    }
}
  
// Driver Code
int main()
{
    string s = "1101011000";
  
    cout << bitToBeFlipped(s) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find which bit sequence 
// to be flipped 
  
class GfG { 
  
// Function to check which bit is to be flipped 
static char bitToBeFlipped(String s) 
    // variable to store first and 
    // last character of string 
    char last = s.charAt(s.length() - 1); 
    char first = s.charAt(0); 
  
    // Check if first and last characters 
    // are equal, if yes, then return 
    // the character which is not at last 
    if (last == first) { 
        if (last == '0') { 
            return '1'
        
        else
            return '0'
        
    
  
    // else return last 
    else if (last != first) { 
        return last; 
    
    return last;
  
// Driver Code 
public static void main(String[] args) 
    String s = "1101011000"
  
    System.out.println(bitToBeFlipped(s)); 

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find which bit 
# sequence to be flipped
  
# Function to check which bit is
# to be flipped
def bitToBeFlipped( s):
  
    # variable to store first and
    # last character of string
    last = s[len(s) - 1]
    first = s[0]
  
    # Check if first and last characters
    # are equal, if yes, then return
    # the character which is not at last
    if (last == first) :
        if (last == '0') :
            return '1'
      
        else :
            return '0'
  
    # else return last
    elif (last != first) :
        return last
  
# Driver Code
if __name__ == "__main__":
      
    s = "1101011000"
  
    print(bitToBeFlipped(s))
  
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find which bit sequence 
// to be flipped
using System;
  
class GfG { 
  
// Function to check which bit is to be flipped 
static char bitToBeFlipped(String s) 
    // variable to store first and 
    // last character of string 
    char last = s[s.Length - 1]; 
    char first = s[0]; 
  
    // Check if first and last characters 
    // are equal, if yes, then return 
    // the character which is not at last 
    if (last == first) { 
        if (last == '0') { 
            return '1'
        
        else
            return '0'
        
    
  
    // else return last 
    else if (last != first) { 
        return last; 
    
    return last;
  
// Driver Code 
public static void Main() 
    string s = "1101011000"
  
    Console.WriteLine(bitToBeFlipped(s)); 
// This code is contributed by anuj_67..

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find which bit sequence 
// to be flipped
  
// Function to check which bit is to be flipped
function bitToBeFlipped($s)
{
    // variable to store first and
    // last character of string
    $last = $s[strlen($s) - 1];
    $first = $s[0];
  
    // Check if first and last characters
    // are equal, if yes, then return
    // the character which is not at last
    if ($last == $first
    {
        if ($last == '0'
        {
            return '1';
        }
        else
        {
            return '0';
        }
    }
  
    // else return last
    else if ($last != $first
    {
        return $last;
    }
}
  
  
// Driver Code
$s = "1101011000";
echo bitToBeFlipped($s);
  
// This code is contributed by ihritik
?>

chevron_right


Output:

0

Time Complexity: O(1)
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.