Minimum shifts of substrings of 1s required to group all 1s together in a given Binary string
Last Updated :
15 Nov, 2021
Given a binary string S of length N, the task is to print the minimum number of indices, substrings consisting only of 1s are required to be shifted such that all 1s present in the string are grouped together.
Examples:
Input: S = “00110111011”
Output: 2
Explanation:
Operation 1: Shift substring {S[2], S[3]} (0-based indexing) to right and place between S[4] and S[5]. Now, S is modified to “00011111011”.
Operation 2: Shift substring {S[10], S[11]} to left and place between S[7] and S[8]. Now, S is modified to “00011111110”.
Therefore, 2 substrings are required to be shifted.
Input: S = “1001001”
Output: 4
Explanation:
Operation 1: Shift ‘1’ at S[0] to right by two indices and place between S[2] and S[3]. Now, S is modified to “0011001”.
Operation 2: Shift ‘1’ at S[6] to left by two indices and place between S[3] and S[4]. Now, S is modified to “0011100”.
Therefore, 4 substrings are required to be shifted.
Approach: The problem can be solved by observing that the minimum number of operations required is equal to the number of 0s present between any pair of consecutive 1s. Follow the steps below to solve the problem:
- Iterate over the characters of the string and search for the first and last occurrences of the character ‘1’ and store it in variable, say firstOne and lastOne respectively.
- Traverse the range [firstOne, lastOne] and count the number of ‘0’s present in the substring {S[firstOne], .. , S[lastOne]} and print it as the required output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countShifts(string str)
{
int firstOne = -1;
int lastOne = -1;
int count = 0;
for ( int i = 0; i < str.length(); i++) {
if (str[i] == '1' ) {
if (firstOne == -1)
firstOne = i;
lastOne = i;
}
}
if ((firstOne == -1) || (firstOne == lastOne)) {
cout << 0;
return ;
}
for ( int i = firstOne; i <= lastOne; i++) {
if (str[i] == '0' ) {
count++;
}
}
cout << count << endl;
}
int main()
{
string str = "00110111011" ;
countShifts(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void countShifts(String str)
{
int firstOne = - 1 ;
int lastOne = - 1 ;
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == '1' )
{
if (firstOne == - 1 )
firstOne = i;
lastOne = i;
}
}
if ((firstOne == - 1 ) || (firstOne == lastOne))
{
System.out.print( 0 );
return ;
}
for ( int i = firstOne; i <= lastOne; i++)
{
if (str.charAt(i) == '0' )
{
count++;
}
}
System.out.println(count);
}
public static void main (String[] args)
{
String str = "00110111011" ;
countShifts(str);
}
}
|
Python3
def countShifts( str ):
firstOne = - 1
lastOne = - 1
count = 0
for i in range ( len ( str )):
if ( str [i] = = '1' ):
if (firstOne = = - 1 ):
firstOne = i
lastOne = i
if ((firstOne = = - 1 ) or
(firstOne = = lastOne)):
print ( 0 )
return
for i in range (firstOne, lastOne + 1 , 1 ):
if ( str [i] = = '0' ):
count + = 1
print (count)
str = "00110111011"
countShifts( str )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void countShifts( string str)
{
int firstOne = -1;
int lastOne = -1;
int count = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '1' )
{
if (firstOne == -1)
firstOne = i;
lastOne = i;
}
}
if ((firstOne == -1) || (firstOne == lastOne))
{
Console.Write(0);
return ;
}
for ( int i = firstOne; i <= lastOne; i++)
{
if (str[i] == '0' )
{
count++;
}
}
Console.WriteLine(count);
}
static void Main()
{
string str = "00110111011" ;
countShifts(str);
}
}
|
Javascript
<script>
function countShifts(str)
{
let firstOne = -1;
let lastOne = -1;
let count = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] == '1' )
{
if (firstOne == -1)
firstOne = i;
lastOne = i;
}
}
if ((firstOne == -1) || (firstOne == lastOne))
{
Console.Write(0);
return ;
}
for (let i = firstOne; i <= lastOne; i++)
{
if (str[i] == '0' )
{
count++;
}
}
document.write(count);
}
let str = "00110111011" ;
countShifts(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...