Watch 8 video solutions for Find Latest Group of Size M, a medium level problem involving Array, Hash Table, Binary Search. This walkthrough by AH Tech has 1,317 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an array arr that represents a permutation of numbers from 1 to n.
You have a binary string of size n that initially has all its bits set to zero. At each step i (assuming both the binary string and arr are 1-indexed) from 1 to n, the bit at position arr[i] is set to 1.
You are also given an integer m. Find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1's such that it cannot be extended in either direction.
Return the latest step at which there exists a group of ones of length exactly m. If no such group exists, return -1.
Example 1:
Input: arr = [3,5,1,2,4], m = 1 Output: 4 Explanation: Step 1: "00100", groups: ["1"] Step 2: "00101", groups: ["1", "1"] Step 3: "10101", groups: ["1", "1", "1"] Step 4: "11101", groups: ["111", "1"] Step 5: "11111", groups: ["11111"] The latest step at which there exists a group of size 1 is step 4.
Example 2:
Input: arr = [3,1,5,4,2], m = 2 Output: -1 Explanation: Step 1: "00100", groups: ["1"] Step 2: "10100", groups: ["1", "1"] Step 3: "10101", groups: ["1", "1", "1"] Step 4: "10111", groups: ["1", "111"] Step 5: "11111", groups: ["11111"] No group of size 2 exists during any step.
Constraints:
n == arr.length1 <= m <= n <= 1051 <= arr[i] <= narr are distinct.Problem Overview: You receive an array arr where arr[i] marks the position flipped from 0 to 1 at step i. After each step, groups of consecutive 1s form. The task is to return the latest step where at least one group has length exactly m.
Approach 1: Array Simulation for Borders (O(n) time, O(n) space)
This approach simulates group formation using a length array that stores the size of a contiguous group only at its boundaries. When position pos flips to 1, check the group size to the left and right using the boundary array. The new merged group length becomes left + right + 1. Update the boundaries at pos - left and pos + right with the new size. Track how many groups currently have size m and update the latest step whenever the count is positive. Each index is processed once, and group lengths are updated in constant time, giving O(n) time and O(n) space. This technique relies on efficient boundary updates in an array while simulating the process step by step, similar to problems in simulation.
Approach 2: Union-Find (O(n α(n)) time, O(n) space)
Another way to track connected components of 1s is using a disjoint-set (Union-Find) structure. Each time a position flips to 1, mark it active and union it with its active neighbors (left and right). The union operation merges components and updates their sizes. Maintain a counter for components whose size equals m. When two components merge, decrement counts for old sizes and increment for the new size if needed. Union-Find operations run in nearly constant time with path compression and union by rank, giving O(n α(n)) time and O(n) space. This approach models the problem as dynamic connectivity and works well when you think in terms of merging segments rather than updating boundaries directly. The structure behaves similarly to techniques used in hash table or connectivity problems.
Recommended for interviews: The boundary array simulation is the expected solution. It achieves strict O(n) time and avoids the overhead of Union-Find. Interviewers usually look for the key insight: store group lengths only at the boundaries so you can merge segments in constant time. Mentioning the Union-Find alternative shows you understand dynamic connectivity problems, but implementing the boundary technique demonstrates stronger problem-solving insight.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Array Boundary Simulation | O(n) | O(n) | Best general solution. Efficient when simulating dynamic group formation step by step. |
| Union-Find (Disjoint Set) | O(n α(n)) | O(n) | Useful when thinking in terms of merging connected components or solving dynamic connectivity problems. |