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.
Input: N = 5, X = 2, P = 2, Q = 3
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
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:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize the last Array element as per the given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Count of Binary Strings possible as per given conditions
- Maximum items that can be bought from the cost Array based on given conditions
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimum initial vertices to traverse whole matrix with given conditions
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Minimum number of given operations required to reduce the array to 0 element
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Maximize Profit by trading stocks based on given rate per day
- Arrange the numbers in the Array as per given inequalities
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Change the given string according to the given conditions
- Minimum number of operations required to reduce N to 1
- Minimum steps required to reduce all the elements of the array to zero
- Minimum number of operations required to reduce N to 0
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Minimum cost to convert str1 to str2 with the given operations
- Count valid pairs in the array satisfying given conditions
- Generate an array of size K which satisfies the given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.