Minimum cost to reduce the integer N to 1 as per given conditions
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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the minimum cost// to reduce the integer N to 1// by the given operationsint min_cost(int n, int x, int p, int q){ // Check if x is 1 if (x == 1) { // Print the answer cout << (n - 1) * p << endl; return 0; } // Prestore the answer int ans = (n - 1) * p; int pre = 0; // Iterate till n exists while (n > 1) { // Divide N by x int tmp = n / x; if (tmp < 0) break; pre += (n - tmp * x) * p; // Reduce n by x n /= x; // Add the cost pre += q; // Update the answer ans = min(ans, pre + (n - 1) * p); } // Return the answer return ans;} // Driver Codeint main(){ // Initialize the variables int n = 5, x = 2, p = 2, q = 3; // Function call cout << min_cost(n, x, p, q); return 0;} |
Java
// Java program for the above approachimport java.util.*; class GFG{ // Function to find the minimum cost// to reduce the integer N to 1// by the given operationsstatic int min_cost(int n, int x, int p, int q){ // Check if x is 1 if (x == 1) { // Print the answer System.out.println((n - 1) * p); return 0; } // Prestore the answer int ans = (n - 1) * p; int pre = 0; // Iterate till n exists while (n > 1) { // Divide N by x int tmp = n / x; if (tmp < 0) break; pre += (n - tmp * x) * p; // Reduce n by x n /= x; // Add the cost pre += q; // Update the answer ans = Math.min(ans, pre + (n - 1) * p); } // Return the answer return ans;} // Driver codepublic static void main(String[] args){ // Initialize the variables int n = 5, x = 2, p = 2, q = 3; // Function call System.out.println(min_cost(n, x, p, q));}} // This code is contributed by offbeat |
Python3
# Python3 program for the above approach # Function to find the minimum cost# to reduce the integer N to 1# by the given operationsdef min_cost(n, x, p, q): # Check if x is 1 if (x == 1): # Print the answer print((n - 1) * p) return 0 # Prestore the answer ans = (n - 1) * p pre = 0 # Iterate till n exists while (n > 1): # Divide N by x tmp = n // x if (tmp < 0): break pre += (n - tmp * x) * p # Reduce n by x n //= x # Add the cost pre += q # Update the answer ans = min(ans, pre + (n - 1) * p) # Return the answer return ans # Driver Codeif __name__ == '__main__': # Initialize the variables n = 5; x = 2; p = 2; q = 3; # Function call print(min_cost(n, x, p, q)) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approachusing System; class GFG{ // Function to find the minimum cost// to reduce the integer N to 1// by the given operationsstatic int min_cost(int n, int x, int p, int q){ // Check if x is 1 if (x == 1) { // Print the answer Console.WriteLine((n - 1) * p); return 0; } // Prestore the answer int ans = (n - 1) * p; int pre = 0; // Iterate till n exists while (n > 1) { // Divide N by x int tmp = n / x; if (tmp < 0) break; pre += (n - tmp * x) * p; // Reduce n by x n /= x; // Add the cost pre += q; // Update the answer ans = Math.Min(ans, pre + (n - 1) * p); } // Return the answer return ans;} // Driver codepublic static void Main(String[] args){ // Initialize the variables int n = 5, x = 2, p = 2, q = 3; // Function call Console.WriteLine(min_cost(n, x, p, q));}} // This code is contributed by princiraj1992 |
Output:
7
Time Complexity: O(N)
Auxiliary Space: O(1)


