Find Latest Group of Size M - Problem

Imagine you have a binary string of length n that starts with all zeros: 000...000. You're given an array arr that represents a permutation of numbers from 1 to n. At each step i, you flip the bit at position arr[i] from 0 to 1.

Your mission: Find the latest step at which there exists a contiguous group of exactly m ones that cannot be extended in either direction (surrounded by zeros or string boundaries).

Example: If arr = [3,5,1,2,4] and m = 1, the binary string evolves like this:

  • Step 1: 00100 → group of size 1 at position 3
  • Step 2: 00101 → two groups of size 1
  • Step 3: 10101 → three groups of size 1
  • Step 4: 11101 → one group of size 3, one group of size 1
  • Step 5: 11111 → one group of size 5

The latest step with a group of size 1 is step 4, so return 4. If no group of size m ever exists, return -1.

Input & Output

example_1.py — Basic Case
$ Input: arr = [3,5,1,2,4], m = 1
Output: 4
💡 Note: At step 4, the binary string is '11101' which has groups [3,1]. The group of size 1 exists at position 4, and this is the latest step where a group of size 1 exists.
example_2.py — No Valid Group
$ Input: arr = [3,1,5,4,2], m = 3
Output: -1
💡 Note: We never get exactly a group of size 3. The groups evolve as: [1] → [1,1] → [1,1,1] → [1,4] → [5]. Size 3 appears momentarily but gets merged immediately.
example_3.py — Early Maximum
$ Input: arr = [1,2,3,4,5], m = 5
Output: 5
💡 Note: At step 5, all bits are set creating one group of size 5: '11111'. This is the only time we have a group of exactly size 5.

Constraints

  • n == arr.length
  • 1 ≤ n ≤ 105
  • 1 ≤ arr[i] ≤ n
  • All integers in arr are distinct
  • 1 ≤ m ≤ n

Visualization

Tap to expand
Find Latest Group of Size M - Visual EvolutionProblem: arr = [3,5,1,2,4], m = 1Initial: 0000012345Groups: noneStep 1: Turn on position 3 → 0010000100Groups: [1] ✓Step 2: Turn on position 5 → 0010100101Groups: [1,1] ✓Step 4: Turn on position 2 → 1110111101Groups: [3,1] ✓ Last valid!Step 5: Turn on position 4 → 1111111111Groups: [5] ✗ No size 1Algorithm Insight✓ Track all group sizes✓ Update on each bit flip✓ Remember latest valid step✓ Handle merging efficientlyAnswer: Step 4Last step with group of size 1
Understanding the Visualization
1
Initialize
Start with all switches off: 00000
2
Turn on switch 3
Binary: 00100, one group of size 1
3
Turn on switch 5
Binary: 00101, two groups of size 1
4
Turn on switch 1
Binary: 10101, three groups of size 1
5
Turn on switch 2
Binary: 11101, groups merge to sizes [3,1]
6
Track latest valid step
Remember the last step where a group of size m existed
Key Takeaway
🎯 Key Insight: Instead of rebuilding the entire state each time, track intervals and group sizes incrementally. When a bit is set, check if it merges adjacent intervals and update the size frequency counter accordingly. This reduces time complexity from O(n²) to O(n).
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
26.4K Views
Medium Frequency
~25 min Avg. Time
892 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