Maximize Number of 1's | Practice | GeeksforGeeks

Given a binary array arr of size N and an integer M. Find the maximum number of consecutive 1's produced by flipping at most M 0's.
 
Example 1:
Input:
N = 3
arr[] = {1, 0, 1}
M = 1
Output:
3
Explanation:
Maximum subarray is of size 3
which can be made subarray of all 1 after
flipping two zeros to 1.
Example 2:
Input:
N = 11
arr[] = {1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1}
M = 2
Output:
8
Explanation:
Maximum subarray is of size 8
which can be made subarray of all 1 after
flipping two zeros to 1.

 
Your Task:
Complete the function findZeroes() which takes array arr and two integers n, m, as input parameters and returns an integer denoting the answer. 
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 107
0 <= M <= N
0 <= arri <= 1
0
siddhant073 weeks ago
//Using sliding window technique
//O(N) solution
    int findZeroes(int arr[], int n, int m) {
        int i = 0;
        int j = 0; 
        int currLength = 0;
        int maxLength = 0;
        int cnt = 0;
        while(j < n){
            if(arr[j] == 1){
                currLength++;
                maxLength = std::max(currLength, maxLength);
                j++;
            }
            else{
                cnt++;
                currLength++;
                while(cnt > m){
                    if(arr[i] == 0)
                    cnt--;
                    currLength--;
                    i++;
                }
                j++;
                maxLength = std::max(currLength, maxLength);
            }
        }
        return maxLength;
    } 
0
lindan1231 month ago
int findZeroes(int arr[], int n, int m) {
        
        if(n==1 && m<=1)
        {
            return 1;
        }
        int i=0;
        int j=0;
        int ans =0;
        int c=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]==0)
            {
                c++;
            }
            while(c>m && j<=i)
            {
                if(arr[j]==0)
                {
                    c--;
                }
                j++;
            }
            ans = max(ans,i-j+1);
        }
        return ans;
    }  
Time Taken : 0.0sec
Cpp
0
gunjangoyal2821 month ago
Simple and short C++ solution using sliding window technique with TC : O(N) and SC : O(1)
 
int findZeroes(int arr[], int n, int m) {
        // code here
        int len = 0, start = 0, end = 0, ctr = 0;
        for(int i=0; i<n; i++){
            if(arr[i] == 1){
                end++;
            }
            else{
                if(ctr < m){
                    ctr++;
                    end++;
                }
                else{
                    while(arr[start] != 0){
                        start++;
                    }
                    start++;
                    end++;
                }
            }
            len = max(len,(end-start));
        }
        return len;
    }  
0
abhinavsinghbiz2 months ago
C++ Code: O(N) && O(1) : Run Time=0.8 secs!  
Detailed solution on Apni Kaksha! 
 
int findZeroes(int arr[], int n, int m){       int zerocount=0,i=0,ans=0;       for(int j=0;j<n;j++){           if(arr[j]==0){               zerocount++;           }           while(zerocount>m){               if(arr[i]==0){                   zerocount--;               }               i++;           }           ans=max(ans,j-i+1);       }       return ans;   } 
0
devopsraj202 months ago
def findZeroes(arr, n, m):
    max_length = 0
    left = 0
    zeros = []
    for i in range(n):
        # If find zero append it to the zeros array 
        if arr[i] == 0:
            zeros.append(i)
        # when zero exceeds the number then pop the first element and left will be the next 
        if len(zeros) > m:
            left = zeros.pop(0) + 1

        # check max_length and assigned if required
        if i - left + 1 > max_length:
            max_length = i - left + 1

    return max_length
0
jswxingyu13 months ago
Python3
def findZeroes(arr, n, m) :
    # code here
    # set a window just like the hint shows.
    left_index = 0
    right_index = 0
    zeros_count = []
    res = 0

    for i in range(n):
        if arr[i] == 0:
            zeros_count.append(i)
            if len(zeros_count) <= m:
                res = max(res, right_index-left_index)
            else:
                left_index = zeros_count[0]+1
                zeros_count.pop(0)
                res = max(res, right_index-left_index)
        else:
            res = max(res, right_index-left_index)   

        right_index += 1
        
        # print(left_index, right_index)
    return res + 1
+3
shiva10903 months ago
  int maxConsecutiveOne = 0 , start = 0 , zeroCount = 0;              for(int end = 0; end < n; end++)       {           if(arr[end] == 0) zeroCount++;                      while(zeroCount > m)           {               if(arr[start] == 0)                   zeroCount--;                                  start++;           }                      maxConsecutiveOne = max(maxConsecutiveOne , end-start+1);       }              return maxConsecutiveOne;
0
vineelpeddi1234 months ago
       int ans = 0 , count = 0;      int l = 0;      vector<int> idx;      for(int i = 0 ; i < n ; i++){          if(arr[i] == 0){              idx.push_back(i);          }      }      for(int i = 0 ; i < n ; i++){          if(arr[i] == 1){              count++;          }else{              if( m > 0){               count++;               m--;             }else{                count = i - idx[l];                l++;             }          }          ans = max(ans , count);      }      return ans;
0
samarthsaxena07776 months ago
int s=0,e=0,k=0,max=0;       while(e<=n-1)       {           if(a[e]==0)           k++;           while(k>m)           {               if(a[s]==0)               {                   k--;                   s++;               }               else               s++;           }           if(max<e-s+1)           max=e-s+1;           e++;       }       return max;
+1
aayushiiiisingh7 months ago
int findZeroes(int arr[], int n, int m) {       int cnt0 =0;       int l=0;       int ans=0;       for(int i=0; i<n; i++){           if(arr[i] == 0)               cnt0++;           while(cnt0 > m){              if(arr[l] == 0)                   cnt0--;               l++;           }           ans= max(ans, i-l+1);       }       return ans;   }  
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.