Minimum Cost to Equalize Arrays Using Swaps
You are given two integer arrays nums1 and nums2 of size n.
You can perform the following two operations any number of times on these two arrays:
- Swap within the same array: Choose two indices
iandj. Then, choose either to swapnums1[i]andnums1[j], ornums2[i]andnums2[j]. This operation is free of charge. - Swap between two arrays: Choose an index
i. Then, swapnums1[i]andnums2[i]. This operation incurs a cost of 1.
Return an integer denoting the minimum cost to make nums1 and nums2 identical. If this is not possible, return -1.
Example 1
Input
nums1 = [10,20], nums2 = [20,10]Output
0Swapping
nums2[0] and nums2[1] is free and makes nums2 equal to [10, 20], so the cost is 0.Example 2
Input
nums1 = [10,10], nums2 = [20,20]Output
1Swapping
nums1[0] with nums2[0] costs 1, then a free swap within nums2 makes both arrays identical.Constraints
- 2 <= n == nums1.length == nums2.length <= 8 * 10^4
- 1 <= nums1[i], nums2[i] <= 8 * 10^4