Rearranging Fruits
You have two fruit baskets containing n fruits each. You receive two 0-indexed integer arrays basket1 and basket2 representing the cost of fruit in each basket. You want to make both baskets equal . To do so, you can use the following operation as many times as you want:
-
Choose two indices
iandj, and swap thei^thfruit ofbasket1with thej^thfruit ofbasket2. -
The cost of the swap is
min(basket1[i], basket2[j]).
Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
Produce the minimum cost to make both the baskets equal or -1 if impossible.
Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2]
Output: 1
Explanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1]
Output: -1
Explanation: It can be shown that it is impossible to make both the baskets equal.
Constraints
basket1.length == basket2.length1 <= basket1.length <= 10^51 <= basket1[i], basket2[i] <= 10^9
Did this problem appear in one of your interviews?
Responses stay private.
