Question: Consider an array of n distinct integers, A = [a 0 ,a 1 ,....,a n-1 ]. You can swap any two elements of the array

Consider an array of n distinct integers, A = [a0,a1,....,an-1]. You can swap any two elements of the array any number of times.

An array is beautiful if the sum of |ai-ai-1| among 0 < i < n is minimal possible(after, possibly, performing some swaps).

Given the array A, find and print the minimum number of swaps that should be preformed to make the array beautiful.

Program to be written in Java.

Input Format:

The first line contains a single integer, n, denoting the number of elements in the array A.

The second line contains n space-separated integers describing the respective distinct values of a0,a1,....,an-1

Constraints:

- 1<= n <= 105

- 1 <= ai <= 2*109

Output Format:

Print the minimum number of swaps that should be preformed in order to make the array beautiful

Sample Input:

4

2 5 3 1

Sample Output:

2

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!