Check If All 1's Are at Least Length K Places Away (LeetCode 1437): Solution and Explanation
Decide whether every two 1s in a binary array have at least k zeros between them.
Intuition
The condition talks about every pair of 1s, but most pairs do not need checking. If two 1s are neighbors, any 1 further left is even further away.
So the closest pair is always a pair of consecutive 1s. Checking each 1 against the 1 just before it covers every pair.
That needs only one remembered index: where the last 1 was.
Approach: Track the previous 1
- Set prev = -1, meaning no 1 has been seen.
- Scan the array. Skip zeros.
- At a 1 with index i, if prev is not -1 and i - prev - 1 < k, return false: there are too few zeros between them.
- Otherwise set prev = i and continue. If the scan finishes, return true.
Step-by-step example
nums = [1,0,0,0,1,0,0,1], k = 2
- Index 0 is the first 1. There is nothing to compare, so prev = 0.
- Indices 1 to 3 are zeros.
- Index 4 is a 1. The gap is 4 - 0 - 1 = 3, at least 2. prev = 4.
- Indices 5 and 6 are zeros.
- Index 7 is a 1. The gap is 7 - 4 - 1 = 2, at least 2. prev = 7.
- The scan ends without a short gap, so return true.
Code: Python, Java, C++
1class Solution:
2 def kLengthApart(self, nums: List[int], k: int) -> bool:
3 prev = -1
4 for i, x in enumerate(nums):
5 if x == 1:
6 if prev != -1 and i - prev - 1 < k:
7 return False
8 prev = i
9 return True1class Solution {
2 public boolean kLengthApart(int[] nums, int k) {
3 int prev = -1;
4 for (int i = 0; i < nums.length; i++) {
5 if (nums[i] == 1) {
6 if (prev != -1 && i - prev - 1 < k) {
7 return false;
8 }
9 prev = i;
10 }
11 }
12 return true;
13 }
14}1class Solution {
2public:
3 bool kLengthApart(vector<int>& nums, int k) {
4 int prev = -1;
5 for (int i = 0; i < (int)nums.size(); i++) {
6 if (nums[i] == 1) {
7 if (prev != -1 && i - prev - 1 < k) {
8 return false;
9 }
10 prev = i;
11 }
12 }
13 return true;
14 }
15};Time and space complexity
O(n)
One pass over the array with constant work per element.
O(1)
Only the index of the previous 1 is stored.
Other approaches
Count zeros since the last 1 (time O(n), space O(1))
Keep a counter that resets at every 1 and grows at every 0. At each 1 after the first, fail if the counter is below k. Same idea with a counter instead of an index.
Compare all pairs of 1s (time O(n^2), space O(n))
Collect the positions of all 1s and check every pair. Correct but quadratic in the number of 1s, and unnecessary because only neighbors matter.
Common mistakes
- Measuring the distance as i - prev instead of i - prev - 1. The question counts the zeros between the 1s, not the index difference.
- Treating the first 1 as if it had a 1 before it at index -1 or 0, which makes leading 1s fail wrongly.
- Forgetting that k can be 0, in which case adjacent 1s are allowed and the answer is always true.
Frequently asked questions
Why is comparing neighbors enough?
For 1s at positions a < b < c, the distance from a to c is larger than from b to c. If every neighboring pair is far enough apart, every other pair is further apart still.
What if the array has no 1s or only one?
Then there is no pair to check and the answer is true. With prev starting at -1, the code never compares and returns true.
Can the scan stop early?
Yes. The first gap that is too short proves the answer is false, so the function returns immediately.