2449. Minimum Number of Operations to Make Arrays Similar Hard
1/**
2 * [2449] Minimum Number of Operations to Make Arrays Similar
3 *
4 * You are given two positive integer arrays nums and target, of the same length.
5 * In one operation, you can choose any two distinct indices i and j where 0 <= i, j < nums.length and:
6 *
7 * set nums[i] = nums[i] + 2 and
8 * set nums[j] = nums[j] - 2.
9 *
10 * Two arrays are considered to be similar if the frequency of each element is the same.
11 * 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.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [8,12,6], target = [2,14,10]
16 * Output: 2
17 * Explanation: It is possible to make nums similar to target in two operations:
18 * - Choose i = 0 and j = 2, nums = [10,12,4].
19 * - Choose i = 1 and j = 2, nums = [10,14,2].
20 * It can be shown that 2 is the minimum number of operations needed.
21 *
22 * <strong class="example">Example 2:
23 *
24 * Input: nums = [1,2,5], target = [4,1,3]
25 * Output: 1
26 * Explanation: We can make nums similar to target in one operation:
27 * - Choose i = 1 and j = 2, nums = [1,4,3].
28 *
29 * <strong class="example">Example 3:
30 *
31 * Input: nums = [1,1,1,1,1], target = [1,1,1,1,1]
32 * Output: 0
33 * Explanation: The array nums is already similiar to target.
34 *
35 *
36 * Constraints:
37 *
38 * n == nums.length == target.length
39 * 1 <= n <= 10^5
40 * 1 <= nums[i], target[i] <= 10^6
41 * It is possible to make nums similar to target.
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/
47// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn make_similar(nums: Vec<i32>, target: Vec<i32>) -> i64 {
53
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2449() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.