Input: nums =[1,2,3,4,5]Output: 7Explanation: There are 11 strong pairs in the array nums:(1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(3,4),(3,5),(4,4),(4,5) and (5,5).The maximum XOR possible from these pairs is3 XOR 4=7.
Input: nums =[10,100]Output: 0Explanation: There are 2 strong pairs in the array nums:(10,10) and (100,100).The maximum XOR possible from these pairs is10 XOR 10=0 since the pair(100,100) also gives 100 XOR 100=0.
Input: nums =[5,6,25,30]Output: 7Explanation: There are 6 strong pairs in the array nums:(5,5),(5,6),(6,6),(25,25),(25,30) and (30,30).The maximum XOR possible from these pairs is25 XOR 30=7 since the only other non-zero XOR value is5 XOR 6=3.
Since the constraints are easy, we can check all possible pairs. For each pair, check if it is a strong pair and compute their XOR. The answer is the maximum XOR among all strong pairs.
classSolution {
public:int maximumStrongPairXor(vector<int>& nums) {
int ans =0;
for (int x : nums) {
for (int y : nums) {
if (abs(x - y) <= min(x, y)) {
ans = max(ans, x ^ y);
}
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcmaximumStrongPairXor(nums []int) int {
ans:=0for_, x:=rangenums {
for_, y:=rangenums {
d:=x-yifd < 0 { d = -d }
m:=xify < m { m = y }
ifd<=m {
ifx^y > ans {
ans = x ^ y }
}
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintmaximumStrongPairXor(int[] nums) {
int ans = 0;
for (int x : nums) {
for (int y : nums) {
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funmaximumStrongPairXor(nums: IntArray): Int {
var ans = 0for (x in nums) for (y in nums) {
if (kotlin.math.abs(x - y) <= minOf(x, y)) {
ans = maxOf(ans, x xor y)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
classSolution:
defmaximumStrongPairXor(self, nums: list[int]) -> int:
ans =0for x in nums:
for y in nums:
if abs(x - y) <= min(x, y):
ans = max(ans, x ^ y)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnmaximum_strong_pair_xor(nums: Vec<i32>) -> i32 {
letmut ans =0;
for&x in&nums {
for&y in&nums {
if (x - y).abs() <= x.min(y) {
ans = ans.max(x ^ y);
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
classSolution {
maximumStrongPairXor(nums: number[]):number {
letans=0;
for (constxofnums) for (constyofnums)
if (Math.abs(x-y) <= Math.min(x, y))
ans= Math.max(ans, x^y);
returnans;
}
}