Leetcode 970: Powerful Integers

grid47
grid47
Exploring patterns and algorithms
Aug 2, 2024 6 min read

Given three integers x, y, and bound, find all powerful integers that are less than or equal to bound. A powerful integer can be expressed in the form x^i + y^j for non-negative integers i and j. Return the results as a list with no duplicates, and the order of the elements does not matter.
Problem
Approach
Steps
Complexity
Input: Three integers x, y, and bound.
Example: Input: x = 2, y = 3, bound = 20
Constraints:
• 1 <= x, y <= 100
• 0 <= bound <= 10^6
Output: A list of all unique powerful integers less than or equal to bound.
Example: Output: [2, 3, 4, 5, 9, 10, 17]
Constraints:
• Each powerful integer must appear only once in the list.
• The integers in the output can be in any order.
Goal: Compute all unique powerful integers less than or equal to the given bound.
Steps:
• Iterate over possible values of i such that x^i <= bound.
• For each i, iterate over possible values of j such that x^i + y^j <= bound.
• If x or y is 1, break early to avoid infinite loops.
• Store the resulting sums in a set to ensure uniqueness.
• Convert the set to a list and return it.
Goal: Ensure the inputs are valid and the solution operates within computational limits.
Steps:
• 1 <= x, y <= 100
• 0 <= bound <= 10^6
• The result must contain no duplicate integers.
Assumptions:
• x and y are positive integers.
• The values of x and y can be 1, which simplifies the calculation.
• The bound is non-negative.
Input: Input: x = 3, y = 2, bound = 25
Explanation: The powerful integers are calculated as follows: 3^0 + 2^0 = 2, 3^1 + 2^0 = 4, 3^0 + 2^1 = 3, 3^1 + 2^1 = 5, 3^2 + 2^0 = 9, 3^2 + 2^1 = 11, 3^0 + 2^2 = 6, 3^1 + 2^2 = 10, 3^2 + 2^2 = 13, ... Output: [2, 3, 4, 5, 6, 9, 10, 11, 13]

Input: Input: x = 1, y = 1, bound = 5
Explanation: Since x and y are both 1, only one unique powerful integer can be formed: 1 + 1 = 2. Output: [2]

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus