2541. Minimum Operations to Make Array Equal II Medium
1/**
2 * [2541] Minimum Operations to Make Array Equal II
3 *
4 * You are given two integer arrays nums1 and nums2 of equal length n and an integer k. You can perform the following operation on nums1:
5 *
6 * Choose two indexes i and j and increment nums1[i] by k and decrement nums1[j] by k. In other words, nums1[i] = nums1[i] + k and nums1[j] = nums1[j] - k.
7 *
8 * nums1 is said to be equal to nums2 if for all indices i such that 0 <= i < n, nums1[i] == nums2[i].
9 * Return the minimum number of operations required to make nums1 equal to nums2. If it is impossible to make them equal, return -1.
10 *
11 * <strong class="example">Example 1:
12 *
13 * Input: nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
14 * Output: 2
15 * Explanation: In 2 operations, we can transform nums1 to nums2.
16 * 1^st operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].
17 * 2^nd operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1].
18 * One can prove that it is impossible to make arrays equal in fewer operations.
19 * <strong class="example">Example 2:
20 *
21 * Input: nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
22 * Output: -1
23 * Explanation: It can be proved that it is impossible to make the two arrays equal.
24 *
25 *
26 * Constraints:
27 *
28 * n == nums1.length == nums2.length
29 * 2 <= n <= 10^5
30 * 0 <= nums1[i], nums2[j] <= 10^9
31 * 0 <= k <= 10^5
32 *
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/
37// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42 pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {
43
44 }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn test_2541() {
55 }
56}
57
Back
© 2025 bowen.ge All Rights Reserved.