# Minimum number of subtract operation to make an array decreasing

You are given a sequence of numbers arr, arr, …, arr[N – 1] and a positive integer K. In each operation, you may subtract K from any element of the array. You are required to find the minimum number of operations to make the given array decreasing.

An array is called decreasing if for each i: .

Input : N = 4, K = 5, arr[] = {1, 1, 2, 3}
Output : 3

Explanation :
Since arr == arr so no subtraction is required for arr. For arr, since arr > arr (2 > 1) so we have to subtract arr by k and after the one subtraction value of arr is -3 which is less than the value of arr, so number of subtraction required only 1 and now value of arr has been updated by -3.
Similarly for arr, since arr > arr (3 > -3) so for this we have to subtract arr by k two times to make the value of arr lesser than arr, the number of subtraction required 2 and the updated value of arr is -7. Now count total number of subtraction /operation required by adding number of operation on each step and that is = 0+1+2 = 3.

Input : N = 5, K = 2, arr[] = {5, 4, 3, 2, 1}
Output : 0

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

Approach :

```1. Traverse each element of array from 1 to n-1.
2. Check if (arr[i] > arr[i-1]) then
Find noOfSubtraction;
noOfSubtraction = If ( (arr[i] - arr[i-1]) % k == 0 )
then noOfSubtraction++

Modify arr[i];
arr[i] = ```

Below is implementation of above approach :

