Minimum count of 0s to be selected such that all 1s are adjacent to them
Last Updated :
17 Jan, 2022
Given a binary string str of size N whose every character is either ‘1’ or ‘0’. The task is to select minimum number of 0‘s such that at least one neighbor for every ‘1’ is selected. Print the count of the selected 0′s.
Examples:
Input: str = “1001”
Output: 2
Explanation: ‘0’s can be selected from index 1 and index 2. As a result, every ‘1’ has at least one neighbor present among the selected ‘0’s.
Input: str = “01010”
Output: 1
Explanation: ‘0’ at index 2 can be selected. As a result one neighbor for both the ‘1’ s are selected.
Input: str = “111”
Output: -1
Explanation: There is no ‘0’ in the given string. So there cannot be any neighbor of ‘1’ which is ‘0’.
Input: str = “110”
Output: -1
Explanation: There is no ‘0’ as neighbor for ‘1’ at first position.
Approach: The solution is based on greedy approach. Follow the below steps to get the solution.
- Start iterating the string from the beginning.
- For each ‘1’ If possible, a ‘0’ is selected from its neighborhood.
- Now, if there is ‘0’ before as well as after current ‘1’, then always select the neighbor next to the current ‘1’ (because there can be more ‘1’s after this one and doing so will allow to select minimum number of neighbors).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumBuckets(string str)
{
int bucketcount = 0;
int N = str.size();
for ( int i = 0; i < N;) {
if (str[i] == '1' ) {
if (i + 1 < N &&
str[i + 1] == '0' ) {
bucketcount++;
i += 3;
continue ;
}
if (i - 1 >= 0 &&
str[i - 1] == '0' ) {
bucketcount++;
i++;
continue ;
}
return -1;
}
i++;
}
return bucketcount;
}
int main()
{
string str = "1001" ;
cout << minimumBuckets(str)<<endl;
string str1 = "1010" ;
cout << minimumBuckets(str1);
return 0;
}
|
Java
class GFG {
static int minimumBuckets(String str) {
int bucketcount = 0 ;
int N = str.length();
for ( int i = 0 ; i < N;) {
if (str.charAt(i) == '1' ) {
if (i + 1 < N && str.charAt(i + 1 ) == '0' ) {
bucketcount++;
i += 3 ;
continue ;
}
if (i - 1 >= 0 && str.charAt(i - 1 ) == '0' ) {
bucketcount++;
i++;
continue ;
}
return - 1 ;
}
i++;
}
return bucketcount;
}
public static void main(String args[]) {
String str = "1001" ;
System.out.println(minimumBuckets(str));
String str1 = "1010" ;
System.out.println(minimumBuckets(str1));
}
}
|
Python3
def minimumBuckets( str ):
bucketcount = 0
N = len ( str )
i = 0
while (i < N):
if ( str [i] = = '1' ):
if (i + 1 < N and str [i + 1 ] = = '0' ):
bucketcount + = 1
i + = 3
continue
if (i - 1 > = 0 and str [i - 1 ] = = '0' ):
bucketcount + = 1
i + = 1
continue
return - 1
i + = 1
return bucketcount
if __name__ = = "__main__" :
str = "1001"
print (minimumBuckets( str ))
str1 = "1010"
print (minimumBuckets(str1))
|
C#
using System;
class GFG {
static int minimumBuckets( string str)
{
int bucketcount = 0;
int N = str.Length;
for ( int i = 0; i < N;) {
if (str[i] == '1' ) {
if (i + 1 < N && str[i + 1] == '0' ) {
bucketcount++;
i += 3;
continue ;
}
if (i - 1 >= 0 && str[i - 1] == '0' ) {
bucketcount++;
i++;
continue ;
}
return -1;
}
i++;
}
return bucketcount;
}
public static void Main()
{
string str = "1001" ;
Console.WriteLine(minimumBuckets(str));
string str1 = "1010" ;
Console.WriteLine(minimumBuckets(str1));
}
}
|
Javascript
<script>
function minimumBuckets(str) {
let bucketcount = 0;
let N = str.length;
for (let i = 0; i < N;) {
if (str[i] == '1' ) {
if (i + 1 < N &&
str[i + 1] == '0' ) {
bucketcount++;
i += 3;
continue ;
}
if (i - 1 >= 0 &&
str[i - 1] == '0' ) {
bucketcount++;
i++;
continue ;
}
return -1;
}
i++;
}
return bucketcount;
}
let str = "1001" ;
document.write(minimumBuckets(str) + '<br>' );
let str1 = "1010" ;
document.write(minimumBuckets(str1) + '<br>' );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...