# Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing

Given an array arr[] consisting of N integers, the task is to find the minimum number of operations required to make array non-decreasing, where, each operation involves incrementing all elements of a non-decreasing subarray from the given array by 1.

Examples:

Input: arr[] = {1, 3, 1, 2, 4}
Output:
Explanation:
Operation 1: Incrementing arr[2] modifies array to {1, 3, 2, 2, 4}
Operation 2: Incrementing subarray {arr[2], arr[3]} modifies array to {1, 3, 3, 3, 4}
Therefore, the final array is non-decreasing.

Input: arr[] = {1, 3, 5, 10}
Output:
Explanation: The array is already non-decreasing.

Approach: Follow the steps below to solve the problem:

• If the array is already a non-decreasing array, then no changes required.
• Otherwise, for any index i where 0 ≤ i < N, if arr[i] > arr[i+1], add the difference to ans.
• Finally print ans as the answer.

Below is the implementation of the above approach:

 `// C++ program to implement ` `// the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return to the minimum ` `// number of operations required to ` `// make the array non-decreasing ` `int` `getMinOps(``int` `arr[], ``int` `n) ` `{ ` `     `  `    ``// Stores the count of operations ` `    ``int` `ans = 0; ` `    ``for``(``int` `i = 0; i < n - 1; i++) ` `    ``{ ` ` `  `        ``// If arr[i] > arr[i + 1], add ` `        ``// arr[i] - arr[i + 1] to the answer ` `        ``// Otherwise, add 0 ` `        ``ans += max(arr[i] - arr[i + 1], 0); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 3, 1, 2, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `     `  `    ``cout << (getMinOps(arr, n)); ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

 `// Java Program to implement the ` `// above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to return to the minimum ` `    ``// number of operations required to ` `    ``// make the array non-decreasing ` `    ``public` `static` `int` `getMinOps(``int``[] arr) ` `    ``{ ` `        ``// Stores the count of operations ` `        ``int` `ans = ``0``; ` `        ``for` `(``int` `i = ``0``; i < arr.length - ``1``; i++) { ` ` `  `            ``// If arr[i] > arr[i + 1], add ` `            ``// arr[i] - arr[i + 1] to the answer ` `            ``// Otherwise, add 0 ` `            ``ans += Math.max(arr[i] - arr[i + ``1``], ``0``); ` `        ``} ` ` `  `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { ``1``, ``3``, ``1``, ``2``, ``4` `}; ` ` `  `        ``System.out.println(getMinOps(arr)); ` `    ``} ` `} `

 `# Python3 program to implement ` `# the above approach ` ` `  `# Function to return to the minimum  ` `# number of operations required to ` `# make the array non-decreasing ` `def` `getMinOps(arr): ` ` `  `    ``# Stores the count of operations ` `    ``ans ``=` `0` `     `  `    ``for` `i ``in` `range``(``len``(arr) ``-` `1``): ` ` `  `        ``# If arr[i] > arr[i + 1], add ` `        ``# arr[i] - arr[i + 1] to the answer ` `        ``# Otherwise, add 0 ` `        ``ans ``+``=` `max``(arr[i] ``-` `arr[i ``+` `1``], ``0``) ` ` `  `    ``return` `ans ` ` `  `# Driver Code ` ` `  `# Given array arr[] ` `arr ``=` `[ ``1``, ``3``, ``1``, ``2``, ``4` `] ` ` `  `# Function call ` `print``(getMinOps(arr)) ` ` `  `# This code is contributed by Shivam Singh`

 `// C# Program to implement the ` `// above approach ` `using` `System; ` `class` `GFG ` `{ ` ` `  `  ``// Function to return to the minimum ` `  ``// number of operations required to ` `  ``// make the array non-decreasing ` `  ``public` `static` `int` `getMinOps(``int``[] arr) ` `  ``{ ` `    ``// Stores the count of operations ` `    ``int` `ans = 0; ` `    ``for` `(``int` `i = 0; i < arr.Length - 1; i++)  ` `    ``{ ` ` `  `      ``// If arr[i] > arr[i + 1], add ` `      ``// arr[i] - arr[i + 1] to the answer ` `      ``// Otherwise, add 0 ` `      ``ans += Math.Max(arr[i] - arr[i + 1], 0); ` `    ``} ` `    ``return` `ans; ` `  ``} ` ` `  `  ``// Driver Code ` `  ``public` `static` `void` `Main(String[] args) ` `  ``{ ` `    ``int``[] arr = { 1, 3, 1, 2, 4 }; ` ` `  `    ``Console.WriteLine(getMinOps(arr)); ` `  ``} ` `} ` ` `  `// This code is contributed by Amit Katiyar`

Output:
```2
```

Time Complexity: O(N)
Auxiliary Space: O(1)

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.

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.

Article Tags :