## CPP

 `// CPP program to make an array decreasing ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count minimum no of operation ` `int` `min_noOf_operation(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `noOfSubtraction; ` `    ``int` `res = 0; ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``noOfSubtraction = 0; ` ` `  `        ``if` `(arr[i] > arr[i - 1]) { ` ` `  `            ``// Count how many times we have to subtract. ` `            ``noOfSubtraction = (arr[i] - arr[i - 1]) / k; ` ` `  `            ``// Check an additional subtraction is  ` `            ``// required or not. ` `            ``if` `((arr[i] - arr[i - 1]) % k != 0) ` `                ``noOfSubtraction++; ` ` `  `            ``// Modify the value of arr[i]. ` `            ``arr[i] = arr[i] - k * noOfSubtraction; ` `        ``} ` ` `  `        ``// Count total no of operation/subtraction . ` `        ``res = res + noOfSubtraction; ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 3 }; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 5; ` `    ``cout << min_noOf_operation(arr, N, k) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to make an ` `// array decreasing ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GfG{ ` `     `  `    ``// Function to count minimum no of operation ` `    ``public` `static` `int` `min_noOf_operation(``int` `arr[],  ` `                                      ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `noOfSubtraction; ` `        ``int` `res = ``0``; ` `         `  `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``noOfSubtraction = ``0``; ` ` `  `            ``if` `(arr[i] > arr[i - ``1``]) { ` `     `  `                ``// Count how many times  ` `                ``// we have to subtract. ` `                ``noOfSubtraction = (arr[i] - arr[i - ``1``]) / k; ` ` `  `                ``// Check an additional subtraction  ` `                ``// is required or not. ` `                ``if` `((arr[i] - arr[i - ``1``]) % k != ``0``) ` `                    ``noOfSubtraction++; ` ` `  `                ``// Modify the value of arr[i] ` `                ``arr[i] = arr[i] - k * noOfSubtraction; ` `            ``} ` ` `  `            ``// Count total no of subtraction ` `            ``res = res + noOfSubtraction; ` `        ``} ` ` `  `        ``return` `res; ` `    ``} ` `     `  `    ``// driver function ` `    ``public` `static` `void` `main(String argc[]){ ` `        ``int` `arr = { ``1``, ``1``, ``2``, ``3` `}; ` `        ``int` `N = ``4``; ` `        ``int` `k = ``5``; ` `        ``System.out.println(min_noOf_operation(arr, ` `                                           ``N, k));  ` `    ``} ` `     `  `} ` ` `  `/* This code is contributed by Sagar Shukla */`

## Python3

 `# Python program to make an array decreasing ` ` `  `# Function to count minimum no of operation ` `def` `min_noOf_operation(arr, n, k): ` ` `  `    ``res ``=` `0` `    ``for` `i ``in` `range``(``1``,n): ` `        ``noOfSubtraction ``=` `0` ` `  `        ``if` `(arr[i] > arr[i ``-` `1``]): ` ` `  `            ``# Count how many times we have to subtract. ` `            ``noOfSubtraction ``=` `(arr[i] ``-` `arr[i ``-` `1``]) ``/` `k; ` ` `  `            ``# Check an additional subtraction is  ` `            ``# required or not. ` `            ``if` `((arr[i] ``-` `arr[i ``-` `1``]) ``%` `k !``=` `0``): ` `                ``noOfSubtraction``+``=``1` ` `  `            ``# Modify the value of arr[i]. ` `            ``arr[i] ``=` `arr[i] ``-` `k ``*` `noOfSubtraction ` `         `  ` `  `        ``# Count total no of operation/subtraction . ` `        ``res ``=` `res ``+` `noOfSubtraction ` `     `  ` `  `    ``return` `int``(res) ` ` `  ` `  `# Driver Code ` `arr ``=` `[ ``1``, ``1``, ``2``, ``3` `] ` `N ``=` `len``(arr) ` `k ``=` `5` `print``(min_noOf_operation(arr, N, k)) ` ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# program to make an ` `// array decreasing ` `using` `System; ` ` `  `public` `class` `GfG{ ` `     `  `    ``// Function to count minimum no of operation ` `    ``public` `static` `int` `min_noOf_operation(``int` `[]arr,  ` `                                       ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `noOfSubtraction; ` `        ``int` `res = 0; ` `         `  `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``noOfSubtraction = 0; ` ` `  `            ``if` `(arr[i] > arr[i - 1]) { ` `     `  `                ``// Count how many times  ` `                ``// we have to subtract. ` `                ``noOfSubtraction = (arr[i] - arr[i - 1]) / k; ` ` `  `                ``// Check an additional subtraction  ` `                ``// is required or not. ` `                ``if` `((arr[i] - arr[i - 1]) % k != 0) ` `                    ``noOfSubtraction++; ` ` `  `                ``// Modify the value of arr[i] ` `                ``arr[i] = arr[i] - k * noOfSubtraction; ` `            ``} ` ` `  `            ``// Count total no of subtraction ` `            ``res = res + noOfSubtraction; ` `        ``} ` ` `  `        ``return` `res; ` `    ``} ` `     `  `    ``// driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[]arr = { 1, 1, 2, 3 }; ` `        ``int` `N = 4; ` `        ``int` `k = 5; ` `        ``Console.WriteLine(min_noOf_operation(arr, ` `                                        ``N, k));  ` `    ``} ` `     `  `} ` ` `  `// This code is contributed by vt_m  `

## PHP

 ` ``\$arr``[``\$i` `- 1])  ` `        ``{ ` ` `  `            ``// Count how many times we ` `            ``// have to subtract. ` `            ``\$noOfSubtraction` `= (``\$arr``[``\$i``] -  ` `                      ``\$arr``[``\$i` `- 1]) / ``\$k``; ` ` `  `            ``// Check an additional subtraction  ` `            ``// is required or not. ` `            ``if` `((``\$arr``[``\$i``] - ``\$arr``[``\$i` `- 1])  ` `                               ``% ``\$k` `!= 0) ` `                ``\$noOfSubtraction``++; ` ` `  `            ``// Modify the value of arr[i]. ` `            ``\$arr``[``\$i``] = ``\$arr``[``\$i``] - ``\$k` `*  ` `                      ``\$noOfSubtraction``; ` `        ``} ` ` `  `        ``// Count total no of  ` `        ``// operation/subtraction . ` `        ``\$res` `= ``\$res` `+ ``\$noOfSubtraction``; ` `    ``} ` ` `  `    ``return` `floor``(``\$res``); ` `} ` ` `  `    ``// Driver Code ` `    ``\$arr` `= ``array``(1, 1, 2, 3); ` `    ``\$N` `= ``count``(``\$arr``); ` `    ``\$k` `= 5; ` `    ``echo` `min_noOf_operation(``\$arr``, ``\$N``, ``\$k``) ; ` ` `  `// This code is contributed by anuj_67. ` `?> `

Output :

``` 3
```

Time Complexity : 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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : vt_m

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.