Problem · Array
Minimum Swaps
Learn this problemProblem statement
You are given an integer array popularity, where popularity[i] is the unique popularity rating of the ith item in a shop. The shopkeeper wants to arrange the items in decreasing order of popularity from left to right and may swap any two items in one operation.
Return the minimum number of swaps needed to order the items by decreasing popularity.
Function
minimumSwaps(popularity: int[]) → intExamples
Example 1
popularity = [3, 4, 1, 2]return = 2- First swap: Swap the items with ratings
3and4to get[4, 3, 1, 2]. - Second swap: Swap the items with ratings
1and2to get[4, 3, 2, 1].
The array is properly ordered in 2 operations.
Example 2
popularity = [3, 1, 2]return = 1Swap the items with ratings 1 and 2 to get [3, 2, 1]. The array is then ordered in 1 operation.
Constraints
1 ≤ n ≤ 2 × 10^51 ≤ popularity[i] ≤ n
More Akuna Capital problems
- Binary CircuitSeen Jul 2026
- Minimize Malware Spread by Removing a NodeOA · Seen Jul 2026
- Sort Array by FrequencyOA · Seen Jul 2026
- Array Challenge (QR Intern)OA · Seen Jul 2026
- Communications HandlerOA · Seen Jul 2026
- K Smallest SubstringOA · Seen Jul 2026
- Maximum K-Star SumOA · Seen Jul 2026
- Delivery Management SystemOA · Seen Jul 2026