Number of Times Binary String Is Prefix-Aligned - Problem

Imagine you're controlling a row of smart light switches that start in the OFF position. You have a special remote control that can turn ON switches in a specific sequence, but here's the catch: you can't choose which switch to turn on next — the sequence is predetermined!

You start with a binary string of length n where all bits are initially 0 (switches OFF). You're given a 1-indexed array flips where flips[i] tells you which switch position to turn ON during the i-th step.

A configuration is called "prefix-aligned" when, after step i, the first i switches are all ON (positions 1 through i are all 1s) and all remaining switches are OFF (all 0s).

Goal: Count how many times during the entire flipping process the switches become prefix-aligned.

Example: If flips = [3,2,4,1,5] and n = 5:
• Step 1: Turn ON position 3 → 00100 (not prefix-aligned)
• Step 2: Turn ON position 2 → 01100 (not prefix-aligned)
• Step 3: Turn ON position 4 → 01110 (not prefix-aligned)
• Step 4: Turn ON position 1 → 11110 (prefix-aligned! First 4 positions are 1111)
• Step 5: Turn ON position 5 → 11111 (prefix-aligned! All 5 positions are 11111)

Answer: 2 times prefix-aligned.

Input & Output

example_1.py — Basic Case
$ Input: flips = [3,2,4,1,5]
Output: 2
💡 Note: Step 1: Position 3 → '00100' (not aligned). Step 2: Position 2 → '01100' (not aligned). Step 3: Position 4 → '01110' (not aligned). Step 4: Position 1 → '11110' (aligned! positions 1-4 are all 1s). Step 5: Position 5 → '11111' (aligned! all positions 1-5 are 1s). Total: 2 times.
example_2.py — Sequential Order
$ Input: flips = [1,2,3,4]
Output: 4
💡 Note: Perfect sequential flipping: Step 1: '1000' (aligned). Step 2: '1100' (aligned). Step 3: '1110' (aligned). Step 4: '1111' (aligned). Every step creates prefix alignment, so answer is 4.
example_3.py — Single Element
$ Input: flips = [1]
Output: 1
💡 Note: Only one switch to flip. After step 1: position 1 becomes '1', which is perfectly prefix-aligned for step 1. Answer: 1.

Visualization

Tap to expand
🎭 Theater Stage Lighting ControlStage12345Control Sequence: [3, 2, 4, 1, 5]Step 1: Turn ON light 3 → Max position = 3, Step = 1 → 3 ≠ 1 ❌Step 4: Turn ON light 1 → Max position = 4, Step = 4 → 4 = 4 ✅Perfect prefix: Lights 1,2,3,4 all ON, Light 5 OFF💡 Key InsightPrefix-aligned when max_position_turned_on == current_step_numberNo need to check each light individually!
Understanding the Visualization
1
Initial Setup
All 5 stage lights are OFF, sequence is [3,2,4,1,5]
2
Turn ON Light 3
Pattern: OFF-OFF-ON-OFF-OFF (not consecutive from left)
3
Turn ON Light 2
Pattern: OFF-ON-ON-OFF-OFF (still not consecutive from left)
4
Turn ON Light 4
Pattern: OFF-ON-ON-ON-OFF (not consecutive from position 1)
5
Turn ON Light 1
Pattern: ON-ON-ON-ON-OFF (perfect! First 4 lights consecutive)
6
Turn ON Light 5
Pattern: ON-ON-ON-ON-ON (perfect! All 5 lights consecutive)
Key Takeaway
🎯 Key Insight: Instead of maintaining the entire light state, just track the rightmost light that's been turned ON. When this equals the step number, you have perfect prefix alignment!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the flips array, constant time operations per step

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables to track maximum position and counter

n
2n
Linear Space

Constraints

  • n == flips.length
  • 1 ≤ n ≤ 5 × 104
  • flips is a permutation of the integers in the range [1, n]
  • All flip positions are 1-indexed
Asked in
Google 25 Amazon 18 Microsoft 12 Meta 8
28.5K Views
Medium Frequency
~12 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen