Quick Overview

A hard array problem: each operation adds 2 to one element and subtracts 2 from another, and you must find the fewest operations that turn an array into a rearrangement of a target array of the same length. It tests reasoning about what the operation can and cannot change, optimal matching between the arrays, and counts beyond 32-bit range.

Minimum +2/-2 Pair Moves to Make One Array a Rearrangement of Another

Company: ByteDance

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given two arrays of positive integers, `nums` and `target`, of the same length. In one operation you choose two distinct indices `i` and `j` and set `nums[i] = nums[i] + 2` and `nums[j] = nums[j] - 2`. Two arrays are **similar** when every value occurs the same number of times in both, that is, when one is a rearrangement of the other. Return the minimum number of operations needed to make `nums` similar to `target`. The input guarantees that this is possible. ### Function Signature ```python def min_operations_to_make_similar(nums: list[int], target: list[int]) -> int: ``` ### Rules - Each operation changes exactly two different positions of `nums`, one by +2 and the other by -2; `target` never changes. - Only the multiset of values matters at the end; positions do not have to match. - There is no bound on the values during the process; only the final array has to be similar to `target`. ### Constraints - `1 <= n <= 10^5`, where `n = len(nums) = len(target)` - `1 <= nums[i], target[i] <= 10^6` - `nums` can always be made similar to `target` with the operation above. - The answer can exceed `2^31 - 1` (it stays below `2.5 * 10^10`); return it as an exact integer. ### Examples **Example 1** ```text Input: nums = [9, 4, 3], target = [5, 7, 4] Output: 1 ``` Adding 2 to the 3 and subtracting 2 from the 9 gives `[7, 4, 5]`, a rearrangement of `target`. **Example 2** ```text Input: nums = [2, 10, 5], target = [6, 9, 2] Output: 2 ``` Adding 2 to the 5 and subtracting 2 from the 10 gives `[2, 8, 7]`; adding 2 to the 7 and subtracting 2 from the 8 gives `[2, 6, 9]`, a rearrangement of `target`. No single operation is enough. **Example 3** ```text Input: nums = [3, 8], target = [8, 3] Output: 0 ``` `nums` is already a rearrangement of `target`.

Overview: A hard array problem: each operation adds 2 to one element and subtracts 2 from another, and you must find the fewest operations that turn an array into a rearrangement of a target array of the same length. It tests reasoning about what the operation can and cannot change, optimal matching between the arrays, and counts beyond 32-bit range.

Read the full ByteDance Machine Learning Engineer interview experience this question came from

You are given two arrays of positive integers, `nums` and `target`, of the same length `n`. In one operation you pick two distinct indices `i` and `j` and set `nums[i] = nums[i] + 2` and `nums[j] = nums[j] - 2`. The array `target` never changes. Two arrays are **similar** when every value occurs the same number of times in both, that is, when one is a rearrangement of the other. Only the final multiset of values in `nums` matters: positions do not have to line up with `target`. Values are not bounded while you operate; only the final array has to be similar to `target`. Return the minimum number of operations needed to make `nums` similar to `target`. The input guarantees that this is always possible. The answer can exceed `2^31 - 1` (it stays below `2.5 * 10^10`), so return it as an exact integer: `long` in Java, `long long` in C++. **Example 1** ```text Input: nums = [9, 4, 3], target = [5, 7, 4] Output: 1 ``` Add 2 to the 3 and subtract 2 from the 9 to get `[7, 4, 5]`, a rearrangement of `target`. **Example 2** ```text Input: nums = [2, 10, 5], target = [6, 9, 2] Output: 2 ``` Add 2 to the 5 and subtract 2 from the 10 to get `[2, 8, 7]`; then add 2 to the 7 and subtract 2 from the 8 to get `[2, 6, 9]`, a rearrangement of `target`. No single operation is enough. **Constraints** - `1 <= n <= 10^5`, where `n = len(nums) = len(target)` - `1 <= nums[i], target[i] <= 10^6` - `nums` can always be made similar to `target` with the operation above. - The answer can exceed `2^31 - 1` (it stays below `2.5 * 10^10`); return it as an exact integer.

Constraints

  • 1 <= n <= 10^5, where n = len(nums) = len(target)
  • 1 <= nums[i], target[i] <= 10^6
  • nums can always be made similar to target with the operation above.
  • The answer can exceed 2^31 - 1 (it stays below 2.5 * 10^10); return it as an exact integer (long in Java, long long in C++).

Examples

Input: ([9, 4, 3], [5, 7, 4])

Expected Output: 1

Explanation: Source example 1: odd values [3, 9] pair with [5, 7] and even [4] with [4]; total difference 4, so 1 operation.

Input: ([2, 10, 5], [6, 9, 2])

Expected Output: 2

Explanation: Source example 2: odd [5] vs [9] differs by 4 and even [2, 10] vs [2, 6] by 4; total 8, so 2 operations.

Hints

  1. Only the final multiset matters, so think about which value of nums should end up as which value of target.
  2. Ask what a single operation preserves, both about each individual value and about the array as a whole.
  3. Each operation moves exactly 2 units up at one position and 2 units down at another; relate the total amount that has to move to the number of operations.

Fewest Paired +2/-2 Moves to Make Two Arrays Similar

hardByteDanceMachine Learning Engineer

You are given two arrays of positive integers, nums and target, of the same length n. In one operation you pick two distinct indices i and j and set nums[i] = nums[i] + 2 and nums[j] = nums[j] - 2. The array target never changes.

Two arrays are similar when every value occurs the same number of times in both, that is, when one is a rearrangement of the other. Only the final multiset of values in nums matters: positions do not have to line up with target. Values are not bounded while you operate; only the final array has to be similar to target.

Return the minimum number of operations needed to make nums similar to target. The input guarantees that this is always possible.

The answer can exceed 2^31 - 1 (it stays below 2.5 * 10^10), so return it as an exact integer: long in Java, long long in C++.

Example 1

Input:  nums = [9, 4, 3], target = [5, 7, 4]
Output: 1

Add 2 to the 3 and subtract 2 from the 9 to get [7, 4, 5], a rearrangement of target.

Example 2

Input:  nums = [2, 10, 5], target = [6, 9, 2]
Output: 2

Add 2 to the 5 and subtract 2 from the 10 to get [2, 8, 7]; then add 2 to the 7 and subtract 2 from the 8 to get [2, 6, 9], a rearrangement of target. No single operation is enough.

Constraints

  • 1 <= n <= 10^5, where n = len(nums) = len(target)
  • 1 <= nums[i], target[i] <= 10^6
  • nums can always be made similar to target with the operation above.
  • The answer can exceed 2^31 - 1 (it stays below 2.5 * 10^10); return it as an exact integer.

Examples

Example 1
Input
([9, 4, 3], [5, 7, 4])
Output
1
Notes
Source example 1: odd values [3, 9] pair with [5, 7] and even [4] with [4]; total difference 4, so 1 operation.
Example 2
Input
([2, 10, 5], [6, 9, 2])
Output
2
Notes
Source example 2: odd [5] vs [9] differs by 4 and even [2, 10] vs [2, 6] by 4; total 8, so 2 operations.

Constraints

  • •1 <= n <= 10^5, where n = len(nums) = len(target)
  • •1 <= nums[i], target[i] <= 10^6
  • •nums can always be made similar to target with the operation above.
  • •The answer can exceed 2^31 - 1 (it stays below 2.5 * 10^10); return it as an exact integer (long in Java, long long in C++).

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_operations_to_make_similar(nums: list[int], target: list[int]) -> int:
Sample call
min_operations_to_make_similar([9, 4, 3], [5, 7, 4])
Language
Case 1
Input
([9, 4, 3], [5, 7, 4])
Expected
1
Case 2
Input
([2, 10, 5], [6, 9, 2])
Expected
2

Browse More Questions

Show the approach

Approach

Every operation adds or subtracts 2, so each element keeps its parity: an odd value of nums can only finish as an odd value of target, and an even value only as an even one. Split both arrays into their odd and even values and sort each group. Within a group, matching the k-th smallest value of nums with the k-th smallest value of target minimises the total absolute difference (exchange argument: uncrossing two crossed pairs never increases the sum). Let D be that minimum total over both groups. Any sequence of operations ends with some same-parity matching, and the operation preserves the array sum, so the total surplus of the elements that must go down equals the total deficit of the elements that must go up, and both equal half the matching's total difference, at least D / 2. One operation lowers the surplus by at most 2, so at least D / 4 operations are needed. D / 4 is also enough: while any surplus remains under the sorted matching there is one element above its matched value and another below its matched value, and one operation on that pair cuts the surplus by exactly 2; every difference is even, so nothing overshoots. The answer is therefore D / 4, which is always an exact integer. Edge cases: n = 1 (no operation is possible, so the guarantee means nums already equals target and the answer is 0), arrays that are already rearrangements of each other (0), a parity group that is empty in both arrays, and D up to about 10^11, which needs 64-bit accumulation (Java long, C++ long long; JavaScript numbers stay exact below 2^53).

Time complexity:
O(n log n)
Space complexity:
O(n)