Max Consecutive Ones – Leetcode

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

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
 

Solution

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

References
https://leetcode.com/problems/max-consecutive-ones/

T Tak

Hey ! I'm Tak. I'm a software developer since 2008 and been programming since I was 17 years, started with pascal. I love programming, teaching and building stuff on web.

Leave a Reply

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