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) 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++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
void greaterK(ll n, ll k)
{
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) {
p[i] = 0;
p[i + 1]++;
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;
if (p[j] >= 2) {
for ( auto & i : p)
i = 0;
p.pb(1);
}
ll ans = 0;
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
class GFG{
static void greaterK( int n, int k)
{
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 )
{
p[i] = 0 ;
p[i + 1 ]++;
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 ;
if (p[j] >= 2 )
{
p[index] = 1 ;
index++;
}
int ans = 0 ;
for ( int i = p.length - 1 ; i >= 0 ; --i)
{
ans = ans * k + p[i];
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int n = 29 , k = 7 ;
greaterK(n, k);
}
}
|
Python3
def greaterK(n, k):
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 ):
p[i] = 0
p[i + 1 ] + = 1
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
if (p[j] > = 2 ):
p[index] = 1
index + = 1
ans = 0
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)
|
C#
using System;
class GFG{
static void greaterK( int n, int k)
{
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)
{
p[i] = 0;
p[i + 1]++;
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;
if (p[j] >= 2)
{
p[index] = 1;
index++;
}
int ans = 0;
for ( int i = p.Length - 1; i >= 0; --i)
{
ans = ans * k + p[i];
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int n = 29, k = 7;
greaterK(n, k);
}
}
|
Javascript
<script>
function greaterK(n, k) {
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) {
p[i] = 0;
p[i + 1]++;
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;
if (p[j] >= 2) {
p[index] = 1;
index++;
}
let ans = 0;
for (let i = p.length - 1; i >= 0; --i) {
ans = ans * k + p[i];
}
document.write(ans + "<br>" );
}
let n = 29, k = 7;
greaterK(n, k);
</script>
|
Time Complexity: O(logkn)
Auxiliary Space: O(logkn)
Last Updated :
20 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...