Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other.
Constraints:
1 <= nums.length <= 1050 <= k <= nums.lengthnums[i] is 0 or 1Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,0,0,0,1,0,0,1] 2
[1,0,0,1,0,1] 2
task-scheduler-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1437: Check If All 1's Are at Least Length K Places Away
class Solution {
public boolean kLengthApart(int[] nums, int k) {
int j = -(k + 1);
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 1) {
if (i - j - 1 < k) {
return false;
}
j = i;
}
}
return true;
}
}
// Accepted solution for LeetCode #1437: Check If All 1's Are at Least Length K Places Away
func kLengthApart(nums []int, k int) bool {
j := -(k + 1)
for i, x := range nums {
if x == 1 {
if i-j-1 < k {
return false
}
j = i
}
}
return true
}
# Accepted solution for LeetCode #1437: Check If All 1's Are at Least Length K Places Away
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
j = -inf
for i, x in enumerate(nums):
if x:
if i - j - 1 < k:
return False
j = i
return True
// Accepted solution for LeetCode #1437: Check If All 1's Are at Least Length K Places Away
impl Solution {
pub fn k_length_apart(nums: Vec<i32>, k: i32) -> bool {
let mut j = -(k + 1);
for (i, &x) in nums.iter().enumerate() {
if x == 1 {
if (i as i32) - j - 1 < k {
return false;
}
j = i as i32;
}
}
true
}
}
// Accepted solution for LeetCode #1437: Check If All 1's Are at Least Length K Places Away
function kLengthApart(nums: number[], k: number): boolean {
let j = -(k + 1);
for (let i = 0; i < nums.length; ++i) {
if (nums[i] === 1) {
if (i - j - 1 < k) {
return false;
}
j = i;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.