# Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M

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.

Examples:

Input: N = 10, M = 2
Output: X = 6
Explanation:
Taken operations are 2 2 2 2 1 1
X = 6 which is divisible by 2

Input: N = 17, M = 4
Output: 8

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• 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:

## C++

 `// C++ program to find minimum ` `// number of steps to cover distance x ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate the minimum number of steps required ` `// total steps taken is divisible ` `// by m and only 1 or 2 steps can be taken at // a time ` `int` `minsteps(``int` `n, ``int` `m) ` `{ ` ` `  `    ``// If m > n ans is -1 ` `    ``if` `(m > n) { ` `        ``return` `-1; ` `    ``} ` `    ``// else discussed above approach ` `    ``else` `{ ` `        ``return` `((n + 1) / 2 + m - 1) / m * m; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 17, m = 4; ` `    ``int` `ans = minsteps(n, m); ` `    ``cout << ans << ``'\n'``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find minimum  ` `// number of steps to cover distance x  ` `class` `GFG  ` `{ ` ` `  `    ``// Function to calculate the  ` `    ``// minimum number of steps required  ` `    ``// total steps taken is divisible  ` `    ``// by m and only 1 or 2 steps can be ` `    ``// taken at // a time  ` `    ``static` `int` `minsteps(``int` `n, ``int` `m)  ` `    ``{  ` `     `  `        ``// If m > n ans is -1  ` `        ``if` `(m > n)  ` `        ``{ ` `            ``return` `-``1``;  ` `        ``}  ` `         `  `        ``// else discussed above approach  ` `        ``else`  `        ``{  ` `            ``return` `((n + ``1``) / ``2` `+ m - ``1``) / m * m;  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `n = ``17``, m = ``4``;  ` `        ``int` `ans = minsteps(n, m);  ` `        ``System.out.println(ans);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 program to find minimum ` `# number of steps to cover distance x ` ` `  `# Function to calculate the minimum number of  ` `# steps required total steps taken is divisible ` `# by m and only 1 or 2 steps can be taken at a time ` `def` `minsteps(n, m): ` ` `  `    ``# If m > n ans is -1 ` `    ``if` `(m > n): ` `        ``return` `-``1` `         `  `    ``# else discussed above approach ` `    ``else` `: ` `        ``return` `((n ``+` `1``) ``/``/` `2` `+` `m ``-` `1``) ``/``/` `m ``*` `m; ` ` `  `# Driver code ` `n ``=` `17` `m ``=` `4` `ans ``=` `minsteps(n, m) ` `print``(ans) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# program to find minimum  ` `// number of steps to cover distance x  ` `using` `System; ` `     `  `class` `GFG  ` `{ ` ` `  `    ``// Function to calculate the  ` `    ``// minimum number of steps required  ` `    ``// total steps taken is divisible  ` `    ``// by m and only 1 or 2 steps can be ` `    ``// taken at // a time  ` `    ``static` `int` `minsteps(``int` `n, ``int` `m)  ` `    ``{  ` `     `  `        ``// If m > n ans is -1  ` `        ``if` `(m > n)  ` `        ``{ ` `            ``return` `-1;  ` `        ``}  ` `         `  `        ``// else discussed above approach  ` `        ``else` `        ``{  ` `            ``return` `((n + 1) / 2 + m - 1) / m * m;  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args) ` `    ``{  ` `        ``int` `n = 17, m = 4;  ` `        ``int` `ans = minsteps(n, m);  ` `        ``Console.WriteLine(ans);  ` `    ``}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```12
```

Time Cmplexity: O(1)

My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.