- Bit Magic, Mathematical, Strings

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:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: arr[] = {0, 0, 1, 0, 0, 0}, K = 1Output: 2Explanation: The 1st and the 5th index in the array arr[] can be flipped such that there is atleast 1 zero between any two 1’s. Therefore, the array after flipping is arr[] = {1, 0, 1, 0, 1, 0}.Input: arr[] = {1, 0, 0, 0, 0, 0, 0, 0, 1, 0}, K = 2Output: 1Explanation: The 4th index in the above array is the only valid index that can be flippedApproach: The given problem can be solved by iterating through the array and finding the count of consecutive zeroes between the two 1’s. Suppose, the number of 0’s between two 1’s is X. Then, it can be observed that the number of 0’s that can be flipped in between are (X-K) / (K+1). Therefore, traverse the array and keep track of the number of consecutive 0’s between two 1’s similar to the algorithm discussed here and add the count of 0’s that can be flipped into a variable cnt, which is the required answer.Below is the implementation of the above approach:C++#include using namespace std;  int maximumOnes(int arr[], int N, int K){          int cnt = 0;          int last = -(K + 1);          for (int i = 0; i < N; i++) {                  if (arr[i] == 1) {                                                  if (i - last - 1 >= 2 * (K – 1)) {                cnt += (i – last – 1 – K) / (K + 1);            }                          last = i;        }    }              cnt += (N – last – 1) / (K + 1);          return cnt;}  int main(){    int arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };    int N = sizeof(arr) / sizeof(arr[0]);    int K = 2;      cout