[LeetCode] Maximum Strong Pair XOR I

2932. Maximum Strong Pair XOR I

You are given a 0-indexed integer array nums. A pair of integers x and y is called a strong pair if it satisfies the condition:

  • |x - y| <= min(x, y)

You need to select two integers from nums such that they form a strong pair and their bitwise XOR is the maximum among all strong pairs in the array.

Return the maximum XOR value out of all possible strong pairs in the array nums.

Note that you can pick the same integer twice to form a pair.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int maximumStrongPairXor(vector<int>& nums) {
int res = 0, n = nums.size();
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(abs(nums[i] - nums[j]) <= min(nums[i], nums[j])) {
res = max(res, nums[i] ^ nums[j]);
}
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/11/12/PS/LeetCode/maximum-strong-pair-xor-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.