Tap here to switch tabs
Problem
Submissions

Problem: Minimum Swaps to Avoid Forbidden Values

Minimum Swaps to Avoid Forbidden Values asks you to rearrange an integer array using swaps so no position matches its forbidden value, returning the fewest swaps or negative one if impossible.

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<n0 \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

Minimum Swaps to Avoid Forbidden Values asks you to rearrange an integer array using swaps so no position matches its forbidden value, returning the fewest swaps or negative one if impossible.

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<n0 \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