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.
- Initialize two counters:
currentStreakandmaxStreak, both starting at 0. - 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 resetcurrentStreakto 0.
- If the element is a 1, increase the
- After the loop, perform one final check to update
maxStreak(in case the array ended with a 1). - Print the final
maxStreak.
01EXAMPLE 1
Input
[1, 1, 0, 1, 1, 1]Output
3Explanation: Three consecutive 1s.
Constraints
- Single pass iteration.
ArraysCounting
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,1,0,1,1,1]
Expected Output
3
Click RUN to test your solution