Count operations to sort the Array by incrementing each element of increasing subsegment by 1
Last Updated :
02 Mar, 2023
Given an array arr[] of size N, the task is to make array arr[] sorted in non-decreasing order in the minimum number of operations where in one operation, you can choose any non-decreasing sub-segment and increment each element of sub-segment by 1.
Examples:
Input: arr[] = {5, 3, 2, 5}
Output: 3
Explanation: First operation: [5, 3, 2, 5]?[5, 3, 3, 5]
Second operation: [5, 3, 3, 5]?[5, 4, 4, 5]
Third operation: [5, 4, 4, 5]?[5, 5, 5, 5]
Hence, the minimum operations required will be 3
Input: arr[] = {1, 2, 3, 5, 3}
Output: 2
Explanation: First operation: [1, 2, 3, 5, 3] -> [1, 2, 3, 5, 4]
Second operation: [1, 2, 3, 5, 4] -> [1, 2, 3, 5, 5]
Hence, the minimum operations required will be 2
Approach: To solve the problem follow the below idea:
This problem can be solved using a greedy approach. We know that an array is sorted in non-decreasing order if arr[i] <= arr[i+1] for all i. So we can start iterating from back and if any element is found to be greater than the minimum of all its right elements, We must set all the right elements to arr[i] in order to make it increase. The number of operations taken will be the absolute difference between the minimum and the arr[i].
Follow the below steps to implement the idea:
- Start iterating from the back and find the index where arr[i] > minimum on right.
- Increment count by the absolute difference of minimum and arr[i] as in one operation element can only be increased by 1.
- Keep track of the minimum in each step.
- If arr[i] is not greater than the minimum of all its right, update minimum to nums[i].
- Return count.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations(vector< int >& nums, int n)
{
int mini = nums[n - 1];
int cnt = 0;
for ( int i = n - 2; i >= 0; i--) {
if (nums[i] > mini) {
cnt += abs (mini - nums[i]);
mini = nums[i];
}
else {
mini = nums[i];
}
}
return cnt;
}
int main()
{
vector< int > arr = { 5, 3, 2, 5 };
int n = arr.size();
cout << minimumOperations(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int minimumOperations(List<Integer> nums,
int n)
{
int mini = nums.get(n - 1 );
int cnt = 0 ;
for ( int i = n - 2 ; i >= 0 ; i--) {
if (nums.get(i) > mini) {
cnt += Math.abs(mini - nums.get(i));
mini = nums.get(i);
}
else {
mini = nums.get(i);
}
}
return cnt;
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList( 5 , 3 , 2 , 5 );
int n = arr.size();
System.out.println(minimumOperations(arr, n));
}
}
|
Python3
def minimumOperations(nums, n):
mini = nums[n - 1 ]
cnt = 0
for i in range (n - 2 , - 1 , - 1 ):
if nums[i] > mini:
cnt + = abs (mini - nums[i])
mini = nums[i]
else :
mini = nums[i]
return cnt
arr = [ 5 , 3 , 2 , 5 ]
n = len (arr)
print (minimumOperations(arr, n))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int MinimumOperations(List< int > nums,
int n)
{
int mini = nums[n - 1];
int cnt = 0;
for ( int i = n - 2; i >= 0; i--) {
if (nums[i] > mini) {
cnt += Math.Abs(mini - nums[i]);
mini = nums[i];
}
else {
mini = nums[i];
}
}
return cnt;
}
public static void Main( string [] args)
{
List< int > arr = new List< int >() { 5, 3, 2, 5 };
int n = arr.Count;
Console.WriteLine(MinimumOperations(arr, n));
}
}
|
Javascript
function minimumOperations(nums, n) {
let mini = nums[n - 1];
let cnt = 0;
for (let i = n - 2; i >= 0; i--) {
if (nums[i] > mini) {
cnt += Math.abs(mini - nums[i]);
mini = nums[i];
}
else {
mini = nums[i];
}
}
return cnt;
}
const arr = [5, 3, 2, 5];
const n = arr.length;
console.log(minimumOperations(arr, n));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...