Minimum Number of Operations to Make Arrays Similar

You are given two positive integer arrays nums and target, of the same length.

In one operation, you can choose any two distinct indices i and j where 0 <= i, j < nums.length and:

  • Set nums[i] = nums[i] + 2.
  • Set nums[j] = nums[j] - 2.

Two arrays are considered to be similar if the frequency of each element is the same.

Return the minimum number of operations required to make nums similar to target. The test cases are generated such that nums can always be similar to target.

Example 1
Inputnums = [8,12,6], target = [2,14,10]
Output2
It is possible to make nums similar to target in two operations, and 2 is the minimum number needed.
Example 2
Inputnums = [1,2,5], target = [4,1,3]
Output1
Choosing i = 1 and j = 2 changes nums to [1,4,3], which is similar to target in one operation.

Constraints

  • n == nums.length == target.length
  • 1 <= n <= 10^5
  • 1 <= nums[i], target[i] <= 10^6
  • It is possible to make nums similar to target.

Asked at 2 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