Chef and Subarray Solution

Problem

Chef loves research! Now he is looking for subarray of maximal length with non-zero product.

Chef has an array A with N elements: A1A2, ..., AN.

Subarray Aij of array A is elements from index i to index jAiAi+1, ..., Aj.

Product of subarray Aij is product of all its elements (from ith to jth).

Input

  • First line contains sinlge integer N denoting the number of elements.
  • Second line contains N space-separated integers A1A2, ..., AN denoting the elements of array.

 

Output

  • In a single line print single integer - the maximal length of subarray with non-zero product.

 

Constraints

  • 1 ≤ N ≤ 100000
  • 0 ≤ Ai ≤ 10000

 

Sample 1:

Input
Output
6
1 0 2 3 0 4
2

Explanation:

For the first sample subarray is: {2, 3}.

Sample 2:

Input
Output
1
0
0

Explanation:

For the second sample there are no subbarays with non-zero product.

Sample 3:

Input
Output
3
1 0 1
1

Explanation:

For the third sample subbarays is {1}, (the first element, or the third one).





Program :


 /* package codechef; // don't place package name! */


import java.util.*;

import java.lang.*;

import java.io.*;


/* Name of the class has to be "Main" only if the class is public. */

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

    Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int a[]=new int[n];

int countA=0;

for(int i=0;i<n;i++)

{

    a[i]=sc.nextInt();

    if(a[i]==0)

      countA++;

}

int max=0;

int count=0;

for(int i=0;i<n;i++)

{

    if(a[i]!=0)

        count++;

    else

       count=0;

    if(count>max)

      max=count;

}

if(countA==n)

  System.out.println(0);

else

  System.out.println(max);

}

}


Post a Comment

0 Comments