Smallest number greater than n that can be represented as a sum of distinct power of k Last Updated : 20 Feb, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Examples: Input: n = 5, k = 5 Output: 5 Explanation: 5 = 51 Input: n = 29, k = 5 Output: 30 Explanation: 30 = 51 + 52 Approach: 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 (((k-1)+1).kx = k.kx = kx+1).Since the smallest number has to be found, after this step, all the lower powers of k are reduced to 0 as adding(k-b)kx (b=value at that position in the base k representation) has already made the bigger than the previous number.Finally, convert the number back to decimal form. Below is the implementation of the above approach. C++ // C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back // Function to find the smallest number // greater than or equal to n represented // as the sum of distinct powers of k void greaterK(ll n, ll k) { // Vector P to store the base k // representation of the number vector<ll> p; ll x = n; while (x) { p.pb(x % k); x /= k; } int idx = 0; for (ll i = 0; i < (ll)p.size() - 1; ++i) { if (p[i] >= 2) { // If the representation is >=2, then // this power of k has to be added // once again and then increase the // next power of k and make the // current power 0 p[i] = 0; p[i + 1]++; // Reduce all the lower power of // k to 0 for (int j = idx; j < i; ++j) { p[j] = 0; } idx = i + 1; } if (p[i] == k) { p[i] = 0; p[i + 1]++; } } ll j = (ll)p.size() - 1; // Check if the most significant // bit also satisfy the above // conditions if (p[j] >= 2) { for (auto& i : p) i = 0; p.pb(1); } ll ans = 0; // Converting back from the // k-nary representation to // decimal form. for (ll i = p.size() - 1; i >= 0; --i) { ans = ans * k + p[i]; } cout << ans << endl; } int main() { ll n = 29, k = 7; greaterK(n, k); return 0; } Java // Java implementation of the above approach class GFG{ // Function to find the smallest number // greater than or equal to n represented // as the sum of distinct powers of k static void greaterK(int n, int k) { // Vector P to store the base k // representation of the number int []p = new int[String.valueOf(n).length() + 2]; int index = 0; int x = n; while (x > 0) { p[index]=(int) (x % k); x /= k; index++; } int idx = 0; for (int i = 0; i < p.length - 1; ++i) { if (p[i] >= 2) { // If the representation is >=2, then // this power of k has to be added // once again and then increase the // next power of k and make the // current power 0 p[i] = 0; p[i + 1]++; // Reduce all the lower power of // k to 0 for (int j = idx; j < i; ++j) { p[j] = 0; } idx = i + 1; } if (p[i] == k) { p[i] = 0; p[i + 1]++; } } int j = p.length - 1; // Check if the most significant // bit also satisfy the above // conditions if (p[j] >= 2) { p[index] = 1; index++; } int ans = 0; // Converting back from the // k-nary representation to // decimal form. for (int i = p.length - 1; i >= 0; --i) { ans = ans * k + p[i]; } System.out.print(ans +"\n"); } // Driver code public static void main(String[] args) { int n = 29, k = 7; greaterK(n, k); } } // This code is contributed by 29AjayKumar Python3 # Python3 implementation of the above approach # Function to find the smallest number # greater than or equal to n represented # as the sum of distinct powers of k def greaterK(n, k): # Vector P to store the base k # representation of the number index = 0 p = [0 for i in range(n + 2)] x = n while (x > 0): p[index] = x % k x //= k index += 1 idx = 0 for i in range(0,len(p)-1, 1): if (p[i] >= 2): # If the representation is >=2, then # this power of k has to be added # once again and then increase the # next power of k and make the # current power 0 p[i] = 0 p[i + 1] += 1 # Reduce all the lower power of # k to 0 for j in range(idx, i, 1): p[j] = 0 idx = i + 1 if (p[i] == k): p[i] = 0 p[i + 1] += 1 j = len(p) - 1 # Check if the most significant # bit also satisfy the above # conditions if (p[j] >= 2): p[index] = 1 index += 1 ans = 0 # Converting back from the # k-nary representation to # decimal form. i = len(p)-1 while(i>= 0): ans = ans * k + p[i] i -= 1 print(ans) if __name__ == '__main__': n = 29 k = 7 greaterK(n, k) # This code is contributed by Surendra_Gangwar C# // C# implementation of the above approach using System; class GFG{ // Function to find the smallest number // greater than or equal to n represented // as the sum of distinct powers of k static void greaterK(int n, int k) { // List P to store the base k // representation of the number int []p = new int[String.Join("",n).Length + 2]; int index = 0; int x = n; int j; while (x > 0) { p[index] = (int) (x % k); x /= k; index++; } int idx = 0; for (int i = 0; i < p.Length - 1; ++i) { if (p[i] >= 2) { // If the representation is >=2, then // this power of k has to be added // once again and then increase the // next power of k and make the // current power 0 p[i] = 0; p[i + 1]++; // Reduce all the lower power of // k to 0 for (j = idx; j < i; ++j) { p[j] = 0; } idx = i + 1; } if (p[i] == k) { p[i] = 0; p[i + 1]++; } } j = p.Length - 1; // Check if the most significant // bit also satisfy the above // conditions if (p[j] >= 2) { p[index] = 1; index++; } int ans = 0; // Converting back from the // k-nary representation to // decimal form. for (int i = p.Length - 1; i >= 0; --i) { ans = ans * k + p[i]; } Console.Write(ans +"\n"); } // Driver code public static void Main(String[] args) { int n = 29, k = 7; greaterK(n, k); } } // This code is contributed by PrinciRaj1992 JavaScript <script> // Javascript implementation of the above approach // Function to find the smallest number // greater than or equal to n represented // as the sum of distinct powers of k function greaterK(n, k) { // Vector P to store the base k // representation of the number let p = new Array(String(n).length + 2).fill(0); let index = 0; let x = n; while (x > 0) { p[index] = (x % k); x = Math.floor(x / k); index++; } let idx = 0; for (let i = 0; i < p.length - 1; ++i) { if (p[i] >= 2) { // If the representation is >=2, then // this power of k has to be added // once again and then increase the // next power of k and make the // current power 0 p[i] = 0; p[i + 1]++; // Reduce all the lower power of // k to 0 for (let j = idx; j < i; ++j) { p[j] = 0; } idx = i + 1; } if (p[i] == k) { p[i] = 0; p[i + 1]++; } } let j = p.length - 1; // Check if the most significant // bit also satisfy the above // conditions if (p[j] >= 2) { p[index] = 1; index++; } let ans = 0; // Converting back from the // k-nary representation to // decimal form. for (let i = p.length - 1; i >= 0; --i) { ans = ans * k + p[i]; } document.write(ans + "<br>"); } // Driver code let n = 29, k = 7; greaterK(n, k); // This code is contributed by gfgking </script> Output: 49 Time Complexity: O(logkn) Auxiliary Space: O(logkn) Comment More infoAdvertise with us L little_angel Follow Improve Article Tags : C/C++ Puzzles Pattern Searching Searching Mathematical Technical Scripter Competitive Programming C++ Programs C Language C++ DSA Arrays Technical Scripter 2019 +8 More Practice Tags : CPPArraysMathematicalPattern SearchingSearching +1 More Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used 5 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Like