#3718
Smallest Missing Multiple of K
EasyArrayHash TableHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(n) | O(n) |
💡
Intuition
Time O(n)Space O(n)
Utilize a hash set for quick lookups and iterate through multiples of k until we find the missing one.
⚙️
Algorithm
3 steps- 1Step 1: Create a set from nums for O(1) lookups.
- 2Step 2: Initialize a variable multiple to k.
- 3Step 3: Check if multiple is in the set; if not, return it. If yes, increment by k and repeat.
solution.py6 lines
1def smallest_missing_multiple(nums, k):
2 num_set = set(nums)
3 multiple = k
4 while multiple not in num_set:
5 return multiple
6 multiple += kℹ
Complexity note: The algorithm runs in O(n) due to the single pass through nums to create the set and the linear check for multiples.
- 1Using a hash set allows for O(1) lookups.
- 2The smallest missing multiple can be found by iterating through multiples of k.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.