Minimum range increment operations to Sort an array

Given an array containing N elements. It is allowed to do the below move any number of times on the array:

  • Choose any L and R and increment all numbers in range L to R by 1.

The task is to find the minimum number of such moves required to sort the array in non decreasing order.

Examples:



Input : arr[] = {1, 2, 3, 4}
Output : 0
The array is already sorted

Input : arr[] = {3, 2, 1}
Output : 2
Step 1: L=1 and R=2 (0-based)
Step 2: L=2 and R=2
Resultant array [3, 3, 3]

Consider a sorted array, incrementing all elements of the array would still result in a sorted array.

So the idea is to traverse the elements of the array from right starting from the last index and keeping track of the minimum element. If at any point, the order of element is found to be increasing calculate the number of moves by subtracting the min element on right from current element.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum range
// increments to sort an array
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find minimum range
// increments to sort an array
int minMovesToSort(int arr[], int n)
{
    int moves = 0;
  
    int i, mn = arr[n - 1];
  
    for (i = n - 2; i >= 0; i--) {
  
        // If current element is found greater than
        // last element
        // Increment all terms in
        // range i+1 to n-1
        if (arr[i] > mn)
            moves += arr[i] - mn;
  
        mn = arr[i]; // Minimum in range i to n-1
    }
  
    return moves;
}
  
// Driver Code
int main()
{
    int arr[] = { 3, 5, 2, 8, 4 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << minMovesToSort(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum range
// increments to sort an array
  
  
import java.io.*;
  
class GFG {
  
// Function to find minimum range
// increments to sort an array
static int minMovesToSort(int arr[], int n)
{
    int moves = 0;
  
    int i, mn = arr[n - 1];
  
    for (i = n - 2; i >= 0; i--) {
  
        // If current element is found greater than
        // last element
        // Increment all terms in
        // range i+1 to n-1
        if (arr[i] > mn)
            moves += arr[i] - mn;
  
        mn = arr[i]; // Minimum in range i to n-1
    }
  
    return moves;
}
  
// Driver Code
  
  
    public static void main (String[] args) {
        int arr[] = { 3, 5, 2, 8, 4 };
  
    int n = arr.length;
  
    System.out.println( minMovesToSort(arr, n));
    }
}
// This code is contributed by anuj_67..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find minimum range
# increments to sort an array
  
# Function to find minimum range
# increments to sort an array
def minMovesToSort(arr, n) :
  
    moves = 0
  
    mn = arr[n - 1]
      
    for i in range(n - 1, -1, -1) :
  
        # If current element is found 
        # greater than last element
        # Increment all terms in
        # range i+1 to n-1
        if (arr[i] > mn) :
            moves += arr[i] - mn
  
        mn = arr[i] # Minimum in range i to n-1
      
    return moves
  
# Driver Code
if __name__ == "__main__" :
  
    arr = [ 3, 5, 2, 8, 4 ]
  
    n = len(arr)
  
    print(minMovesToSort(arr, n))
  
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find minimum range 
// increments to sort an array 
using System;
  
class GFG
{
// Function to find minimum range 
// increments to sort an array 
static int minMovesToSort(int []arr,
                          int n) 
    int moves = 0; 
  
    int i, mn = arr[n - 1]; 
  
    for (i = n - 2; i >= 0; i--) 
    
  
        // If current element is found 
        // greater than last element 
        // Increment all terms in 
        // range i+1 to n-1 
        if (arr[i] > mn) 
            moves += arr[i] - mn; 
  
        mn = arr[i]; // Minimum in range 
                     // i to n-1 
    
    return moves; 
  
// Driver Code 
static public void Main ()
{
    int []arr = { 3, 5, 2, 8, 4 }; 
    int n = arr.Length; 
      
    Console.WriteLine(minMovesToSort(arr, n)); 
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum range
// increments to sort an array
  
// Function to find minimum range
// increments to sort an array
function minMovesToSort($arr, $n)
{
    $moves = 0;
  
    $mn = $arr[$n - 1];
  
    for ($i = $n - 2; $i >= 0; $i--)
    {
  
        // If current element is found 
        // greater than last element
        // Increment all terms in
        // range i+1 to n-1
        if ($arr[$i] > $mn)
            $moves += $arr[$i] - $mn;
  
        $mn = $arr[$i]; // Minimum in range i to n-1
    }
  
    return $moves;
}
  
// Driver Code
$arr = array(3, 5, 2, 8, 4 );
  
$n = sizeof($arr);
  
echo minMovesToSort($arr, $n);
  
// This code is contributed 
// by Akanksha Rai
?>

chevron_right


Output:

7

Time Complexity: O(N)



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.