Given an array a, your task is to convert it into a non-increasing form such that we can either increment or decrement the array value by 1 in minimum changes possible.

Examples :

Input : a[] = {3, 1, 2, 1} Output : 1 Explanation : We can convert the array into 3 1 1 1 by changing 3rd element of array i.e. 2 into its previous integer 1 in one step hence only one step is required. Input : a[] = {3, 1, 5, 1} Output : 4 We need to decrease 5 to 1 to make array sorted in non-increasing order. Input : a[] = {1, 5, 5, 5} Output : 4 We need to increase 1 to 5.

**Brute-Force approach: ** We consider both possibilities for every element and find the minimum of two possibilities.

**Efficient Approach :** Calculate the sum of absolute differences between the final array elements and the current array elements. Thus, the answer will be the sum of the difference between the ith element and the smallest element occurred until then. For this, we can maintain a min-heap to find the smallest element encountered till then. In the min-priority queue, we will put the elements and new elements are compared with the previous minimum. If new minimum is found we will update it, this is done because each of the next element which is coming should be smaller than the current minimum element found till now. Here, we calculate the difference so that we can get how much we have to change the current number so that it will be equal or less than previous numbers encountered till. Lastly, the sum of all these difference will be our answer as this will give the final value up to which we have to change the elements.

Below is implementation of the above approach

## C++

`// CPP code to count the change required to ` `// convert the array into non-increasing array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `DecreasingArray(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `sum = 0, dif = 0; ` ` ` ` ` `// min heap ` ` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `> > pq; ` ` ` ` ` `// Here in the loop we will ` ` ` `// check that whether the upcoming ` ` ` `// element of array is less than top ` ` ` `// of priority queue. If yes then we ` ` ` `// calculate the difference. After ` ` ` `// that we will remove that element ` ` ` `// and push the current element in ` ` ` `// queue. And the sum is incremented ` ` ` `// by the value of difference ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(!pq.empty() && pq.top() < a[i]) { ` ` ` `dif = a[i] - pq.top(); ` ` ` `sum += dif; ` ` ` `pq.pop(); ` ` ` `pq.push(a[i]); ` ` ` `} ` ` ` `pq.push(a[i]); ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 3, 1, 2, 1 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << DecreasingArray(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to count the change required to ` `// convert the array into non-increasing array ` `import` `java.util.PriorityQueue; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `DecreasingArray(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `, dif = ` `0` `; ` ` ` ` ` `PriorityQueue<Integer> pq = ` `new` `PriorityQueue<>(); ` ` ` ` ` `// Here in the loop we will ` ` ` `// check that whether the upcoming ` ` ` `// element of array is less than top ` ` ` `// of priority queue. If yes then we ` ` ` `// calculate the difference. After ` ` ` `// that we will remove that element ` ` ` `// and push the current element in ` ` ` `// queue. And the sum is incremented ` ` ` `// by the value of difference ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(!pq.isEmpty() && pq.element() < a[i]) ` ` ` `{ ` ` ` `dif = a[i] - pq.element(); ` ` ` `sum += dif; ` ` ` `pq.remove(); ` ` ` `pq.add(a[i]); ` ` ` `} ` ` ` `pq.add(a[i]); ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `[] a = {` `3` `, ` `1` `, ` `2` `, ` `1` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` ` ` `System.out.println(DecreasingArray(a, n)); ` ` ` `} ` `} ` ` ` `// This Code is contributed by sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity: ****O(n log(n))**

**Space Complexity:** **O(n)**

Also see : Convert to strictly increasing array with minimum changes.

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.

## Recommended Posts:

- Minimum cost to make array size 1 by removing larger of pairs
- Minimum operations to make GCD of array a multiple of k
- Make array elements equal in Minimum Steps
- Minimum operations required to make all Array elements divisible by K
- Minimum number of changes required to make the given array an AP
- Minimum array insertions required to make consecutive difference <= K
- Minimum Increment / decrement to make array elements equal
- Minimum number of operations on an array to make all elements 0
- Minimum number of elements that should be removed to make the array good
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum deletions required to make GCD of the array equal to 1
- Make all the array elements odd with minimum operations of given type
- Minimum increment or decrement operations required to make the array sorted
- Minimum elements to be removed from the ends to make the array sorted
- Minimum changes required to make all element in an array equal
- Minimum number of steps to make all elements of array equal
- Minimum cost to make an Array a permutation of first N natural numbers
- Minimum operations to make all elements equal using the second array
- Minimum insertions to make XOR of an Array equal to half of its sum
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing

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.