Question: Modify an Array the minimum cost to sort the array either ascending or descending along its length. Example n = 6 arr = [ 0

Modify an Array
the minimum cost to sort the array either ascending or descending along its length.
Example
n=6
arr=[0,1,2,5,6,5,7]
Only arr[5]=5 violates the order for an ascending sort.
If the value 5 is increased to 6,the array will be sorted in ascending order. arr' =[0,1,2,5,6,6,7].
The cost is ?arr[5]-arr[5]|=|5-6|=1.
Function Description
Complete the modifyArray function in the editor below.
long modifyArray(vector arr)
modifyArray has the following parameters:
int arr[n]: an array of integers
Returns
long int: an integer that denotes the minimum cost required to make the array ascending (non-decreasing) or descending (non-increasing) along its length.
Constraints
1n103
1arn[i]103
V Input Format for Custom Testing
Input from stdin will be processed as follows and passed to the function.
The first line contains the integer n, the number of elements in arr.
The next n lines each contain an integer, arrij].
Sample Case 0
Sample Input 0
STDIN Function
,arr[] size n=6
arr=[9,8,7,2,3,3]
Sample Output 0
1
Explanation 0
Only arr [3]=2 violates the array descending order.
If the value 2 is increased to 3, the array will be sorted in descending order: arr'=[9,8,7,3,3,3].
The minimum cost to sort it in descending order is ?arr[3]- arr[3]|=|2-3|=1.
 Modify an Array the minimum cost to sort the array either

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!