Skip to main content

Max Consecutive Ones

Problem Statement:​

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


βœ… Solution: Linear Scan​

int findMaxConsecutiveOnes(vector<int>& binaryArray) {
int maxConsecutiveOnes = 0; // Stores the maximum count of consecutive 1s
int currentCount = 0; // Tracks the current streak of 1s

for (int i = 0; i < binaryArray.size(); i++) {
if (binaryArray[i] == 1) {
currentCount++; // Extend the streak when we find a 1
} else {
// Update the max if current streak ends
maxConsecutiveOnes = max(maxConsecutiveOnes, currentCount);
currentCount = 0; // Reset the streak
}
}

// Final check in case the array ends with a streak of 1s
maxConsecutiveOnes = max(maxConsecutiveOnes, currentCount);

return maxConsecutiveOnes;
}


πŸ“ How It Works​

  • You scan the array once, keeping a running count of consecutive 1s.
  • Each time you see a 1, you increment currentCount.
  • If you see a 0, you:
    • Compare currentCount with maxConsecutiveOnes and update it if needed.
    • Reset currentCount to 0.
  • After the loop ends, you again update maxConsecutiveOnes to handle the case where the array ends with 1s.

🧩 Key Logic​

There’s no recurrence here β€” it’s a simple one-pass comparison-based logic:

maxConsecutiveOnes = max(maxConsecutiveOnes, currentCount)


⏱️ Time & Space Complexity​

MetricValue
⏱ TimeO(n)
πŸ—‚ SpaceO(1)
  • Single pass through the array.
  • Only two integer variables used.

⚠️ Edge Cases​

  • All elements are 1 β†’ final max is updated at the end.
  • All elements are 0 β†’ maxConsecutiveOnes remains 0.
  • Single element β†’ works correctly for both 0 and 1.

πŸ’‘ Other Approaches​

ApproachTimeSpaceNotes
Linear Scan (this)O(n)O(1)βœ… Most efficient
Segment TreeO(log n)O(n)❌ Overkill
Sliding Window (Fixed Size)Not applicable hereβ€“βŒ Only works for fixed-length windows

πŸ’¬

Discussion & Doubts