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.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers.
Return true if it is possible. Otherwise, return false.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= k <= nums.length <= 1051 <= nums[i] <= 109Problem summary: Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers. Return true if it is possible. Otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[1,2,3,3,4,4,5,6] 4
[3,2,1,2,3,4,3,4,5,9,10,11] 3
[1,2,3,4] 3
split-array-into-consecutive-subsequences)all-divisions-with-the-highest-score-of-a-binary-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1296: Divide Array in Sets of K Consecutive Numbers
class Solution {
public boolean isPossibleDivide(int[] nums, int k) {
if (nums.length % k != 0) {
return false;
}
Arrays.sort(nums);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
for (int x : nums) {
if (cnt.getOrDefault(x, 0) > 0) {
for (int y = x; y < x + k; ++y) {
if (cnt.merge(y, -1, Integer::sum) < 0) {
return false;
}
}
}
}
return true;
}
}
// Accepted solution for LeetCode #1296: Divide Array in Sets of K Consecutive Numbers
func isPossibleDivide(nums []int, k int) bool {
if len(nums)%k != 0 {
return false
}
sort.Ints(nums)
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
for _, x := range nums {
if cnt[x] > 0 {
for y := x; y < x+k; y++ {
if cnt[y] == 0 {
return false
}
cnt[y]--
}
}
}
return true
}
# Accepted solution for LeetCode #1296: Divide Array in Sets of K Consecutive Numbers
class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k:
return False
cnt = Counter(nums)
for x in sorted(nums):
if cnt[x]:
for y in range(x, x + k):
if cnt[y] == 0:
return False
cnt[y] -= 1
return True
// Accepted solution for LeetCode #1296: Divide Array in Sets of K Consecutive Numbers
struct Solution;
use std::collections::BTreeMap;
use std::collections::VecDeque;
impl Solution {
fn is_possible_divide(nums: Vec<i32>, k: i32) -> bool {
let n = nums.len();
let k = k as usize;
if n % k != 0 {
return false;
}
let mut btm: BTreeMap<i32, usize> = BTreeMap::new();
for x in nums {
*btm.entry(x).or_default() += 1;
}
let mut queue: VecDeque<(i32, usize)> = VecDeque::new();
for (val, size) in btm {
queue.push_back((val, size));
if queue.len() == k {
let (first_val, first_size) = queue.pop_front().unwrap();
for i in 1..k {
let (next_val, next_size) = queue.pop_front().unwrap();
if next_val != first_val + i as i32 {
return false;
}
if next_size < first_size {
return false;
}
if next_size > first_size {
queue.push_back((next_val, next_size - first_size));
}
}
}
}
queue.is_empty()
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3, 3, 4, 4, 5, 6];
let k = 4;
let res = true;
assert_eq!(Solution::is_possible_divide(nums, k), res);
let nums = vec![3, 2, 1, 2, 3, 4, 3, 4, 5, 9, 10, 11];
let k = 3;
let res = true;
assert_eq!(Solution::is_possible_divide(nums, k), res);
let nums = vec![3, 3, 2, 2, 1, 1];
let k = 3;
let res = true;
assert_eq!(Solution::is_possible_divide(nums, k), res);
let nums = vec![1, 2, 3, 4];
let k = 3;
let res = false;
assert_eq!(Solution::is_possible_divide(nums, k), res);
}
// Accepted solution for LeetCode #1296: Divide Array in Sets of K Consecutive Numbers
function isPossibleDivide(nums: number[], k: number): boolean {
if (nums.length % k !== 0) {
return false;
}
const cnt = new Map<number, number>();
for (const x of nums) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
nums.sort((a, b) => a - b);
for (const x of nums) {
if (cnt.get(x)! > 0) {
for (let y = x; y < x + k; y++) {
if ((cnt.get(y) || 0) === 0) {
return false;
}
cnt.set(y, cnt.get(y)! - 1);
}
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.