Max Consecutive Ones – Leetcode

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

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


This is an easy to solve problem. We keep on counting the ones till a 0 is encountered. And compare the current count of consecutive ones with the previous maximum. Here is the java solution which can be made more concise but for the sake of understanding it is more elaborated.

You may also like Java interview questions


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: