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 and
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 makenumssimilar totarget. The test cases are generated such that nums can always be similar to target.
Input: nums =[8,12,6], target =[2,14,10] Output:2 Explanation: It is possible to make nums similar to target in two operations:- Choose i =0 and j =2, nums =[10,12,4].- Choose i =1 and j =2, nums =[10,14,2]. It can be shown that 2is the minimum number of operations needed.
Since we can only add/subtract 2, the parity of each element is preserved. We need to match even numbers in nums to even numbers in target, and odd to odd. Sort both arrays by parity, pair them, and sum the differences divided by 2.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:longlong makeSimilar(vector<int>& nums, vector<int>& target) {
vector<int> ne, no, te, to;
for (int x : nums) (x %2? no : ne).push_back(x);
for (int x : target) (x %2? to : te).push_back(x);
sort(ne.begin(), ne.end()); sort(no.begin(), no.end());
sort(te.begin(), te.end()); sort(to.begin(), to.end());
longlong ans =0;
for (int i =0; i < ne.size(); ++i) ans += abs(ne[i] - te[i]) /2;
for (int i =0; i < no.size(); ++i) ans += abs(no[i] - to[i]) /2;
return ans;
}
};
import java.util.*;
classSolution {
publiclongmakeSimilar(int[] nums, int[] target) {
List<Integer> ne =new ArrayList<>(), no =new ArrayList<>();
List<Integer> te =new ArrayList<>(), to =new ArrayList<>();
for (int x : nums) (x % 2 == 0 ? ne : no).add(x);
for (int x : target) (x % 2 == 0 ? te : to).add(x);
Collections.sort(ne); Collections.sort(no);
Collections.sort(te); Collections.sort(to);
long ans = 0;
for (int i = 0; i < ne.size(); ++i) ans += Math.abs(ne.get(i) - te.get(i)) / 2;
for (int i = 0; i < no.size(); ++i) ans += Math.abs(no.get(i) - to.get(i)) / 2;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funmakeSimilar(nums: IntArray, target: IntArray): Long {
val ne = mutableListOf<Int>(); val no = mutableListOf<Int>()
val te = mutableListOf<Int>(); val to = mutableListOf<Int>()
for (x in nums) if (x % 2==0) ne.add(x) else no.add(x)
for (x in target) if (x % 2==0) te.add(x) else to.add(x)
ne.sort(); no.sort(); te.sort(); to.sort()
var ans = 0Lfor (i in ne.indices) ans += kotlin.math.abs(ne[i] - te[i]) / 2for (i in no.indices) ans += kotlin.math.abs(no[i] - to[i]) / 2return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defmakeSimilar(self, nums: list[int], target: list[int]) -> int:
ne = sorted(x for x in nums if x %2==0)
no = sorted(x for x in nums if x %2)
te = sorted(x for x in target if x %2==0)
to = sorted(x for x in target if x %2)
ans = sum(abs(a - b) //2for a, b in zip(ne, te))
ans += sum(abs(a - b) //2for a, b in zip(no, to))
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnmake_similar(nums: Vec<i32>, target: Vec<i32>) -> i64 {
letmut ne: Vec<i32>= nums.iter().filter(|&&x| x %2==0).cloned().collect();
letmut no: Vec<i32>= nums.iter().filter(|&&x| x %2!=0).cloned().collect();
letmut te: Vec<i32>= target.iter().filter(|&&x| x %2==0).cloned().collect();
letmut to: Vec<i32>= target.iter().filter(|&&x| x %2!=0).cloned().collect();
ne.sort(); no.sort(); te.sort(); to.sort();
letmut ans =0i64;
for (a, b) in ne.iter().zip(te.iter()) { ans += (a - b).abs() asi64/2; }
for (a, b) in no.iter().zip(to.iter()) { ans += (a - b).abs() asi64/2; }
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
makeSimilar(nums: number[], target: number[]):number {
constne=nums.filter(x=>x%2===0).sort((a, b) =>a-b);
constno=nums.filter(x=>x%2!==0).sort((a, b) =>a-b);
constte=target.filter(x=>x%2===0).sort((a, b) =>a-b);
constto=target.filter(x=>x%2!==0).sort((a, b) =>a-b);
letans=0;
for (leti=0; i<ne.length; ++i) ans+= Math.abs(ne[i] -te[i]) /2;
for (leti=0; i<no.length; ++i) ans+= Math.abs(no[i] -to[i]) /2;
returnans;
}
}