# 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 = 2Output:X = 6Explanation:

Taken operations are 2 2 2 2 1 1

X = 6 which is divisible by 2Input:N = 17, M = 4Output:12

**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((minimum_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 <bits/stdc++.h>` `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` |

## Javascript

`<script>` `// javascript 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` `function` `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` `var` `n = 17, m = 4;` `var` `ans = minsteps(n, m);` `document.write(ans);` `// This code contributed by shikhasingrajput` `</script>` |

**Output:**

12

**Time Complexity:** O(1)