[LeetCode] Integers With Multiple Sum of Two Cubes

3890. Integers With Multiple Sum of Two Cubes

You are given an integer n.

An integer x is considered good if there exist at least two distinct pairs (a, b) such that:

  • a and b are positive integers.
  • a <= b
  • x = a^3 + b^3

Return an array containing all good integers less than or equal to n, sorted in ascending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> findGoodIntegers(int n) {
unordered_map<int,int> freq;
vector<int> res;
for(long long i = 1; 2 * i * i * i <= n; i++) {
for(long long j = i; i * i * i + j * j * j <= n; j++) {
if(++freq[i * i * i + j * j * j] == 2) res.push_back(i * i * i + j * j * j);
}
}
sort(begin(res), end(res));
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/integers-with-multiple-sum-of-two-cubes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.