Integers With Multiple Sum of Two Cubes - Solution & Explanation
Problem Statement
You are given an integer n.
An integer x is considered good if there exist at least two distinct pairs (a, b) such that:
aandbare positive integers.a <= bx = a3 + b3
Return an array containing all good integers less than or equal to n, sorted in ascending order.
Example 1:
Input: n = 4104
Output: [1729,4104]
Explanation:
Among integers less than or equal to 4104, the good integers are:
- 1729:
13 + 123 = 1729and93 + 103 = 1729. - 4104:
23 + 163 = 4104and93 + 153 = 4104.
Thus, the answer is [1729, 4104].
Example 2:
Input: n = 578
Output: []
Explanation:
There are no good integers less than or equal to 578, so the answer is an empty array.
Constraints:
1 <= n <= 109
Approach Overview
Problem Overview: Given a bound on integers, identify numbers that can be written as a^3 + b^3 in more than one distinct way. Different pairs (a, b) may produce the same cube sum, and the task is to detect sums that appear multiple times.
Approach 1: Brute Force Pair Enumeration (O(n^2) time, O(1) extra space)
Enumerate every pair (a, b) where 1 ≤ a ≤ b ≤ n. For each pair compute a^3 + b^3 and compare it against previously generated sums by scanning stored results. This approach relies purely on enumeration and repeated comparisons, which makes it simple but inefficient. Time complexity grows to O(n^3) if comparisons require scanning previous results, and even optimized versions still need O(n^2) enumeration. Use this only to demonstrate the mathematical property of cube sums.
Approach 2: Hash Table Counting (O(n^2) time, O(n^2) space)
Enumerate all cube pairs but store the computed sum in a hash table. Each key represents a value of a^3 + b^3, and the value stores how many pairs generate it. When the same sum appears again, increment the counter. After enumeration, every key with count ≥ 2 represents an integer that has multiple cube representations. Hash lookup is O(1), so the algorithm remains dominated by the n^2 pair enumeration. This approach is straightforward and commonly used in interview solutions.
Approach 3: Sort Cube Sums and Count Duplicates (O(n^2 log n) time, O(n^2) space)
Instead of hashing, store all cube sums in an array while enumerating pairs. Sort the array using a standard sorting algorithm. Identical sums become adjacent after sorting, allowing a single pass to count duplicates and detect numbers that appear multiple times. Sorting introduces a log n factor but can be useful when deterministic ordering or offline processing is preferred over hashing.
Approach 4: Ordered Pair Enumeration with Min Heap (O(n^2 log n) time, O(n) space)
A more memory‑efficient technique generates sums in sorted order using a min heap. Start with pairs (a, a) for each a. Push their cube sums into a heap. Each time you pop the smallest sum, push the next pair for that a (i.e., (a, b+1)). This technique resembles merging sorted sequences and is common in enumeration problems. Duplicate sums appear consecutively, making it easy to detect integers with multiple representations. The approach relies on priority queue operations and controlled enumeration rather than storing every pair.
Recommended for interviews: The hash table counting approach is the most practical. It clearly shows you understand pair enumeration and how hash tables eliminate repeated searches. Mentioning brute force first demonstrates reasoning about the search space, while the optimized hash-based solution shows the ability to reduce lookup cost and handle duplicate detection efficiently.
Solution
We observe that when a or b is greater than 1000, the expression a^3 + b^3 > 10^9. Therefore, we only need to enumerate 1 leq a leq b leq 1000 and count the occurrences of each integer x = a^3 + b^3. Finally, we filter out the integers that appear more than once and sort them in ascending order to obtain all good integers.
We preprocess all good integers and store them in an array GOOD. For each query, we use binary search to find the index idx of the first integer in GOOD that is greater than n, then return the first idx integers in GOOD.
The time complexity is O(m^2 + k log k), where m = 1000 is the enumeration range and k is the number of good integers. The space complexity is O(k).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Enumeration | O(n^2) to O(n^3) | O(1) | Conceptual baseline or very small constraints |
| Hash Table Counting | O(n^2) | O(n^2) | General solution; fastest lookup for duplicate cube sums |
| Sort Cube Sums | O(n^2 log n) | O(n^2) | When deterministic ordering or post‑processing of sums is required |
| Min Heap Enumeration | O(n^2 log n) | O(n) | When memory is constrained but sorted enumeration is needed |
Video Solution
LeetCode Problem 3890 | Integers With Multiple Sum of Two Cubes • Repovive TV • 182 views views
Watch 7 more video solutions →Frequently Asked Questions
Is Integers With Multiple Sum of Two Cubes easy or hard?
Integers With Multiple Sum of Two Cubes Python/Java solution
How to solve Integers With Multiple Sum of Two Cubes in O(n^2)?
What is the best approach for Integers With Multiple Sum of Two Cubes?
Is Integers With Multiple Sum of Two Cubes asked at Google/Amazon/Meta?
What data structure is used in Integers With Multiple Sum of Two Cubes?
What is the time complexity of Integers With Multiple Sum of Two Cubes?
Ready to solve this problem?
Practice Integers With Multiple Sum of Two Cubes with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor