Tap here to switch tabs
Problem
Submissions

Problem: Minimum Swaps to Avoid Forbidden Values

Try to solve the Minimum Swaps to Avoid Forbidden Values problem.

Statement

You are given two integer arrays nums and forbidden, each of length n.

In one operation, you can choose any two indices i and j and swap the elements nums[i] and nums[j].

Return the minimum number of swaps needed such that nums[i] \neq forbidden[i] for all indices i where 0≤i<0 \leq i < n. If it is impossible to satisfy this condition for all indices, return −1-1.

Constraints:

  • 1≤1 \leq n ==== nums.length ==== forbidden.length ≤105\leq 10^5

  • 1≤1 \leq nums[i], forbidden[i] ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Swaps to Avoid Forbidden Values

Try to solve the Minimum Swaps to Avoid Forbidden Values problem.

Statement

You are given two integer arrays nums and forbidden, each of length n.

In one operation, you can choose any two indices i and j and swap the elements nums[i] and nums[j].

Return the minimum number of swaps needed such that nums[i] \neq forbidden[i] for all indices i where 0≤i<0 \leq i < n. If it is impossible to satisfy this condition for all indices, return −1-1.

Constraints:

  • 1≤1 \leq n ==== nums.length ==== forbidden.length ≤105\leq 10^5

  • 1≤1 \leq nums[i], forbidden[i] ≤109\leq 10^9