Problem Β· Greedy
Obtain Maximum Score Using Minimum Swaps
Given an array of N (even) distinct numbers, rearrange the elements to obtain the maximum score. The score is defined as the product of sums of pairs: (A1 + A2) * (A3 + A4) * ... * (A[N-1] + A[N]). You can use swap operations to rearrange the elements. In one swap, you can swap any two numbers. The goal is to obtain the maximum score using the minimum number of swaps.
Complete the function minimumSwapsForMaximumScore in the editor.
minimumSwapsForMaximumScore has the following parameter:
int[] arr: an array of integers
Returns
int: the minimum number of swaps required to obtain the maximum score
Examples
01 Β· Example 1
arr = [4, 1, 2, 9, 3, 6] return = 2
Perform the following swaps to obtain the maximum score:
Swap(4, 9) to get [9, 1, 2, 4, 3, 6]
Swap(4, 6) to get [1, 9, 2, 6, 3, 4]
The score after these swaps is
(1+9) * (2+6) * (3+4). Therefore, the minimum number of swaps required is 2.Constraints
An unkown urban legendary for nowMore infosys problems
public int minimumSwapsForMaximumScore(int[] arr) {
// write your code here
}
arr[4, 1, 2, 9, 3, 6]
expected2
checking account