Please login

Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in C

## Find Zeros to be Flipped so that number of Consecutive 1’s is maximized in C

In this page we will look into a coding question where we will learn how to find the zeros that needs to be flipped so that the number of consecutive 1’s is maximized
There might be different approach to solve this question, one you will find here. If your approach is bit different post it onto the comment section. ### Problem Statement

Given a binary array and number of zeros to be flipped, write a program to find the zeros that needs to be flipped so that the number of consecutive 1’s is maximized

Input Format
-First line will contain the number of elements present in the array.
-Second line will contain the elements of array
-Third line will contain the maximum numbers of zero which can be flipped.

Output Format

-Output contain the index of the zero’s those are flipped.

Sample Test Case : 1

Input
7
0 1 0 1 1 0 1
2

Output
The indexes are : 2, 5

Sample Test Case : 2

Input
11
1 0 0 1 1 0 1 0 1 1 1
2

Output
The indexes are : 5, 7 ### Program to find zeros to be flipped so that binary number is maximized in C

```#include <stdio.h>
void zeroesIndexes(int arr[],int zeroes, int n) //this function prints the zero index that should be flipped
{
int start =0, end = 0; //starting and ending of the window
int count =0; //number of zeros in window
int bestwindow =0, bestwindowstart= 0;// bestwindow size and starting point
while(end<n)
{
if(count <= zeroes) //if no of zeros is less than input zeros
{
if(arr[end]==0)
{
count++;
}
end++;    //increasing window size
}
if(count > zeroes) //if no of zeros greater than input zeroes
{
if(arr[start]==0)
{
count--;
}
start++;          //decreasing window size
}
if(end-start > bestwindow) //updating the best window
{
bestwindow = end-start;
bestwindowstart = start;
}
}

printf("The indexes are : ");
for (int i = bestwindowstart; i < bestwindow; ++i)
{
if(arr[i]==0)
printf("%d, ",i);
}
}
int main()
{
int arr[] = {0, 1, 0, 1, 1, 0, 1};
int zeroes= 2;         //no of zeroes that can be flipped
int n = sizeof(arr)/sizeof(arr);
zeroesIndexes(arr, zeroes, n);
return 0;
}```

## Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

## Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures