Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 26 Apr, 2021
Geek Week

Given an array arr[] consisting of N integers, the task is to find the minimum number of operations required to make the 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 answer.

Below is the implementation of the above approach:



C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
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




// 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




# 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#




// 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

Javascript




<script>
// Java Script  Program to implement the
// above approach
 
    // Function to return to the minimum
    // number of operations required to
    // make the array non-decreasing
    function getMinOps( arr)
    {
        // Stores the count of operations
        let ans = 0;
        for (let 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
     
        let arr = [ 1, 3, 1, 2, 4 ];
 
        document.write(getMinOps(arr));
 
//contributed by bobby
 
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :