|
| 1 | +# Powerful Integers |
| 2 | + |
| 3 | +Given three integers x, y, and bound, return a list of all the powerful integers that have a value less than or equal to |
| 4 | +bound. |
| 5 | + |
| 6 | +An integer is powerful if it can be represented as xi + yj for some integers i >= 0 and j >= 0. |
| 7 | + |
| 8 | +You may return the answer in any order. In your answer, each value should occur at most once. |
| 9 | + |
| 10 | +## Examples |
| 11 | + |
| 12 | +Example 1: |
| 13 | + |
| 14 | +```text |
| 15 | +Input: x = 2, y = 3, bound = 10 |
| 16 | +Output: [2,3,4,5,7,9,10] |
| 17 | +Explanation: |
| 18 | +2 = 20 + 30 |
| 19 | +3 = 21 + 30 |
| 20 | +4 = 20 + 31 |
| 21 | +5 = 21 + 31 |
| 22 | +7 = 22 + 31 |
| 23 | +9 = 23 + 30 |
| 24 | +10 = 20 + 32 |
| 25 | +``` |
| 26 | + |
| 27 | +Example 2: |
| 28 | +```text |
| 29 | +Input: x = 3, y = 5, bound = 15 |
| 30 | +Output: [2,4,6,8,10,14] |
| 31 | +``` |
| 32 | + |
| 33 | +## Constraints |
| 34 | + |
| 35 | +- 1 <= x, y <= 100 |
| 36 | +- 0 <= bound <= 10^6 |
| 37 | + |
| 38 | +## Topics |
| 39 | + |
| 40 | +- Hash Table |
| 41 | +- Math |
| 42 | +- Enumeration |
| 43 | + |
| 44 | +## Solution(s) |
| 45 | + |
| 46 | +1. [Logarithmic Bounds](#logarithmic-bounds) |
| 47 | +2. [Logarithmic Bound 2](#logarithmic-bound-2) |
| 48 | + |
| 49 | +### Logarithmic Bounds |
| 50 | + |
| 51 | +Our approach here will only focus on finding the bounds for numbers x and y. One way to get the bounds on the powers is |
| 52 | +to have nested loops that iterate from [0⋯bound]. However, this is very inefficient because the bound can be an extremely |
| 53 | +large value and a nested-loop over this bound will take forever to finish. Also, we don't need to iterate over all of the |
| 54 | +values and combinations. There is a way to find a much smaller bound for the powers. |
| 55 | + |
| 56 | +m^n <= bound |
| 57 | + |
| 58 | +This formula implies that |
| 59 | + |
| 60 | +n<=log(m) bound |
| 61 | + |
| 62 | +> We can use the log function to determine the bounds for the powers of "x" and "y". |
| 63 | +
|
| 64 | +#### Algorithm |
| 65 | + |
| 66 | +1. Let's define `a` as the power bound for the number `x`. Thus `a=log(x)bound`. |
| 67 | +2. Similarly, let's define `b` as the power bound for the number `y`. Thus `b=log(y)bound`. |
| 68 | +3. Now we will have our nested for-loop structure where the outer loop will iterate from [0⋯a] and the inner loop will |
| 69 | + iterate from [0⋯b]. |
| 70 | +4. We will use a set to store our results. This is because we might generate the same value multiple times. E.g. |
| 71 | + `2^1 + 3^2 = 11` and `2^3 + 3^1 = 11`. We only need to include the value 11 once and hence, we will use a set called |
| 72 | + `resultSet` to store our answers. |
| 73 | +5. At each step, we calculate `x^a + y^b` and check if this value is less than or equal to bound. If it is, then this is |
| 74 | + a powerful integer and we add it to our set of answers. |
| 75 | +6. We need special break conditions to handle the scenario when x or y is 1. This is because if the number x or y is 1, |
| 76 | + then their power-bound will be equal to bound itself. Also, it doesn't matter what their power-bound is because 1^N |
| 77 | + is always 1. Thus, when the number is 1, we don't need to loop from [0⋯N] and we can break early. |
| 78 | +7. Finally, convert the set to a list and return. |
| 79 | + |
| 80 | +#### Time Complexity |
| 81 | + |
| 82 | +Time Complexity: Let `N` be `log(x)bound` and `M` be `log(y)bound`. Then the overall time complexity is `O(N×M)` because |
| 83 | +we used a nested loop structure to calculate all of the powerful integers. |
| 84 | + |
| 85 | +#### Space Complexity |
| 86 | + |
| 87 | +`O(N×M)` because we use a set to omit duplicates. We could just use our result list to check membership before adding |
| 88 | +values. However, that would be costly in terms of time complexity because it would require a full scan of the result list |
| 89 | +to see if the value already exists. |
| 90 | + |
| 91 | +### Logarithmic Bound 2 |
| 92 | + |
| 93 | +The key insight is that since x^i and y^j grow exponentially, the number of distinct powers of x and y that remain within |
| 94 | +bound is at most O(log(bound) each. We can enumerate all pairs (i,j) by iterating through powers of x in an outer loop |
| 95 | +and powers of y in an inner loop, adding each sum x^i + y^j that is less than or equal to bound into a hash set called |
| 96 | +`resultSet`. The set automatically handles deduplication, ensuring each powerful integer appears at most once. A special |
| 97 | +case arises when x or y equals 1, because 1^i is always 1 regardless of the exponent, which would cause an infinite loop. |
| 98 | +We handle this by breaking out of the respective loop after the first iteration when x or y is 1. |
| 99 | + |
| 100 | +Solution steps: |
| 101 | + |
| 102 | +1. Initialize an empty set resultSet to store unique powerful integers. |
| 103 | +2. Initialize `powX` to 1, representing x^0 |
| 104 | +3. Start an outer while loop that continues as long as `powX ≤ bound`. |
| 105 | + - Inside the outer loop, initialize `powY` to 1, representing y^0 |
| 106 | + - Start an inner while loop that continues as long as `powX + powY ≤ bound`. |
| 107 | + - Add the value `powX` + `powY` to `resultSet`. |
| 108 | + - If y equals 1, break out of the inner loop immediately, since y^j will always be 1 and further iterations would |
| 109 | + not produce new values. |
| 110 | + - Otherwise, multiply `powY` by y to advance to the next power of y. |
| 111 | + - After the inner loop, if x equals 1, break out of the outer loop immediately, since x^i will always be 1 and further |
| 112 | + iterations would not produce new values. |
| 113 | + - Otherwise, multiply `powX` by x to advance to the next power of x. |
| 114 | +4. Convert resultSet to a list and return it. |
| 115 | + |
| 116 | +#### Time Complexity |
| 117 | + |
| 118 | +The time complexity of the solution is O(logx(bound)) * logy(bound) because the outer loop runs at most O(logx(bound)) |
| 119 | +times and the inner loop runs at most O(logy(bound)) times for each iteration of the outer loop. Since bound ≤10^6 and |
| 120 | +the minimum base greater than 1 is 2, each loop runs at most about log2(10^6)≈20 iterations, making this very efficient. |
| 121 | +When x or y is 1, the corresponding loop runs only once. |
| 122 | + |
| 123 | +#### Space Complexity |
| 124 | + |
| 125 | +The space complexity is `O(logx(bound)) * logy(bound)` because in the worst case, every combination of powers produces |
| 126 | +a unique sum, and all of these are stored in the resultSet. This is bounded by the total number of pairs enumerated, |
| 127 | +which is at most `O(logx(bound)) * logy(bound)`. |
0 commit comments