Given a number N, the task is to form N (from 0) by adding 1 or 2 in the minimum number of operations X such that X is divisible by M.
Input: N = 10, M = 2
Output: X = 6
Taken operations are 2 2 2 2 1 1
X = 6 which is divisible by 2
Input: N = 17, M = 4
- As we can take either 1 or 2 steps at a time, so we can say that minimum no. of steps taken is n/2, and the maximum no. of steps is n, irrespective of that the no. of steps are divisible by m.
- So we have to count n/2 steps to get a minimum number of steps. Now if n is even, then a minimum number of steps will be n/2, but if it is odd, then it will be n/2+1, irrespective of that the no. of steps are divisible by m. To make minimum steps of a multiple of m we can do floor((minumum_steps + m – 1)/m) * m
- Also if n is less than m, then it is not possible to find the minimum number of steps, and in that case, we will return -1.
Below is the implementation of above approach:
Time Cmplexity: O(1)
- Find minimum possible digit sum after adding a number d
- Reduce N to 1 with minimum number of given operations
- Minimum number of operations required to reduce N to 1
- Minimum prime number operations to convert A to B
- Minimum number operations required to convert n to m | Set-2
- Convert a number m to n using minimum number of given operations
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum value that divides one number and divisible by other
- Find minimum number of merge operations to make an array palindrome
- Find the minimum number of operations required to make all array elements equal
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum number of given moves required to make N divisible by 25
- Minimum and Maximum element of an array which is divisible by a given number k
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.