Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given a binary array nums, return the maximum number of consecutive 1's in the array.
Example 1:
Input: nums = [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
Example 2:
Input: nums = [1,0,1,1,0,1] Output: 2
Constraints:
1 <= nums.length <= 105nums[i] is either 0 or 1.Problem summary: Given a binary array nums, return the maximum number of consecutive 1's in the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,1,0,1,1,1]
[1,0,1,1,0,1]
max-consecutive-ones-ii)max-consecutive-ones-iii)consecutive-characters)longer-contiguous-segments-of-ones-than-zeros)length-of-the-longest-alphabetical-continuous-substring)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #485: Max Consecutive Ones
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int ans = 0, cnt = 0;
for (int x : nums) {
if (x == 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
return ans;
}
}
// Accepted solution for LeetCode #485: Max Consecutive Ones
func findMaxConsecutiveOnes(nums []int) (ans int) {
cnt := 0
for _, x := range nums {
if x == 1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 0
}
}
return
}
# Accepted solution for LeetCode #485: Max Consecutive Ones
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = cnt = 0
for x in nums:
if x:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 0
return ans
// Accepted solution for LeetCode #485: Max Consecutive Ones
impl Solution {
pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
let mut ans = 0;
let mut cnt = 0;
for &x in nums.iter() {
if x == 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 0;
}
}
ans
}
}
// Accepted solution for LeetCode #485: Max Consecutive Ones
function findMaxConsecutiveOnes(nums: number[]): number {
let [ans, cnt] = [0, 0];
for (const x of nums) {
if (x) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.