Practice Check If All 1's Are at Least Length K Places Away
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Given an binary array nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.
Source: Check If All 1's Are at Least Length K Places Away on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [1,0,0,0,1,0,0,1], k = 2
true
The 1s occur at zero-based indices 0, 4 and 7. There are 4 - 0 - 1 = 3 zeros between the first pair and 7 - 4 - 1 = 2 between the second pair. Both gaps meet k = 2, so the answer is true. Testing consecutive 1s is sufficient, because any nonconsecutive pair is even farther apart.
# Example 2
nums = [1,0,0,1,0,1], k = 2
false
The 1s are at indices 0, 3 and 5. The first gap contains two zeros and is valid, but the second contains only 5 - 3 - 1 = 1 zero. That violates k = 2, so return false. The index difference is two in that second pair, but one position is occupied by an endpoint; the required zero count is the difference minus one.
Constraints
How to solve Check If All 1's Are at Least Length K Places Away
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
Only consecutive ones can be the closest pair, so a single left-to-right pass that remembers the index of the most recent one and checks the number of zeros between it and the next one, which is the index difference minus one, decides the answer without any pairwise work.
Why brute force is not enough
Compare every pair of ones and check their separation, which is O(n^2) when the array is dense with ones; with nums.length up to 10^5 that is on the order of 10^10 comparisons.
Approach
- Keep the index of the last one seen, initialized to a sentinel meaning none yet.
- Scan the array. On encountering a one, if a previous one exists, compute the gap as the current index minus the previous index minus one, which is exactly the number of zeros strictly between them.
- If that gap is smaller than k, return false immediately; otherwise record the current index as the last one seen. The invariant is that all pairs of ones seen so far are at least k apart.
- If the scan finishes, return true; arrays with zero or one ones trivially satisfy the condition.
Walkthrough of Example 2
nums = [1,0,0,1,0,1], k = 2 → false
- Index 0 holds a one and nothing precedes it, so record 0 as the last one.
- Indices 1 and 2 are zeros and are skipped.
- Index 3 holds a one; the gap is 3 minus 0 minus 1, which is 2, not less than k equals 2, so it passes and 3 becomes the last one.
- Index 4 is a zero.
- Index 5 holds a one; the gap is 5 minus 3 minus 1, which is 1, less than 2, so return false, matching the expected output.
Time and space
Time O(n) · Space O(1). n is the array length; each element is inspected once and only the last index is stored.
Interview pitfalls
- Comparing the raw index difference to k instead of the difference minus one; ones at indices 3 and 5 have one zero between them, not two.
- Initializing the last index to 0 or -1 and then treating the first one as if it had a predecessor, which can produce a false negative at the start.
- Forgetting that k equals 0 should always return true, and that an array with a single one or no ones is valid for any k.
- Counting zeros with a running counter but failing to reset it after each one, or resetting it before the comparison.
Follow-up questions interviewers ask
- How would you find the largest k for which the array is valid, in one pass?
- If the array were circular so that the last and first ones are also neighbors, what extra check is needed?
Related LeetCode Problems
Practice more problems similar to Check If All 1's Are at Least Length K Places Away.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.