Minimum range increment operations to Sort an array
Last Updated :
22 Sep, 2022
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]
Considering 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++
// 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;
}
Java
// 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..
Python3
# 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
C#
// 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
PHP
<?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
?>
JavaScript
<script>
// Javascript program to find minimum range
// increments to sort an array
// Function to find minimum range
// increments to sort an array
function minMovesToSort(arr, n)
{
var moves = 0;
var 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;
// Minimum in range i to n-1
mn = arr[i];
}
return moves;
}
// Driver Code
var arr = [ 3, 5, 2, 8, 4 ];
var n = arr.length;
document.write(minMovesToSort(arr, n));
// This code is contributed by aashish1995
</script>
Time Complexity: O(N)
Auxiliary space: O(1)
Similar Reads
Operations to Sort an Array in non-decreasing order Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.arr[i] ⤠arr[i+1]) by using two types of operation by performing any numbers of time: You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1]
7 min read
Minimum operations required to sort the array Given an array arr[], the task is to find the minimum operations required to sort the array in increasing order. In one operation, you can set each occurrence of one element to 0. Examples: Input: item[] = [4, 1, 5, 3, 2]Output: 4Explanation: Set arr[0], arr[1], arr[2], arr[3] = 0. Hence, the minimu
7 min read
Minimum increment operations to make the array in increasing order Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array. Examples: Input : a = { 1, 3, 3, 2 }, X = 2 Output : 3 Explanation : Modified array is { 1, 3, 5, 6 } Input : a = { 3, 5, 6 }, X = 5 Output : 0 O
6 min read
Minimum increment or decrement operations required to make the array sorted Given an array arr[] of N integers, the task is to sort the array in non-decreasing order by performing the minimum number of operations. In a single operation, an element of the array can either be incremented or decremented by 1. Print the minimum number of operations required.Examples: Input: arr
15+ min read
Find minimum in an array without using Relational Operators Given an array A[] of non-negative integers, find the minimum in the array without using Relational Operators. Examples: Input : A[] = {2, 3, 1, 4, 5} Output : 1 Input : A[] = {23, 17, 93} Output : 17 We use repeated subtraction to find out the minimum. To find minimum between two numbers, we take a
6 min read