Given two integers M and N, the task is to find the minimum cost to convert M to N by repetitive addition of even divisors of the current value of M (except M).
The cost to add an even divisor of the current value of M, say d, is equal to M / d.
Print “-1” if it is impossible to convert M to N.
Input: M = 6, N = 24
Step 1: M = 6 + 2 = 8, Cost = (6 / 2) = 3
Step 2: M = 8 + 4 = 12, Cost = 3 + (8 / 2) = 5
Step 3: M = 12 + 6 = 18, Cost = 5 + (12/ 6) = 7
Step 4: M = 18 + 6 = 24, Cost = 7 + (18 / 6) = 10
Therefore, the minimum cost to convert M to N is equal to 10.
Input: M = 9, N = 17
Since there are no even divisors of 9, therefore, conversion is not possible.
Naive approach: The simplest approach is to iterate through all possible even divisors of given number M and recursively calculate the minimum cost to change M to N. The recurrence relation formed is given by:
min_cost = Math.min(min_cost, m / i + minSteps(m + i, n))
Below is the implementation of the above approach :
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using dynamic programming and memoization to the above implementation. Instead of computing the states again and again store it in an array dp and use it when required.
dp(m) = min(dp(m), (m/i) + dp(m+i)) for all even divisors of m less than m
Below is the implementation of the above approach:
Time Complexity: O(Nlog(M))
Auxiliary Space: O(N)
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.
- Minimum repeated addition of even divisors of N required to convert N to M
- Check if a number has an odd count of odd divisors and even count of even divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Number formed after K times repeated addition of smallest divisor of N
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Minimum array size after repeated replacement of even sum pair with sum
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimum steps to convert X to Y by repeated division and multiplication
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Find sum of divisors of all the divisors of a natural number
- Divisors of n-square that are not divisors of n
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Minimum cost required to convert all Subarrays of size K to a single element
- Minimum cost to convert given string to consist of only vowels
- Generating all divisors of a number using its prime factorization
- Product of divisors of a number from a given list of its prime factors
- Find number from its divisors
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.