Minimum cost to reduce the integer N to 1 as per given conditions
Last Updated :
27 Oct, 2021
Given four integers N, X, P, and Q, the task is to find the minimum cost to make N to 1 by the following two operations:
- Subtract 1 from N with cost as P.
- Divide N by X(if N is divisible by X), with cost Q.
Examples:
Input: N = 5, X = 2, P = 2, Q = 3
Output: 7
Explanation:
Operation 1: 5 – 1 -> cost = 2
Operation 2: 4 / 2 -> cost = 3
Operation 3: 2 – 1 -> cost = 2
Minimum total cost is 2 + 3 + 2 = 7.
Input: N = 6, X = 6, P = 2, Q = 1
Output: 1
Explanation:
Operation 1: 6 / 6 with cost = 1, hence that would be the minimum.
Approach: This problem can be solved using Greedy Approach. Below are the observations:
- If x = 1, then the answer is (N – 1) * P.
- Otherwise, if N is less than X, then it is only possible to decrease the number by 1, so the answer is (N – 1) * P.
- Otherwise, take the first operation until N is not divisible by X.
- Choose the second operation optimally by comparing the first and second operations i.e., if we can perform the first operation such that the cost of reducing N to 1 is minimum, else choose the second operation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_cost( int n, int x, int p, int q)
{
if (x == 1) {
cout << (n - 1) * p << endl;
return 0;
}
int ans = (n - 1) * p;
int pre = 0;
while (n > 1) {
int tmp = n / x;
if (tmp < 0)
break ;
pre += (n - tmp * x) * p;
n /= x;
pre += q;
ans = min(ans,
pre + (n - 1) * p);
}
return ans;
}
int main()
{
int n = 5, x = 2, p = 2, q = 3;
cout << min_cost(n, x, p, q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int min_cost( int n, int x,
int p, int q)
{
if (x == 1 )
{
System.out.println((n - 1 ) * p);
return 0 ;
}
int ans = (n - 1 ) * p;
int pre = 0 ;
while (n > 1 )
{
int tmp = n / x;
if (tmp < 0 )
break ;
pre += (n - tmp * x) * p;
n /= x;
pre += q;
ans = Math.min(ans,
pre + (n - 1 ) * p);
}
return ans;
}
public static void main(String[] args)
{
int n = 5 , x = 2 , p = 2 , q = 3 ;
System.out.println(min_cost(n, x, p, q));
}
}
|
Python3
def min_cost(n, x, p, q):
if (x = = 1 ):
print ((n - 1 ) * p)
return 0
ans = (n - 1 ) * p
pre = 0
while (n > 1 ):
tmp = n / / x
if (tmp < 0 ):
break
pre + = (n - tmp * x) * p
n / / = x
pre + = q
ans = min (ans, pre + (n - 1 ) * p)
return ans
if __name__ = = '__main__' :
n = 5 ; x = 2 ;
p = 2 ; q = 3 ;
print (min_cost(n, x, p, q))
|
C#
using System;
class GFG{
static int min_cost( int n, int x,
int p, int q)
{
if (x == 1)
{
Console.WriteLine((n - 1) * p);
return 0;
}
int ans = (n - 1) * p;
int pre = 0;
while (n > 1)
{
int tmp = n / x;
if (tmp < 0)
break ;
pre += (n - tmp * x) * p;
n /= x;
pre += q;
ans = Math.Min(ans,
pre + (n - 1) * p);
}
return ans;
}
public static void Main(String[] args)
{
int n = 5, x = 2, p = 2, q = 3;
Console.WriteLine(min_cost(n, x, p, q));
}
}
|
Javascript
<script>
function min_cost(n, x, p, q) {
if (x == 1) {
document.write((n - 1) * p + "<br>" );
return 0;
}
let ans = (n - 1) * p;
let pre = 0;
while (n > 1) {
let tmp = Math.floor(n / x);
if (tmp < 0)
break ;
pre += (n - tmp * x) * p;
n = Math.floor(n / x)
pre += q;
ans = Math.min(ans,
pre + (n - 1) * p);
}
return ans;
}
let n = 5, x = 2, p = 2, q = 3;
document.write(min_cost(n, x, p, q));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...