Given two positive integers N and K, the task is to find the minimum number to be added to N to make it a power of K.
Input: N = 9, K = 10
9 + 1 = 10 = 101
Input: N = 20, K = 5
20 + 5 = 25 = 52
Approach: The idea to solve this problem is to observe that the minimum power of K which can be formed from N is the next greater power of K. So, the idea is to find the next greater power of K and find the difference between N and this number. The next greater power of K can be found by the formula,
Kint(log(N)/log(K)) + 1
Therefore, the minimum number to be added can be computed by:
Minimum Number = Kint(log(N)/log(K)) + 1 – N
Below is the implementation of the above approach:
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.