Longest contiguous 1’s 

Given an array of elements containing 0’s and 1’s. You have to find the length of longest contiguous 1’s.

Input Format

First line of input contains N – size of the array. The next line contains the N integers of array A.

Constraints

1 <= N <= 1000

Output Format

Print the length of longest contiguous 1’s.

Input Output
10 1 0 0 1 0 1 1 1 1 04

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int arr[] = new int[N];
        for(int i=0; i<N; i++){
                arr[i] = sc.nextInt();
        }
        int count=0;
        int max = 0;
        for(int i:arr){
            if(i==1){
                count++;   
                if(count>max)
                    max=count;
            }
            else
                count=0;
        }
        System.out.println(max);
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top