https://www.geeksforgeeks.org/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.