Given three non-negative integers x, y and bound, the task is to print all the powerful integer ? bound in sorted order.
A powerful integer is of the form xi + yj for all i, j ? 0.
Input: x = 3, y = 5, bound = 10
Output: 2 4 6 8 10
30 + 50 = 1 + 1 = 2
30 + 51 = 1 + 5 = 6
31 + 50 = 3 + 1 = 4
31 + 51 = 3 + 5 = 8
32 + 50 = 9 + 1 = 10
Input: x = 2, y = 3, bound = 10
Output: 2 3 4 5 7 9 10
Approach: Initialize i = 0 for outer loop and j = 0 for inner loop, if xi = bound then break out of the outer loop (as adding any power of y to it will make it out of the bound). If xi + yj > bound then break out of the inner loop and in every other iteration of the inner loop, save xi + yj in a set to maintain a distinct and sorted list of the powerful integers. Print the contents of the set in the end.
To avoid calculating the powers of y again and again, all the powers of y can be pre-calculated and stored in a vector.
Below is the implementation of the above approach:
2 3 4 5 7 9 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Sum of first N natural numbers with all powers of 2 added twice
- Print all possible ways to write N as sum of two or more positive integers
- Print all prime factors and their powers
- Represent n as the sum of exactly k powers of two | Set 2
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Check if a number can be expressed as sum of two Perfect powers
- Sum of fourth powers of the first n natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Find k numbers which are powers of 2 and have sum N | Set 1
- Sum of first N natural numbers by taking powers of 2 as negative number
- Sum of first N natural numbers which are not powers of K
- Find the sum of numbers from 1 to n excluding those which are powers of K
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Find all powers of 2 less than or equal to a given number
- Print all n-digit numbers whose sum of digits equals to given sum
- Print powers using Anonymous Function in Python
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Add two integers of different base and represent sum in smaller base of the two
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.