# Minimum increment operations to make the array in increasing order

Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array.

Examples:

```Input : a = { 1, 3, 3, 2 }, X = 2
Output : 3
Explanation : Modified array is { 1, 3, 5, 6 }

Input : a = { 3, 5, 6 }, X = 5
Output : 0
```

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

Observation :

Let’s take two numbers a and b. a >= b and convert this into a < b by adding some number X.
so, a < b + k*X
( a – b ) / x < k

so, the minimum possible value of k is ( a – b ) / x + 1.

Approach :

```Iterate over the given array and take two numbers when a[i] >= a[i-1]
and apply above observation.
```

Below is the implementation of the above approach:

 `// CPP program to find minimum moves required ` `// to make the array in increasing order ` `#include ` `using` `namespace` `std; ` ` `  `// function to find minimum moves required ` `// to make the array in increasing order ` `int` `MinimumMoves(``int` `a[], ``int` `n, ``int` `x) ` `{ ` `    ``// to store answer ` `    ``int` `ans = 0; ` ` `  `    ``// iterate over an array ` `    ``for` `(``int` `i = 1; i < n; i++) { ` ` `  `        ``// non- increasing order ` `        ``if` `(a[i] <= a[i - 1]) { ` `            ``int` `p = (a[i - 1] - a[i]) / x + 1; ` ` `  `            ``// add moves to answer ` `            ``ans += p; ` ` `  `            ``// increase the element ` `            ``a[i] += p * x; ` `        ``} ` `    ``} ` ` `  `    ``// return required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 3, 3, 2 }; ` `    ``int` `x = 2; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << MinimumMoves(arr, n, x); ` ` `  `    ``return` `0; ` `} `

 `// Java program to find minimum moves required ` `// to make the array in increasing order ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` `  `class` `GFG{ ` `// function to find minimum moves required ` `// to make the array in increasing order ` `static` `int` `MinimumMoves(``int` `a[], ``int` `n, ``int` `x) ` `{ ` `    ``// to store answer ` `    ``int` `ans = ``0``; ` `  `  `    ``// iterate over an array ` `    ``for` `(``int` `i = ``1``; i < n; i++) { ` `  `  `        ``// non- increasing order ` `        ``if` `(a[i] <= a[i - ``1``]) { ` `            ``int` `p = (a[i - ``1``] - a[i]) / x + ``1``; ` `  `  `            ``// add moves to answer ` `            ``ans += p; ` `  `  `            ``// increase the element ` `            ``a[i] += p * x; ` `        ``} ` `    ``} ` `  `  `    ``// return required answer ` `    ``return` `ans; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``1``, ``3``, ``3``, ``2` `}; ` `    ``int` `x = ``2``; ` `    ``int` `n = arr.length; ` `  `  `    ``System.out.println(MinimumMoves(arr, n, x)); ` `  `  `} ` `} `

 `# Python3 program to find minimum  ` `# moves required to make the array  ` `# in increasing order  ` ` `  `# function to find minimum moves required  ` `# to make the array in increasing order  ` `def` `MinimumMoves(a, n, x) : ` ` `  `    ``# to store answer  ` `    ``ans ``=` `0` ` `  `    ``# iterate over an array ` `    ``for` `i ``in` `range``(``1``, n) : ` ` `  `        ``# non- increasing order ` `        ``if` `a[i] <``=` `a[i ``-` `1``] : ` ` `  `            ``p ``=` `(a[i ``-` `1``] ``-` `a[i]) ``/``/` `x ``+` `1` ` `  `            ``# add moves to answer  ` `            ``ans ``+``=` `p ` ` `  `            ``# increase the element ` `            ``a[i] ``+``=` `p ``*` `x ` ` `  `    ``# return required answer  ` `    ``return` `ans ` `         `  `# Driver code      ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``arr ``=` `[``1``, ``3``, ``3``, ``2``] ` `    ``x ``=` `2` `    ``n ``=` `len``(arr) ` ` `  `    ``print``(MinimumMoves(arr, n, x)) ` ` `  ` `  `# This code is contributed by ANKITRAI1 `

 `// C# program to find minimum moves required  ` `// to make the array in increasing order  ` `using` `System; ` ` `  `class` `GFG { ` `     `  `// function to find minimum moves required  ` `// to make the array in increasing order  ` `static` `int` `MinimumMoves(``int``[] a, ``int` `n, ``int` `x)  ` `{  ` `     `  `    ``// to store answer  ` `    ``int` `ans = 0;  ` `     `  `    ``// iterate over an array  ` `    ``for` `(``int` `i = 1; i < n; i++) {  ` `     `  `        ``// non- increasing order  ` `        ``if` `(a[i] <= a[i - 1]) {  ` `             `  `            ``int` `p = (a[i - 1] - a[i]) / x + 1;  ` `     `  `            ``// add moves to answer  ` `            ``ans += p;  ` `     `  `            ``// increase the element  ` `            ``a[i] += p * x;  ` `        ``}  ` `    ``}  ` `     `  `    ``// return required answer  ` `    ``return` `ans;  ` `}  ` `     `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `     `  `    ``int``[] arr = {1, 3, 3, 2};  ` `    ``int` `x = 2;  ` `    ``int` `n = arr.Length;  ` `     `  `    ``Console.Write(MinimumMoves(arr, n, x));  ` `     `  `}  ` `}  ` ` `  `// This code is contributed by ChitraNayal `

 ` `

Output:
```3
```

pawanasipugmailcom

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 :