# Question

Formatted question description: https://leetcode.ca/all/970.html

Given two positive integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.

Return a list of all powerful integers that have value less than or equal to bound.

You may return the answer in any order.  In your answer, each value should occur at most once.

Example 1:

Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2
Example 2:

Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]
Note:

1 <= x <= 100
1 <= y <= 100
0 <= bound <= 10^6



# Algorithm

This question defines a powerful integer, which is the sum of the i power and j power of a given integer x and y respectively. Now an integer bound is given, so that all powerful integers that do not exceed this range are returned.

Since it’s an Easy topic, don’t think about too many techniques, just crack it without thinking. The initial solution of the blogger is to generate x and y exponent arrays in the bound range, namely x^0, x^1, x^2… and y^0, y^1, y^2. …, and then take any number from each of the two arrays and add them together. As long as it does not exceed bound, it can be put into the result res. It should be noted that if x and y are equal to 1, then you will fall into death Loop, because multiplying by 1 is always equal to itself, so additional judgments must be added.

The method can actually be optimized, there is no need to use an additional array to save, but can be processed directly in the for loop. Also, in order to prevent duplicate numbers, first store the results in a TreeSet, take advantage of the feature that can remove duplicates, and finally turn back to the array. See the code as follows:

# Code

C++

class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
set<int> res;
for (int a = 1; a < bound; a *= x) {
for (int b = 1; a + b <= bound; b *= y) {
res.insert(a + b);
if (y == 1) break;
}
if (x == 1) break;
}
return vector<int>(res.begin(), res.end());
}
};



Java

• class Solution {
public List<Integer> powerfulIntegers(int x, int y, int bound) {
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
int xNum = 1, yNum = 1;
if (x == 1)
else {
while (xNum <= bound) {
xNum *= x;
}
}
if (y == 1)
else {
while (yNum <= bound) {
yNum *= y;
}
}
List<Integer> powerfulIntegers = new ArrayList<Integer>();
int xSize = xList.size(), ySize = yList.size();
for (int i = 0; i < xSize; i++) {
int numX = xList.get(i);
for (int j = 0; j < ySize; j++) {
int numY = yList.get(j);
int sum = numX + numY;
if (sum > bound)
break;
if (!powerfulIntegers.contains(sum))
}
}
Collections.sort(powerfulIntegers);
return powerfulIntegers;
}
}

• // OJ: https://leetcode.com/problems/powerful-integers/
// Time: O(log_x^bound * log_y^bound)
// Space: O(log_x^bound * log_y^bound)
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
unordered_set<int> s;
for (int a = 1; a + 1 <= bound; a = x == 1 ? bound : a * x)
for (int b = 1; a + b <= bound; b = y == 1 ? bound : b * y)
s.insert(a + b);
return vector<int>(s.begin(), s.end());
}
};

• class Solution(object):
def powerfulIntegers(self, x, y, bound):
"""
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
"""
res = set()
i = 0
while x ** i <= bound and i <= bound:
j = 0
while j <= bound:
target = x ** i + y ** j
if target > bound:
break