Skip to main content

Smallest Missing Multiple of K - Solution & Explanation

EasyArrayHash Table5 min read
Practice this problem

Problem Statement

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 <= 100
  • 1 <= nums[i] <= 100
  • 1 <= k <= 100

Approach Overview

Problem Overview: You are given an integer array and a value k. The task is to find the smallest positive multiple of k that does not appear in the array. In other words, check multiples k, 2k, 3k... and return the first one missing from the input.

Approach 1: Brute Force Enumeration (O(n^2) time, O(1) space)

Start from the first multiple k and repeatedly check if that value exists in the array. Each check requires scanning the entire array. If k exists, move to 2k, then 3k, and continue until a multiple is not found. The nested work comes from enumerating multiples while performing a full array search for each candidate. This approach works for very small inputs but becomes slow as the array grows because every membership check costs O(n).

Approach 2: Hash Table + Enumeration (O(n) time, O(n) space)

Store all numbers from the array in a hash set. This enables constant-time membership checks. After building the set, enumerate multiples of k starting from k. For each multiple, perform a hash lookup. The first multiple not present in the set is the answer. If the array contains k, 2k, 3k ... mk, the algorithm returns (m + 1) * k. The key insight is replacing repeated linear scans with O(1) lookups using a hash structure.

This technique is common when solving presence or frequency problems in Array tasks. A hash-based structure dramatically reduces lookup cost compared to repeated scanning. Problems involving membership queries or duplicate detection typically benefit from a Hash Table.

Recommended for interviews: The hash table + enumeration solution is the expected approach. Interviewers want to see that you recognize repeated lookups and replace them with constant-time hash checks. Mentioning the brute force approach first shows problem understanding, but implementing the hash-based solution demonstrates practical optimization skills.

Solution

We first use a hash table s to store the numbers that appear in the array nums. Then, starting from the first positive multiple of k, which is k times 1, we enumerate each positive multiple in sequence until we find the first multiple that does not appear in the hash table s, which is the answer.

The time complexity is O(n) and the space complexity is O(n), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^2)O(1)Small inputs or quick conceptual explanation of the problem
Hash Table + EnumerationO(n)O(n)General case. Fast membership checks using a hash set

Video Solution

3718. Smallest Missing Multiple of K (Leetcode Easy) • Programming Live with Larry • 472 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Smallest Missing Multiple of K easy or hard?
Smallest Missing Multiple of K is categorized as an Easy problem. The main idea is recognizing that repeated membership checks should use a hash set instead of scanning the array each time.
Smallest Missing Multiple of K Python/Java solution
In Python, use a set to store the array elements and repeatedly check multiples of k. In Java, use a HashSet<Integer> for constant-time lookups. The same logic works in C++, Go, and TypeScript using their respective hash-based set structures.
How to solve Smallest Missing Multiple of K in O(n)?
Insert every number from the array into a hash set. Start with candidate = k and repeatedly check if the candidate exists in the set. If it exists, increase the candidate by k and continue. The first candidate not found in the set is the smallest missing multiple.
What is the best approach for Smallest Missing Multiple of K?
The most efficient approach uses a hash table (set) combined with enumeration. Insert all array elements into a set, then check multiples of k starting from k. The first multiple not found in the set is the answer. This reduces lookup time to O(1) and results in overall O(n) time complexity.
Is Smallest Missing Multiple of K asked at Google/Amazon/Meta?
Problems involving missing numbers, hash set lookups, and array membership checks appear frequently in interviews at companies like Amazon, Google, and Meta. While the exact problem may vary, the underlying hash table pattern is a common interview topic.
What data structure is used in Smallest Missing Multiple of K?
A hash table (typically implemented as a hash set) is the primary data structure. It allows constant-time membership checks when verifying whether a multiple of k already exists in the array.
What is the time complexity of Smallest Missing Multiple of K?
The optimal solution runs in O(n) time. Building the hash set requires O(n), and checking multiples of k continues until the first missing value is found. In the worst case, if the array contains the first n multiples of k, the enumeration also runs up to O(n).

Ready to solve this problem?

Practice Smallest Missing Multiple of K with our built-in code editor and test cases.

Practice on FleetCode