Company: JPMorgan Chase
Difficulty: easy
Popularity Reorder A shopkeeper in Koxland gives every item in the shop a unique popularity rating. The items are currently arranged left to right in the order given by the array popularity , and the shopkeeper wants them arranged in strictly decreasing popularity from left to right. In one operation the shopkeeper may pick any two positions in the arrangement and swap the items that sit there. The two positions do not have to be adjacent. Determine the minimum number of operations needed to reorder the items correctly. Function minimumSwaps(popularity: int[]) -> int minimumSwaps has the following parameter: int popularity[n] : an array of integers that represents the popularity of each item, in left to right order. Returns int : the minimum number of swap operations needed to arrange the items in strictly decreasing popularity from left to right. Input Format The first line contains a single integer n , the number of items. The second line contains n space-separated integers, popul