Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input:
 [1,1,0,1,1,1]

Output:
 3

Explanation:
 The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.

Note:

  • The input array will only contain 0 and 1 .
  • The length of input array is a positive integer and will not exceed 10,000

Subscribeto see which companies asked this question.

Hide Tags

Array

Hide Similar Problems

(M) Max Consecutive Ones II


public class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int maxLen = 0;
         int len = 0;
         for(int i=0;i<nums.length;i++){
             int num = nums[i];
             if(num == 1){
                 len++;
                 maxLen = Math.max(maxLen,len);
             }else{
                 len = 0;
             }
         }
         return maxLen;
    }
}

results matching ""

    No results matching ""