Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

=+3. How does an abstract word differ from a concrete word? [LO-4]

Answered: 1 week ago