- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximize 0s to be flipped in a given Binary array such that there are at least K 0s between two 1s
A Binary Array is a special type of array that only contains the numbers 0 and 1. In this problem, we have given a binary array and integer K. Our task is to count the maximum number of 0’s that can be flipped to 1 in a given binary array such that there are at least K 0’s between two 1s.
Sample Examples
Input 1: arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, K = 2
Output 1: yes
Explanation
The 3rd and 6th indexes of the above array in the only valid indexes for filliping so that there are at least 2 zeros between the two 1s. Therefore, the resultant arr is {1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0}
Input 2: arr[] = {0, 1, 0, 0, 0, 1}, k = 1
Output 2: 1
Explanation
The 3rd index of the above array is the only valid index for flipping.
Approach
We have seen the example above for the given array and integer k, let us move to the approach −
The idea of this approach is that count the consecutive zeros between the two ones and check whether it is suitable to fillip some zeros to one between them. Let's say there are X 0s in between two 1s. The number of 0s that can be flipped between is (X-K) / (K+1), as per the observation. Therefore, traverse the array and keep note of how many consecutive 0s there are between each pair of 1s. Then, add the number of 0s that can be flipped into the variable count, which is the needed response.
Let’s discuss this approach step by step below −
First, we will create a function ‘onesCount’ that will take the given array ‘arr’ and the integer ‘K’ as the parameter and will return the required integer ‘count’ as the return value.
Create variable count and lastIdx.
Initialize the variable count with 0 to store the fillip 0s count.
Initialize the variable lastIdx with (-(K+1)) to store the last index of value 1 of the array.
Traverse the array using for loop, which checks if the current element is 1 then verifies whether there are enough 0s between two consecutive 1s to add another 1 in between them. In the end, update the value of the last index of 1.
Write the condition for calculating the last section of 0s of the array and add it to the variable count.
In the end, return our final answer count.
Example
Below is C++ program for count maximize 0s to be filliped 1 such that there are at least k o's present between two 1s.
#include <bits/stdc++.h> using namespace std; // Function to find the count of the maximum number of 0s to be filliped int onesCount(int arr[], int n, int k){ int count = 0; // Stores the count of 1's int lastIdx = -(k + 1); // Stores the last index of value 1 // Traverse the array using for loop for (int i = 0; i < n; i++) { // If the current element is 1 if (arr[i] == 1) { // Verify whether there are enough 0s between two consecutive 1s to add another 1 in between them. if (i - lastIdx - 1 >= 2 * (k - 1)) { count += (i - lastIdx - 1 - k) / (k + 1); } lastIdx = i; // Update the last index of the value 1 of the array } } // condition to include the last section of 0s in the array count += (n - lastIdx - 1) / (k + 1); // Return the answer return count; } int main(){ int arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }; // given array int N = sizeof(arr) / sizeof(arr[0]); //getting size of an array int K = 2; //given integer // Call the function int result = onesCount(arr, N, K); cout<< "The count of Maximum filliped of 0's is "<< result ; return 0; }
Output
The Count of Maximum filliped of 0's is 2
Time and Space Complexity
The time complexity of the above code is O(N), as we traverse the array only. Where N is the size of the given array.
The space complexity of the above code is O(1), as we are not using any extra space.
Conclusion
In this tutorial, we have implemented a program to find the Maximize 0s to be flipped in a given Binary array such that there are at least K 0s between two 1s. Solved this problem by counting the consecutive zeros between the two ones and checking whether it is suitable to fillip some zeros to one between them. The time complexity is O(N) and the space complexity of O(1). Where N is the size of the string.