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 integer array nums and an integer k, return the smallest positive multiple of k that is missing from nums.
A multiple of k is any positive integer divisible by k.
Example 1:
Input: nums = [8,2,3,4,6], k = 2
Output: 10
Explanation:
The multiples of k = 2 are 2, 4, 6, 8, 10, 12... and the smallest multiple missing from nums is 10.
Example 2:
Input: nums = [1,4,7,10,15], k = 5
Output: 5
Explanation:
The multiples of k = 5 are 5, 10, 15, 20... and the smallest multiple missing from nums is 5.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1001 <= k <= 100Problem summary: Given an integer array nums and an integer k, return the smallest positive multiple of k that is missing from nums. A multiple of k is any positive integer divisible by k.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[8,2,3,4,6] 2
[1,4,7,10,15] 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3718: Smallest Missing Multiple of K
class Solution {
public int missingMultiple(int[] nums, int k) {
boolean[] s = new boolean[101];
for (int x : nums) {
s[x] = true;
}
for (int i = 1;; ++i) {
int x = k * i;
if (x >= s.length || !s[x]) {
return x;
}
}
}
}
// Accepted solution for LeetCode #3718: Smallest Missing Multiple of K
func missingMultiple(nums []int, k int) int {
s := map[int]bool{}
for _, x := range nums {
s[x] = true
}
for i := 1; ; i++ {
if x := k * i; !s[x] {
return x
}
}
}
# Accepted solution for LeetCode #3718: Smallest Missing Multiple of K
class Solution:
def missingMultiple(self, nums: List[int], k: int) -> int:
s = set(nums)
for i in count(1):
x = k * i
if x not in s:
return x
// Accepted solution for LeetCode #3718: Smallest Missing Multiple of K
fn missing_multiple(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashSet;
let s: HashSet<_> = nums.into_iter().collect();
let mut v = 1;
loop {
let n = v * k;
if !s.contains(&n) {
return n;
}
v += 1;
}
}
fn main() {
let nums = vec![1, 4, 7, 10, 15];
let ret = missing_multiple(nums, 5);
println!("ret={ret}");
}
#[test]
fn test() {
{
let nums = vec![8, 2, 3, 4, 6];
let ret = missing_multiple(nums, 2);
assert_eq!(ret, 10);
}
{
let nums = vec![1, 4, 7, 10, 15];
let ret = missing_multiple(nums, 5);
assert_eq!(ret, 5);
}
}
// Accepted solution for LeetCode #3718: Smallest Missing Multiple of K
function missingMultiple(nums: number[], k: number): number {
const s = new Set<number>(nums);
for (let i = 1; ; ++i) {
const x = k * i;
if (!s.has(x)) {
return x;
}
}
}
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.
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.