Given a number n and a value k, the task is to find the smallest m(m>=n), such that m can be represented as a sum of distinct powers of k.
Input: n = 5, k = 5
Explanation: 5 = 51
Input: n = 29, k = 5
Explanation: 30 = 51 + 52
- Store the k-nary(base k) representation of n. Then traverse through each element of the base k representation.
- If the base k representation of this position is 1 or 0 then continue, if it is >1; it means that the current power of k occurs more than once.
- In that case, that power is added k (k’s-position value) times which makes it one power more .
- Since the smallest number has to be found, after this step, all the lower powers of k is reduced to 0 as adding (b=value at that position in base k representation) has already made the number bigger than the previous number.
- Finally, convert the number back to decimal form.
Below is the implementation of the above approach
- Smallest power of 4 greater than or equal to N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Highest power of 2 that divides a number represented in binary
- Check whether a number can be represented as sum of K distinct positive integers
- Find the smallest positive number which can not be represented by given digits
- Smallest N digit number which is a perfect fourth power
- Smallest number greater or equals to N such that it has no odd positioned bit set
- Smallest number greater than Y with sum of digits equal to X
- Smallest number greater than or equal to N divisible by K
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest Special Prime which is greater than or equal to a given number
- Modulo power for large numbers represented as strings
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Count of distinct power of prime factor of N
- Minimum element whose n-th power is greater than product of an array of size n
- Smallest subarray with sum greater than a given value
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Smallest subarray with k distinct numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.