Obtain Maximum Score Using Minimum Swaps
Updated
Problem statement
You are given an array arr containing an even number of distinct positive integers. You may swap any two positions in one operation.
After rearrangement, the score is the product of the sums of adjacent pairs:
(arr[0] + arr[1]) * (arr[2] + arr[3]) * ... * (arr[n - 2] + arr[n - 1]).
Return the minimum number of swaps needed to obtain the maximum possible score.
Function
minimumSwapsForMaximumScore(arr: int[]) → intExamples
Example 1
arr = [4, 1, 2, 9, 3, 6]return = 2For positive values, the maximum score is obtained by pairing the smallest value with the largest, the second-smallest with the second-largest, and so on. Here the required pairs are (1, 9), (2, 6), and (3, 4).
- Swap
4and9to obtain[9, 1, 2, 4, 3, 6]. - Swap
4and6to obtain[9, 1, 2, 6, 3, 4].
The adjacent pair sums are then 10, 8, and 7. The maximum score is reached in the minimum possible 2 swaps.
Constraints
arr.lengthis positive and even.- Every value in
arris a positive integer. - All values in
arrare distinct.
More Infosys interview questions
- At Most K Jumps with Quadratic Transition CostOA · Seen Nov 2026
- Hiking Club Fastest PaceOA · Seen Nov 2026
- Lowest Common Ancestor with a Changing RootOA · Seen Nov 2026
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
- Minimum Leaf Path Sum over All RootsOA · Seen Nov 2026
- Split Array to Minimize the Largest SumOA · Seen Nov 2026
- Cherry PickupOA · Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Grid Path with One Letter ChangeOA · Seen Nov 2026
Similar questions at other companies
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OABlackRock interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Minimum Cost of Every K-Capable Model SetTikTok · OATikTok interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Maximize Minimum Deployment DifficultyJP Morgan Chase · OAJP Morgan Chase interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions