|
| 1 | +--- |
| 2 | +id: powerful-integers |
| 3 | +title: Powerful Integers |
| 4 | +sidebar_label: 970-Powerful Integers |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - LeetCode |
| 8 | + - Java |
| 9 | + - Python |
| 10 | + - C++ |
| 11 | +description: "This is a solution to the Powerful Integers problem on LeetCode." |
| 12 | +sidebar_position: 12 |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +Given three integers `x`, `y`, and `bound`, return a list of all the powerful integers that have a value less than or equal to `bound`. |
| 18 | + |
| 19 | +An integer is powerful if it can be represented as `x^i + y^j` for some integers `i >= 0` and `j >= 0`. |
| 20 | + |
| 21 | +You may return the answer in any order. In your answer, each value should occur at most once. |
| 22 | + |
| 23 | +### Examples |
| 24 | + |
| 25 | +**Example 1:** |
| 26 | + |
| 27 | +``` |
| 28 | +Input: x = 2, y = 3, bound = 10 |
| 29 | +Output: [2,3,4,5,7,9,10] |
| 30 | +Explanation: |
| 31 | +2 = 2^0 + 3^0 |
| 32 | +3 = 2^1 + 3^0 |
| 33 | +4 = 2^0 + 3^1 |
| 34 | +5 = 2^1 + 3^1 |
| 35 | +7 = 2^2 + 3^1 |
| 36 | +9 = 2^3 + 3^0 |
| 37 | +10 = 2^0 + 3^2 |
| 38 | +``` |
| 39 | + |
| 40 | +**Example 2:** |
| 41 | + |
| 42 | +``` |
| 43 | +Input: x = 3, y = 5, bound = 15 |
| 44 | +Output: [2,4,6,8,10,14] |
| 45 | +``` |
| 46 | + |
| 47 | +### Constraints |
| 48 | + |
| 49 | +- `1 <= x, y <= 100` |
| 50 | +- `0 <= bound <= 10^6` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Solution for Powerful Integers Problem |
| 55 | + |
| 56 | +To solve this problem, we need to find all unique integers that can be represented as `x^i + y^j` and are less than or equal to `bound`. We iterate over possible values of `i` and `j` while ensuring the results stay within the bounds. |
| 57 | + |
| 58 | +### Approach |
| 59 | + |
| 60 | +1. **Iterate Over Powers:** |
| 61 | + - For `x`, calculate powers `x^i` starting from `i = 0` and stopping when `x^i > bound`. |
| 62 | + - For each `x^i`, calculate powers `y^j` starting from `j = 0` and stopping when `y^j > bound`. |
| 63 | + - If `x^i + y^j <= bound`, add it to the result set to ensure uniqueness. |
| 64 | + |
| 65 | +2. **Handling Edge Cases:** |
| 66 | + - If `x == 1`, then `x^i` will always be `1` for all `i`, so the loop should run only once for `i = 0`. |
| 67 | + - Similarly, if `y == 1`, then `y^j` will always be `1`. |
| 68 | + |
| 69 | +### Code in Different Languages |
| 70 | + |
| 71 | +<Tabs> |
| 72 | +<TabItem value="C++" label="C++" default> |
| 73 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 74 | + |
| 75 | +```cpp |
| 76 | +class Solution { |
| 77 | +public: |
| 78 | + vector<int> powerfulIntegers(int x, int y, int bound) { |
| 79 | + set<int> resultSet; |
| 80 | + for (int i = 1; i < bound; i = (x == 1) ? bound : i * x) { |
| 81 | + for (int j = 1; i + j <= bound; j = (y == 1) ? bound : j * y) { |
| 82 | + resultSet.insert(i + j); |
| 83 | + } |
| 84 | + } |
| 85 | + return vector<int>(resultSet.begin(), resultSet.end()); |
| 86 | + } |
| 87 | +}; |
| 88 | +``` |
| 89 | +
|
| 90 | +</TabItem> |
| 91 | +<TabItem value="Java" label="Java"> |
| 92 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 93 | +
|
| 94 | +```java |
| 95 | +class Solution { |
| 96 | + public List<Integer> powerfulIntegers(int x, int y, int bound) { |
| 97 | + Set<Integer> resultSet = new HashSet<>(); |
| 98 | + for (int i = 1; i < bound; i = (x == 1) ? bound + 1 : i * x) { |
| 99 | + for (int j = 1; i + j <= bound; j = (y == 1) ? bound + 1 : j * y) { |
| 100 | + resultSet.add(i + j); |
| 101 | + } |
| 102 | + } |
| 103 | + return new ArrayList<>(resultSet); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +</TabItem> |
| 109 | +<TabItem value="Python" label="Python"> |
| 110 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 111 | + |
| 112 | +```python |
| 113 | +class Solution: |
| 114 | + def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]: |
| 115 | + result_set = set() |
| 116 | + i = 1 |
| 117 | + while i < bound: |
| 118 | + j = 1 |
| 119 | + while i + j <= bound: |
| 120 | + result_set.add(i + j) |
| 121 | + if y == 1: |
| 122 | + break |
| 123 | + j *= y |
| 124 | + if x == 1: |
| 125 | + break |
| 126 | + i *= x |
| 127 | + return list(result_set) |
| 128 | +``` |
| 129 | + |
| 130 | +</TabItem> |
| 131 | +</Tabs> |
| 132 | + |
| 133 | +#### Complexity Analysis |
| 134 | + |
| 135 | +- **Time Complexity**: $O(log_x(text{bound})*log_y(text{bound}))$, since we iterate logarithmically based on the bound. |
| 136 | +- **Space Complexity**: $O(k)$, where `k` is the number of unique powerful integers found. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +<h2>Authors:</h2> |
| 141 | + |
| 142 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 143 | +{['ImmidiSivani'].map(username => ( |
| 144 | + <Author key={username} username={username} /> |
| 145 | +))} |
| 146 | +</div> |
0 commit comments