Powerful Integers - Problem

Imagine you're a mathematician exploring the fascinating world of powerful integers! 🔢

Given three integers x, y, and bound, your mission is to find all the powerful integers that don't exceed the given boundary. A number is considered powerful if it can be expressed as xi + yj where both i and j are non-negative integers (≥ 0).

Your Goal: Return a list of all unique powerful integers ≤ bound. The order doesn't matter, but each value should appear only once in your result.

Example: If x=2, y=3, and bound=10, then:

  • 20 + 30 = 1 + 1 = 2
  • 21 + 30 = 2 + 1 = 3
  • 20 + 31 = 1 + 3 = 4
  • And so on...

Input & Output

example_1.py — Basic Case
$ Input: x = 2, y = 3, bound = 10
Output: [2, 3, 4, 5, 7, 9, 10]
💡 Note: Powerful integers: 2^0+3^0=2, 2^1+3^0=3, 2^0+3^1=4, 2^2+3^0=5, 2^1+3^1=7, 2^0+3^2=9, 2^1+3^2=10. Note that 2^2+3^1=7 is duplicate.
example_2.py — Edge Case with 1
$ Input: x = 3, y = 5, bound = 15
Output: [2, 4, 6, 8, 10, 14]
💡 Note: Valid combinations: 3^0+5^0=2, 3^1+5^0=4, 3^0+5^1=6, 3^2+5^0=10, 3^1+5^1=8, 3^0+5^2=26 (too big), but we get [2,4,6,8,10,14] after removing duplicates.
example_3.py — Small Bound
$ Input: x = 2, y = 1, bound = 10
Output: [2, 3, 5, 9]
💡 Note: Since y=1, all y^j=1. So we get: 1+1=2, 2+1=3, 4+1=5, 8+1=9, 16+1=17 (exceeds bound).

Visualization

Tap to expand
🔮 Powerful Integer Crystal Collection💎 X Crystals1x^0xx^1x^2💎 Y Crystals1y^0yy^1y^2✨ Powerful Integers1+1x+11+yx+yx²+y²...and more!+=🧠 Key Insights:💡 Powers grow exponentially → Only log(bound) iterations needed⚠️ Special handling for x=1 or y=1 to prevent infinite loops🎯 Use HashSet to automatically eliminate duplicate sums⚡ Time Complexity: O(log²(bound)) - Very efficient!
Understanding the Visualization
1
Generate X Powers
Create all powers x^i that don't exceed the bound
2
Generate Y Powers
Create all powers y^j that don't exceed the bound
3
Combine Powers
Try all combinations of x^i + y^j within the bound
4
Collect Unique Results
Use a set to automatically handle duplicates
Key Takeaway
🎯 Key Insight: Since powers grow exponentially, there are only logarithmically many combinations to check, making this algorithm very efficient even for large bounds!

Time & Space Complexity

Time Complexity
⏱️
O(log(bound)²)

We generate at most log(bound) powers for both x and y, then combine them in O(log²(bound)) operations

n
2n
Linearithmic
Space Complexity
O(log(bound)²)

We store the powers lists and result set, each containing at most O(log(bound)) elements

n
2n
Linearithmic Space

Constraints

  • 1 ≤ x, y ≤ 100
  • 0 ≤ bound ≤ 106
  • Important: Handle cases where x = 1 or y = 1 to avoid infinite loops
  • Each powerful integer should appear at most once in the result
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
21.5K Views
Medium Frequency
~18 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen