Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Length of longest subarray consisting only of 1s

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

Java




// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Utility function to find the length of
// longest subarray containing only 1s
static int longestSubarrayUtil(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 = Math.max(m, old);
 
            //Assign new to old
            // and set new to zero
            old = neww;
            neww = 0;
        }
    }
    m = Math.max(m, old);
    // 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
static void longestSubarray(int []arr, int n)
{
     
    // Stores the length of longest
    // subarray consisting of 1s
    int len = longestSubarrayUtil(arr, n);
 
    // Print the length
    // of the subarray
    System.out.print(len);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = {1, 1, 1};
    int n = arr.length;
   
    // Function call to find the longest
    // subarray containing only 1's
    longestSubarray(arr, n);
}
}
 
// This code is contributed by amreshkumar3.

C#




using System;
 
public class GFG {
 
  // Function to find length of the
  // longest subarray containing only 1's
  static void longestSubarray(int[] arr, int n)
  {
 
    // Stores the length of longest
    // subarray consisting of 1s
    int len = longestSubarrayUtil(arr, n);
 
    // Print the length
    // of the subarray
    Console.WriteLine(len);
  }
 
  // Utility function to find the length of
  // longest subarray containing only 1s
  static int longestSubarrayUtil(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 = Math.Max(m, old);
 
        // Assign new to old
        // and set new to zero
        old = neww;
        neww = 0;
      }
    }
    m = Math.Max(m, old);
    // Return the final length
    if (m < n)
      return m;
    else
      return m - 1;
  }
 
  static public void Main()
  {
 
    // Code
    // Given array
    int[] arr = { 1, 1, 1 };
    int n = arr.Length;
 
    // Function call to find the longest
    // subarray containing only 1's
    longestSubarray(arr, n);
  }
}
 
// this code is contributed by devendra solunke

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)


My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials