Minimum Total Cost to Make Arrays Unequal

You are given two 0-indexed integer arrays nums1 and nums2, of equal length n.

In one operation, you can swap the values of any two indices of nums1. The cost of this operation is the sum of the indices.

Find the minimum total cost of performing the given operation any number of times such that nums1[i] != nums2[i] for all 0 <= i <= n - 1 after performing all the operations.

Return the minimum total cost such that nums1 and nums2 satisfy the above condition. In case it is not possible, return -1.

Example 1
Inputnums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]
Output10
By swapping indices 0 and 3, then 1 and 2, then 0 and 4, every index satisfies nums1[i] != nums2[i] with total cost 10, which is minimum.
Example 2
Inputnums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]
Output10
Swapping indices 2 and 3 and then 1 and 4 makes all corresponding values unequal with total cost 10, which is minimum.

Constraints

  • n == nums1.length == nums2.length
  • 1 <= n <= 10^5
  • 1 <= nums1[i], nums2[i] <= n

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate