oneby7

Arrays and Hashing Array basics

53. Max Consecutive Ones

Easy

You are given an array nums in which every element is 0 or 1. Return the length of its longest run of 1s.

A run of 1s is a block of 1s that sit next to each other, with no 0 between them. If nums holds no 1s at all, return 0.

Example 1

Input: nums = [1, 1, 0, 1, 1, 1, 0, 1]

Output: 3

Explanation: The 1s form three runs: two at the start, three in the middle and one at the end. The longest run has length 3.

Example 2

Input: nums = [0, 0, 0, 0]

Output: 0

Example 3

Input: nums = [1, 0, 1, 1, 1, 1]

Output: 4

Explanation: The longest run is the last four elements. It reaches the end of the array, so no 0 comes after it.

Check your understanding

Work out the answer for this input before you write any code.

nums = [0, 1, 1, 0, 1, 1, 1, 1]

Constraints

  • 1 ≤ nums.length ≤ 105
  • nums[i] is 0 or 1

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.