Minimum powers of P and Q to represent N
Given integer N and values P and Q, The task is to calculate the minimum number of powers of P and Q required to generate N.
Note: The 0th power of the values is also considered.
Examples:
Input: N = 15, P = 2, Q = 3
Output: 3
Explanation: We can make 15 by using (8, 4, 3) or (9, 3, 3). Both take 3 numbers.
Input: N = 19, P = 4, Q = 3
Output: 2
Explanation: In the second case, we can make 19 by using (16, 3) which is 2 numbers.
Approach: Recursion (Memoization)
The Basic idea is to use memoization approach for this problem, simply we’ll check ways to reach or to generate N by considering both P and Q powers by making recursive calls.
Pseudo Code:
To check the powers being used in recursive relation.
‘long long int a=1;
ans = 1e9; // to store potential answer
if(power = 1){
return n;
}
while(n-a >= 0)
{
ans = min(ans, dp[n-a]);
a = a*power;
}
return ans+1;
Follow the steps mentioned below to implement the idea:
- Initialize a dp[] array of size N+1 and initialize it with 1e9.
- Set, the base cases, dp[0] = 0 and dp[1] = 1.
- Traverse through 2 to N and find the ways with powers.
- Way1 by considering the power of P.
- Way2 by considering the power of Q.
- Consider dp[i] = min(way1, way2).
- After traversing return dp[N].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int check( int n, int power, vector< int >& dp)
{
long long int a = 1;
int ans = 1e9;
if (power == 1)
return n;
while (n - a >= 0) {
ans = min(ans, dp[n - a]);
a = a * power;
}
return ans + 1;
}
int moves( int n, int p, int q)
{
vector< int > dp(n + 1, 1e9);
dp[0] = 0;
dp[1] = 1;
for ( int i = 2; i <= n; ++i) {
int way1 = check(i, p, dp);
int way2 = check(i, q, dp);
dp[i] = min(way1, way2);
}
return dp[n];
}
int main()
{
int N = 15, P = 2, Q = 3;
cout << moves(N, P, Q) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int check( int n, int power, int dp[])
{
long a = 1 ;
int ans = Integer.MAX_VALUE;
if (power == 1 )
return n;
while (n - a >= 0 ) {
ans = Math.min(ans, dp[n - ( int )a]);
a = a * power;
}
return ans + 1 ;
}
public static int moves( int n, int p, int q)
{
int dp[] = new int [n + 1 ];
for ( int i = 0 ; i < n + 1 ; i++) {
dp[i] = Integer.MAX_VALUE;
}
dp[ 0 ] = 0 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; ++i) {
int way1 = check(i, p, dp);
int way2 = check(i, q, dp);
dp[i] = Math.min(way1, way2);
}
return dp[n];
}
public static void main(String args[])
{
int N = 15 , P = 2 , Q = 3 ;
System.out.println(moves(N, P, Q));
}
}
|
Python3
def check(n, power, dp):
a = 1
ans = float ( "inf" )
if (power = = 1 ):
return n
while (n - a > = 0 ):
ans = min (ans, dp[n - a])
a = a * power
return ans + 1
def moves(n, p, q):
dp = [ float ( "inf" )] * (n + 1 )
dp[ 0 ] = 0
dp[ 1 ] = 1
for i in range ( 2 , n + 1 ):
way1 = check(i, p, dp)
way2 = check(i, q, dp)
dp[i] = min (way1, way2)
return dp[n]
N, P, Q = 15 , 2 , 3
print (moves(N, P, Q))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int check( int n, int power, int [] dp)
{
long a = 1;
int ans = int .MaxValue;
if (power == 1)
return n;
while (n - a >= 0) {
ans = Math.Min(ans, dp[n - ( int )a]);
a = a * power;
}
return ans + 1;
}
public static int moves( int n, int p, int q)
{
int [] dp = new int [n + 1];
for ( int i = 0; i < n + 1; i++) {
dp[i] = int .MaxValue;
}
dp[0] = 0;
dp[1] = 1;
for ( int i = 2; i <= n; ++i) {
int way1 = check(i, p, dp);
int way2 = check(i, q, dp);
dp[i] = Math.Min(way1, way2);
}
return dp[n];
}
public static void Main(String[] args)
{
int N = 15, P = 2, Q = 3;
Console.WriteLine(moves(N, P, Q));
}
}
|
Javascript
function check(n, power, dp) {
var a = 1;
var ans = Number.MAX_VALUE;
if (power == 1) {
return n;
}
while (n - a >= 0) {
ans = Math.min(ans, dp[n - a]);
a = a * power;
}
return ans + 1;
}
function moves(n, p, q) {
var dp = new Array(n + 1).fill(Number.MAX_VALUE);
dp[0] = 0;
dp[1] = 1;
for ( var i = 2; i <= n; i++) {
var way1 = check(i, p, dp);
var way2 = check(i, q, dp);
dp[i] = Math.min(way1, way2);
}
return dp[n];
}
var N = 15, P = 2, Q = 3;
console.log(moves(N, P, Q));
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N )
Related Articles:
Last Updated :
13 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...