Find Kth number that can be written as sum of different powers of N Last Updated : 13 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two positive integers N and K. The task is to find the Kth number that can be written as the sum of different non-negative powers of N. Examples: Input: N = 3, K = 4Output: 9Explanation:First number that can be written as sum of powers of 3 is [1 = 30]Second number that can be written as sum of powers of 3 is [3 = 31]Third number that can be written as sum of powers of 3 is [4 = 30 + 31]Fourth number that can be written as sum of powers of 3 is [9 = 32]Therefore answer is 9. Input: N = 2, K = 12Output: 12 Approach: This problem can be solved by using the concept of decimal to binary conversion. The idea is to find the binary representation of K and start iterating from the least significant bit to the most significant bit. If the current bit is set then include the corresponding power of N to the answer otherwise skip that bit. Refer to the picture below for a better understanding. Example: When N = 3, K = 9 Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find Kth number that can be // represented as sum of different // non-negative powers of N int FindKthNum(int n, int k) { // To store the ans int ans = 0; // Value of current power of N int currPowOfN = 1; // Iterating through bits of K // from LSB to MSB while (k) { // If the current bit is 1 then include // corresponding power of n into ans if (k & 1) { ans = ans + currPowOfN; } currPowOfN = currPowOfN * n; k = k / 2; } // Return the result return ans; } // Driver Code int main() { int N = 3; int K = 4; cout << FindKthNum(N, K); } Java // Java program for the above approach class GFG { // Function to find Kth number that can be // represented as sum of different // non-negative powers of N static int FindKthNum(int n, int k) { // To store the ans int ans = 0; // Value of current power of N int currPowOfN = 1; // Iterating through bits of K // from LSB to MSB while (k > 0) { // If the current bit is 1 then include // corresponding power of n into ans if ((k & 1) == 1) { ans = ans + currPowOfN; } currPowOfN = currPowOfN * n; k = k / 2; } // Return the result return ans; } // Driver Code public static void main(String []args) { int N = 3; int K = 4; System.out.println(FindKthNum(N, K)); } } // This Code is contributed by ihritik Python3 # Python program for the above approach # Function to find Kth number that can be # represented as sum of different # non-negative powers of N def FindKthNum(n, k): # To store the ans ans = 0 # value of current power of N currPowOfN = 1 # Iterating through bits of K # from LSB to MSB while (k > 0): # If the current bit is 1 then include # corresponding power of n into ans if ((k & 1) == 1) : ans = ans + currPowOfN currPowOfN = currPowOfN * n k = k // 2 # Return the result return ans # Driver Code N = 3 K = 4 print(FindKthNum(N, K)); # This Code is contributed by ihritik C# // C# program for the above approach using System; class GFG { // Function to find Kth number that can be // represented as sum of different // non-negative powers of N static int FindKthNum(int n, int k) { // To store the ans int ans = 0; // Value of current power of N int currPowOfN = 1; // Iterating through bits of K // from LSB to MSB while (k > 0) { // If the current bit is 1 then include // corresponding power of n into ans if ((k & 1) == 1) { ans = ans + currPowOfN; } currPowOfN = currPowOfN * n; k = k / 2; } // Return the result return ans; } // Driver Code public static void Main() { int N = 3; int K = 4; Console.WriteLine(FindKthNum(N, K)); } } // This Code is contributed by ihritik JavaScript <script> // JavaScript program for the above approach // Function to find Kth number that can be // represented as sum of different // non-negative powers of N function FindKthNum(n, k) { // To store the ans let ans = 0; // Value of current power of N let currPowOfN = 1; // Iterating through bits of K // from LSB to MSB while (k) { // If the current bit is 1 then include // corresponding power of n into ans if (k & 1) { ans = ans + currPowOfN; } currPowOfN = currPowOfN * n; k = Math.floor(k / 2); } // Return the result return ans; } // Driver Code let N = 3; let K = 4; document.write(FindKthNum(N, K)); // This code is contributed by rohitsingh07052. </script> Output: 9 Time Complexity: O(log2K) Space Complexity: O(1) Comment More infoAdvertise with us Next Article Find Kth number that can be written as sum of different powers of N K koladiyadrashti123 Follow Improve Article Tags : Algorithms Greedy Mathematical C++ Programs Data Structures C++ DSA number-theory maths-power Maths +6 More Practice Tags : CPPAlgorithmsData StructuresGreedyMathematicalnumber-theory +2 More Similar Reads Smallest number greater than n that can be represented as a sum of distinct power of k 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) repre 8 min read Distinct powers of a number N such that the sum is equal to K Given two numbers N and K, the task is to print the distinct powers of N which are used to get the sum K. If it's not possible, print -1. Examples: Input: N = 3, K = 40 Output: 0, 1, 2, 3 Explanation: The value of N is 3. 30 + 31 + 32 + 33 = 40 Input: N = 4, K = 65 Output: 0, 3 The value of N is 4. 14 min read C++ Program To Find Armstrong Numbers Between Two Integers A positive integer with digits a, b, c, d... is called an Armstrong number of order n if following condition is satisfied. abcd... = an + bn + cn + dn +... 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153 Therefore, 153 is an Armstrong number. Examples: Input: 100 400 Output: 153 370 371 Explanatio 2 min read Sum of first n term of Series 3, 5, 9, 17, 33.... Given n, we need to find sum of first n terms of the series represented as Sn = 3 + 5 + 9 + 17 + 33 ⦠upto nExamples: Input : 2Output : 83 + 5 = 8Input : 5Output : 673 + 5 + 9 + 17 + 33 = 67 Let, the nth term be denoted by tn. This problem can easily be solved by splitting each term as follows : Sn 7 min read Find Nth positive number whose digital root is X Given a number X ( 1<= X <= 9) and a positive number N, find the Nth positive number whose digital root is X.Digital Root: The digital root of a positive number is obtained by iteratively summing up the digits of a number. On each iteration, the number is replaced by the sum of its digit and t 9 min read Like