Powerful Integers - Problem

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.

An integer is powerful if it can be represented as xi + yj for some integers i >= 0 and j >= 0.

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

Input & Output

Example 1 — Basic Case
$ Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
💡 Note: Powers of 2: [1,2,4,8], Powers of 3: [1,3,9]. Valid sums ≤ 10: 1+1=2, 2+1=3, 1+3=4, 2+3=5, 4+3=7, 8+1=9, 1+9=10
Example 2 — Edge Case x=1
$ Input: x = 1, y = 2, bound = 5
Output: [2,3,5]
💡 Note: x=1 means x^i=1 for all i. Powers of 2: [1,2,4]. Valid sums: 1+1=2, 1+2=3, 1+4=5
Example 3 — Small Bound
$ Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]
💡 Note: Powers of 3: [1,3,9], Powers of 5: [1,5]. Valid sums ≤ 15: 1+1=2, 3+1=4, 1+5=6, 3+5=8, 9+1=10, 9+5=14

Constraints

  • 1 ≤ x, y ≤ 100
  • 0 ≤ bound ≤ 106

Visualization

Tap to expand
Powerful Integers INPUT x = 2 y = 3 bound = 10 Powers of x=2: 1 2 4 8 Powers of y=3: 1 3 9 Find all x^i + y^j where result <= 10 i >= 0, j >= 0 ALGORITHM STEPS 1 Initialize Use Set for unique results 2 Loop x^i While x^i <= bound 3 Nested Loop y^j While x^i + y^j <= bound 4 Early Termination Break when x=1 or y=1 Sample Calculations: 1+1=2 1+3=4 1+9=10 2+1=3 2+3=5 2+9=11(skip) 4+1=5 4+3=7 4+9=13(skip) 8+1=9 8+3=11(skip) FINAL RESULT Unique Powerful Integers: 2 1+1 3 2+1 4 1+3 5 2+3 7 4+3 9 8+1 10 1+9 [2, 3, 4, 5, 7, 9, 10] OK Key Insight: Early termination optimization: When x=1 or y=1, the power stays constant (1^n = 1), so we only need one iteration. Use a Set to automatically handle duplicates like 2+3=5 and 4+1=5. TutorialsPoint - Powerful Integers | Optimized with Early Termination
Asked in
Google 25 Facebook 18 Amazon 15
28.5K Views
Medium Frequency
~15 min Avg. Time
485 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