Skip to content
Related Articles

Related Articles

Improve Article
Length of longest subarray consisting only of 1s
  • Difficulty Level : Basic
  • Last Updated : 09 Jun, 2021

Given an array arr[] of size N, consisting of binary values, the task is to find the longest non-empty subarray consisting only of 1s after removal of a single array element.

Examples:

Input: arr[] = {1, 1, 1}
Output: 2

Input: arr[] = {0, 0, 0}
Output: 0

Approach: Follow the steps below to solve the problem:



  • Initialize three variables, newLen = 0, prevLen = 0, maxLen = 0.
  • Traverse the array arr[] by appending zero at the beginning:
    • If arr[i] = 1: Increment both newLen & prevLen by 1.
    • Otherwise:
      • Assign the maximum value to the variable maxLen.
      • Set prevLen = newLen and newLen = 0.
  • Print maxLen if maxLen < len(arr).
  • Otherwise, print maxLen – 1.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to find the length of
// longest subarray containing only 1s
int longestSubarrayUtil(vector<int> arr, int n)
{
    int neww = 0, old = 0, m = 0;
     
    // Traverse the array
    for(int x = 0; x <= n; x++)
    {
         
        // If array element is 1
        if (arr[x] == 1)
        {
             
            // Increment both by 1
            neww += 1;
            old += 1;
        }
        else
        {
             
            // Assign maximum value
            m = max(m, old);
 
            //Assign new to old
            // and set new to zero
            old = neww;
            neww = 0;
        }
    }
    
    // Return the final length
    if (m < n)
    {
        return m;
    }
    else return m - 1;
}
 
// Function to find length of the
// longest subarray containing only 1's
void longestSubarray(vector<int> arr, int n)
{
     
    // Stores the length of longest
    // subarray consisting of 1s
    int len = longestSubarrayUtil(arr, n);
 
    // Print the length
    // of the subarray
    cout << len;
}
 
// Driver code
int main()
{
     
    // Given array
    vector<int> arr = {1, 1, 1};
    int n = arr.size();
     
    // Append 0 at beginning
    for(int i = n; i >= 0; i--)
    {
        arr[i] = arr[i - 1];
    }
    arr[0] = 0;
     
    // Function call to find the longest
    // subarray containing only 1's
    longestSubarray(arr, n);
}
 
// This code is contributed by SoumikMondal

Python3




# Python3 program for the above approach
 
# Utility function to find the length of
# longest subarray containing only 1s
def longestSubarrayUtil(arr):
 
    new, old, m = 0, 0, 0
 
    # Traverse the array
    for x in arr+[0]:
 
        # If array element is 1
        if x == 1:
 
            # Increment both by 1
            new += 1
            old += 1
        else:
 
            # Assign maximum value
            m = max(m, old)
 
            # Assign new to old
            # and set new to zero
            old, new = new, 0
 
    # Return the final length
    return m if m < len(arr) else m - 1
 
# Function to find length of the
# longest subarray containing only 1's
def longestSubarray(arr):
 
    # Stores the length of longest
    # subarray consisting of 1s
    len = longestSubarrayUtil(arr)
 
    # Print the length
    # of the subarray
    print(len)
 
 
# Given array
arr = [1, 1, 1]
 
# Function call to find the longest
# subarray containing only 1's
longestSubarray(arr)

Javascript




<script>
 
// JavaScript program for the above approach
 
// Utility function to find the length of
// longest subarray containing only 1s
function longestSubarrayUtil(arr, n)
{
    var neww = 0, old = 0, m = 0;
     
    // Traverse the array
    for(var x = 0; x <= n; x++)
    {
         
        // If array element is 1
        if (arr[x] == 1)
        {
             
            // Increment both by 1
            neww += 1;
            old += 1;
        }
        else
        {
             
            // Assign maximum value
            m = Math.max(m, old);
 
            //Assign new to old
            // and set new to zero
            old = neww;
            neww = 0;
        }
    }
    
    // Return the final length
    if (m < n)
    {
        return m;
    }
    else
    {
        return m - 1;
    }
}
 
// Function to find length of the
// longest subarray containing only 1's
function longestSubarray(arr, n)
{
     
    // Stores the length of longest
    // subarray consisting of 1s
    var len = longestSubarrayUtil(arr, n);
 
    // Print the length
    // of the subarray
    document.write( len);
}
 
// Driver code
// Given array
var arr = [1, 1, 1];
var n = arr.length;
 
// Append 0 at beginning
for(var i = n-1; i >= 0; i--)
{
    arr[i] = arr[i - 1];
}
arr[0] = 0;
 
// Function call to find the longest
// subarray containing only 1's
longestSubarray(arr, n);
 
 
</script>
Output: 
2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

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 industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :