Tap here to switch tabs
Problem
Submissions

Problem: Minimum Total Cost to Make Arrays Unequal

Minimum Total Cost to Make Arrays Unequal asks you to swap elements within one array so every position differs from a second array, minimizing index-based swap costs or returning -1 if impossible.

Statement

You are given two 00 indexed integer arrays nums1 and nums2, both of length n.

In a single operation, you can select two indices i and j such that 0≤i,j<n0 \leq i, j < n and swap the elements nums1[i] and nums1[j]. The cost incurred by this swap is i+ji + j.

You may execute this operation any number of times.

Return the minimum total cost required to make nums1[i] != nums2[i] for all 0≤i<n0 \leq i < n. If it is impossible to satisfy this condition, return −1-1.

Constraints:

  • n == nums1.length == nums2.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq nums1[i], nums2[i] ≤\leq n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Total Cost to Make Arrays Unequal

Minimum Total Cost to Make Arrays Unequal asks you to swap elements within one array so every position differs from a second array, minimizing index-based swap costs or returning -1 if impossible.

Statement

You are given two 00 indexed integer arrays nums1 and nums2, both of length n.

In a single operation, you can select two indices i and j such that 0≤i,j<n0 \leq i, j < n and swap the elements nums1[i] and nums1[j]. The cost incurred by this swap is i+ji + j.

You may execute this operation any number of times.

Return the minimum total cost required to make nums1[i] != nums2[i] for all 0≤i<n0 \leq i < n. If it is impossible to satisfy this condition, return −1-1.

Constraints:

  • n == nums1.length == nums2.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq nums1[i], nums2[i] ≤\leq n