Check if K 0s can be flipped such that the given Array has no adjacent 1s Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: arr[] = {1, 0, 0, 0, 1}, K = 2Output: falseExplanation: The 0 at index 2 can be replaced by 1. Hence 1 element can be flipped (!= K). Approach: The solution is based on greedy approach. Please follow the steps mentioned below: Iterate over the array and for every index which has 0, check if its adjacent two indices have 0 or not. For each possible flip, decrement the count of K.For the last and first index of the array, check for the adjacent left and right index respectively.For every such index satisfying the above condition, Decrement the count of K if it is possible.Return true if K<=0, else return false. Below is the implementation of the above approach. C++ // C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to check // the possibility of K flips bool flips(vector<int>& arr, int K) { if (arr[0] == 0 && (arr.size() == 1 || arr[1] == 0)) { arr[0] = 1; K--; } for (int i = 1; i < arr.size() - 1; i++) { if (arr[i - 1] == 0 && arr[i] == 0 && arr[i + 1] == 0) { arr[i] = 1; K--; } } if (arr.size() > 1 && arr[arr.size() - 2] == 0 && arr[arr.size() - 1] == 0) { arr[arr.size() - 1] = 1; K--; } return K <= 0; } // Driver code int main() { vector<int> arr = { 0, 0, 0, 0, 1 }; int K = 2; cout << (flips(arr, K) ? "true" : "false"); return 0; } Java // Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to check // the possibility of K flips static Boolean flips(int arr[], int K) { if (arr[0] == 0 && (arr.length == 1 || arr[1] == 0)) { arr[0] = 1; K--; } for (int i = 1; i < arr.length - 1; i++) { if (arr[i - 1] == 0 && arr[i] == 0 && arr[i + 1] == 0) { arr[i] = 1; K--; } } if (arr.length > 1 && arr[arr.length - 2] == 0 && arr[arr.length - 1] == 0) { arr[arr.length - 1] = 1; K--; } return K <= 0; } // Driver code public static void main (String[] args) { int arr[] = { 0, 0, 0, 0, 1 }; int K = 2; System.out.println(flips(arr, K) ? "true" : "false"); } } // This code is contributed by hrithikgarg03188 Python3 # Python code for the above approach # Function to check # the possibility of K flips def flips(arr, K): if (arr[0] == 0 and (len(arr) == 1 or arr[1] == 0)): arr[0] = 1; K -= 1 for i in range(1, len(arr) - 1): if (arr[i - 1] == 0 and arr[i] == 0 and arr[i + 1] == 0): arr[i] = 1; K -= 1 if (len(arr) > 1 and arr[len(arr) - 2] == 0 and arr[len(arr) - 1] == 0): arr[len(arr) - 1] = 1; K -= 1 return K <= 0; # Driver code arr = [0, 0, 0, 0, 1]; K = 2; if (flips(arr, K)): print("true"); else: print("false"); # This code is contributed by Saurabh Jaiswal C# // C# program for the above approach using System; class GFG { // Function to check // the possibility of K flips static Boolean flips(int[] arr, int K) { if (arr[0] == 0 && (arr.Length == 1 || arr[1] == 0)) { arr[0] = 1; K--; } for (int i = 1; i < arr.Length - 1; i++) { if (arr[i - 1] == 0 && arr[i] == 0 && arr[i + 1] == 0) { arr[i] = 1; K--; } } if (arr.Length > 1 && arr[arr.Length - 2] == 0 && arr[arr.Length - 1] == 0) { arr[arr.Length - 1] = 1; K--; } return K <= 0; } // Driver code public static void Main () { int[] arr = { 0, 0, 0, 0, 1 }; int K = 2; Console.Write(flips(arr, K) ? "true" : "false"); } } // This code is contributed by Saurabh Jaiswal JavaScript <script> // JavaScript code for the above approach // Function to check // the possibility of K flips function flips(arr, K) { if (arr[0] == 0 && (arr.length == 1 || arr[1] == 0)) { arr[0] = 1; K--; } for (let i = 1; i < arr.length - 1; i++) { if (arr[i - 1] == 0 && arr[i] == 0 && arr[i + 1] == 0) { arr[i] = 1; K--; } } if (arr.length > 1 && arr[arr.length - 2] == 0 && arr[arr.length - 1] == 0) { arr[arr.length - 1] = 1; K--; } return K <= 0; } // Driver code let arr = [0, 0, 0, 0, 1]; let K = 2; if (flips(arr, K)) document.write("true"); else document.write("false"); // This code is contributed by Potta Lokesh </script> Outputtrue Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if K 0s can be flipped such that the given Array has no adjacent 1s C code_r Follow Improve Article Tags : Greedy Algo Geek DSA Arrays Algo-Geek 2021 binary-string +2 More Practice Tags : ArraysGreedy Similar Reads 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 Check if K '0's can be flipped such that Binary String contains no pair of adjacent '1's Given a binary string S of length N and an integer K, the task is to check if it is possible to flip K 0s such that the resulting string does not contain any pair of adjacent 1s. If it is possible to do so, then print "Yes". Otherwise, print "No". Input: S = "01001001000", K = 1Output: YesExplanatio 9 min read Maximize 0s to be flipped in given Binary array such that there are at least K 0s between two 1s Given a binary array arr[] and an integer K, the task is to count the maximum number of 0's that can be flipped to 1's such that there are at least K 0's between two 1's. Example: Input: arr[] = {0, 0, 1, 0, 0, 0}, K = 1Output: 2Explanation: The 1st and the 5th index in the array arr[] can be flippe 7 min read Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1 Given a number N, find the number of ways to construct an array of size N such that it contains only 1s and 0s but no two consecutive indexes have value 1 in them. Examples: Input : 2 Output : 3 Explanation: For n=2, the possible arrays are: {0, 1} {1, 0} {0, 0} Input : 3 Output : 5 Explanation: For 6 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 Check if given Array can be made a binary Array with K consecutive 1s Given an array A[] of size N and an integer K. Each element of the array is either 0, 1 or 2. The task is to check if the array can be converted into a binary array (elements will be either 0 or 1) by replacing every element equal to 2 with either 0 or 1, such that there are only K occurrences of 1, 11 min read Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1. Given a square boolean matrix mat[n][n], find k such that all elements in k'th row are 0 and all elements in k'th column are 1. The value of mat[k][k] can be anything (either 0 or 1). If no such k exists, return -1. Examples: Input: bool mat[n][n] = { {1, 0, 0, 0}, {1, 1, 1, 0}, {1, 1, 0, 0}, {1, 1, 15 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 Check if all elements of binary array can be made 1 Given a binary array Arr and an integer K. If the value at index i is 1 you can change 0 to 1 with in the range of ( i - K ) to ( i + K ).The task is to determine whether all the elements of the array can be made 1 or not.Examples: Input: Arr = { 0, 1, 0, 1 }, K = 2 Output: 2 Input: Arr = { 1, 0, 0, 7 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, 4 min read Like