Consecutive Streak

MediumAcc. 93.5%
+30 XP 12

The Binary Streak

In digital transmission, "bursts" of active signals (1s) are common. Your task is to analyze a binary array (containing only 0s and 1s) and identify the longest continuous streak of active signals. This is a primary step in signal processing and data compression.

The Assignment

Your mission is to find the maximum streak length. Your function receives a binary array nums.

  1. Initialize two counters: currentStreak and maxStreak, both starting at 0.
  2. Iterate through the array:
    • If the element is a 1, increase the currentStreak.
    • If the element is a 0, update maxStreak (if the current streak was higher) and reset currentStreak to 0.
  3. After the loop, perform one final check to update maxStreak (in case the array ended with a 1).
  4. Print the final maxStreak.

01EXAMPLE 1

Input[1, 1, 0, 1, 1, 1]
Output3

Explanation: Three consecutive 1s.

Constraints

  • Single pass iteration.
ArraysCounting
JavaScript
Loading...
1 Hidden

Input Arguments

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

Expected Output

3

Click RUN to test your solution