cswork / OAOpen practice workspace

infosysOA #4

Obtain Maximum Score Using Minimum Swaps

Estimated HardStatement and editorial — judging is being prepared

Topics (AI-labelled)Union-FindGreedy

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. Function Description 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

Constraints

An unkown urban legendary for now

Example 1

Input:

arr = [4, 1, 2, 9, 3, 6]

Output:

2

Explanation: 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.

Editorial

Loading solution…