Minimum count of 0s to be selected such that all 1s are adjacent to them Last Updated : 17 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: 2Explanation: '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: 1Explanation: '0' at index 2 can be selected. As a result one neighbor for both the '1' s are selected. Input: str = "111"Output: -1Explanation: There is no '0' in the given string. So there cannot be any neighbor of '1' which is '0'. Input: str = "110"Output: -1Explanation: 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++ // C++ code to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to count // minimum number of buckets int minimumBuckets(string str) { int bucketcount = 0; int N = str.size(); // Loop to count minimum buckets for (int i = 0; i < N;) { if (str[i] == '1') { // If bucket can be put, // no need of next two indices, // so shift to i+3 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; } // Driver code int main() { string str = "1001"; cout << minimumBuckets(str)<<endl; string str1 = "1010"; cout << minimumBuckets(str1); return 0; } Java // Java code to implement the above approach class GFG { // Function to count // minimum number of buckets static int minimumBuckets(String str) { int bucketcount = 0; int N = str.length(); // Loop to count minimum buckets for (int i = 0; i < N;) { if (str.charAt(i) == '1') { // If bucket can be put, // no need of next two indices, // so shift to i+3 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; } // Driver code public static void main(String args[]) { String str = "1001"; System.out.println(minimumBuckets(str)); String str1 = "1010"; System.out.println(minimumBuckets(str1)); } } // This code is contributed by Saurabh Jaiswal Python3 # python code to implement the above approach # Function to count # minimum number of buckets def minimumBuckets(str): bucketcount = 0 N = len(str) # Loop to count minimum buckets i = 0 while(i < N): if (str[i] == '1'): # If bucket can be put, # no need of next two indices, # so shift to i+3 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 # Driver code if __name__ == "__main__": str = "1001" print(minimumBuckets(str)) str1 = "1010" print(minimumBuckets(str1)) # This code is contributed by rakeshsahni C# // C# code to implement the above approach using System; class GFG { // Function to count // minimum number of buckets static int minimumBuckets(string str) { int bucketcount = 0; int N = str.Length; // Loop to count minimum buckets for (int i = 0; i < N;) { if (str[i] == '1') { // If bucket can be put, // no need of next two indices, // so shift to i+3 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; } // Driver code public static void Main() { string str = "1001"; Console.WriteLine(minimumBuckets(str)); string str1 = "1010"; Console.WriteLine(minimumBuckets(str1)); } } // This code is contributed by ukasp. JavaScript <script> // JavaScript code for the above approach // Function to count // minimum number of buckets function minimumBuckets(str) { let bucketcount = 0; let N = str.length; // Loop to count minimum buckets for (let i = 0; i < N;) { if (str[i] == '1') { // If bucket can be put, // no need of next two indices, // so shift to i+3 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; } // Driver code let str = "1001"; document.write(minimumBuckets(str) + '<br>'); let str1 = "1010"; document.write(minimumBuckets(str1) + '<br>'); // This code is contributed by Potta Lokesh </script> Output2 1 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum count of 0s to be selected such that all 1s are adjacent to them R rishabhbatra53 Follow Improve Article Tags : Strings Greedy Competitive Programming DSA binary-string +1 More Practice Tags : GreedyStrings Similar Reads Minimum removals to segregate all 0s and 1s such that count of 0s and 1s are equal Given a binary string S of length N, the task is to find the minimum number of removals to segregate all the 0s and all the 1s such that all the 0s are before all the 1s and their count are also same. Examples: Input: S = 01001101Output: 2Explanation: If you remove the 1 at index 1 and 0 at index 6, 7 min read Maximum number of 0s that can be flipped such that Array has no adjacent 1s Given a binary array arr, the task is to find the maximum number of 0s that can be flipped such that the array has no adjacent 1s, i.e. the array does not contain any two 1s at consecutive indices. Examples: Input: arr[] = {1, 0, 0, 0, 1} Output: 1 Explanation: The 0 at index 2 can be replaced by 1. 4 min read Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart Given a binary string s and a number K, the task is to find the maximum number of 0s that can be replaced by 1s such that two adjacent 1s are separated by at least K 0s in between them. Examples: Input: K = 2, s = "000000" Output: 2 Explanation:Change the 0s at position 0 and 3. Then the final strin 8 min read Minimum deletion such that Xor of adjacent digits is atmost 1 Given a string S consisting of N digits, the task is to find the minimum number of deletions such that the bitwise Xor of any two adjacent digits of the remaining string is at most 1. Examples: Input: S = "24244"Output: 2?Explanation: We can delete both the 2's to make the string "444", which satisf 5 min read Print n 0s and m 1s such that no two 0s and no three 1s are together Given two integers n and m where n is the number of 0s and m is the number of 1s. The task is to print all the 0s and 1s in a single row such that no two 0s are together and no three 1s are together. If it's not possible to arrange 0s and 1s according to the condition then print -1.Examples: Input: 6 min read Minimize insertion of 0 or 1 such that no adjacent pair has same value Given a binary array A[] of length N, the task is to find the minimum number of operations required such that no adjacent pair has the same value where in each operation we can insert either 0 or 1 at any position in the array. Examples: Input: A[] = {0, 0, 1, 0, 0} Output: 2?Explanation: We can per 4 min read Minimum operations required to convert a binary string to all 0s or all 1s Given a binary string str, the task is to find the minimum number of operations required to make all the characters of the string same i.e. either the resultant string contains all 0s or all 1s. In a single operation, any block of consecutive 0s can be converted to a block of consecutive 1s of the s 4 min read Check if K 0s can be flipped such that the given Array has no adjacent 1s Given a binary array arr[] of size N, and an integer K, the task is to check if K 0s can be flipped such that the array has no adjacent 1s. Examples: Input: arr[] = {0, 0, 0, 0, 1}, K=2Output: trueExplanation: The 0 at indices 0 and 2 can be replaced by 1. Hence 2 elements can be flipped (=K). Input 5 min read Remove all 1s from the adjacent left of 0s in a Binary Array Given a binary array arr[], the task is to find the number of operations required to remove all 1s from the adjacent left of 0s. In each operation, all 1s, to the immediate left of a 0, are changed to 0. Examples: Input: arr[] = { 1, 0, 0, 1, 1, 0 } Output: 2 Explanation: Operation 1: Change in inde 6 min read Maximum flips possible such that no pair of adjacent elements are both 1 Given a binary array arr[] of size N, the task is to find the maximum count of 0s that can be converted into 1s such that no pair of adjacent array elements are 1. Examples: Input: arr[] = { 1, 0, 0, 0, 1 } Output: 1 Explanation: Updating arr[2] to 1 modifies arr[] to { 1, 0, 1, 0, 1 } Therefore, th 6 min read Like