Given a binary list containing only 0s and 1s, return the maximum number of consecutive 1s. Return 0 for a null or empty list.
Example 1:
Input: nums = [1, 1, 0, 1, 1, 1]
Output: 3
Explanation: The longest run of consecutive 1s is the three at the end of the list.
Example 2:
Input: nums = [1, 1, 1]
Output: 3
Explanation: All elements are 1, so the entire list is one consecutive run.
Example 3:
Input: nums = [0, 0, 0]
Output: 0
Explanation: There are no 1s in the list, so the maximum consecutive count is zero.
Example 4:
Input: nums = []
Output: 0
Explanation: An empty list contains no elements, so the result is zero.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.