LeetCode 2932: Maximum Strong Pair XOR I (Strong Pair Check + XOR Enumeration)
LeetCode 2932ArrayBitwiseEnumerationToday we solve LeetCode 2932 - Maximum Strong Pair XOR I.
Source: https://leetcode.com/problems/maximum-strong-pair-xor-i/
English
Problem Summary
Given an integer array nums, choose a pair (x, y) from the array such that the pair is strong: |x - y| <= min(x, y). Return the maximum value of x XOR y among all strong pairs.
Key Insight
For this "I" version, constraints are small enough for pair enumeration. So the core is not advanced data structure design — it is correct condition checking plus complete traversal of all pairs.
Condition Rewrite
If we let a = min(x, y) and b = max(x, y), then |x - y| = b - a. The strong condition becomes:
b - a <= a ⟺ b <= 2a.
During pair checks, we can simply test either form. In code, Math.abs(x - y) <= Math.min(x, y) is direct and clear.
Algorithm Steps
1) Initialize answer ans = 0.
2) Enumerate all pairs (i, j) with i ≤ j (or all i, j).
3) If pair (nums[i], nums[j]) is strong, compute nums[i] ^ nums[j] and update ans.
4) Return ans.
Complexity Analysis
Time: O(n²).
Space: O(1).
Common Pitfalls
- Forgetting that the same element can pair with itself (which gives XOR 0, but still valid).
- Using only adjacent elements after sorting: that can miss the real maximum XOR.
- Miswriting condition as |x-y| < min(x,y) (strict) instead of <=.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int maximumStrongPairXor(int[] nums) {
int ans = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int x = nums[i], y = nums[j];
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
}
}func maximumStrongPairXor(nums []int) int {
ans := 0
n := len(nums)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
x, y := nums[i], nums[j]
diff := x - y
if diff < 0 {
diff = -diff
}
mn := x
if y < mn {
mn = y
}
if diff <= mn {
v := x ^ y
if v > ans {
ans = v
}
}
}
}
return ans
}class Solution {
public:
int maximumStrongPairXor(vector<int>& nums) {
int ans = 0;
int n = (int)nums.size();
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
int x = nums[i], y = nums[j];
if (abs(x - y) <= min(x, y)) {
ans = max(ans, x ^ y);
}
}
}
return ans;
}
};class Solution:
def maximumStrongPairXor(self, nums: list[int]) -> int:
ans = 0
n = len(nums)
for i in range(n):
for j in range(i, n):
x, y = nums[i], nums[j]
if abs(x - y) <= min(x, y):
ans = max(ans, x ^ y)
return ansvar maximumStrongPairXor = function(nums) {
let ans = 0;
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
const x = nums[i], y = nums[j];
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
};中文
题目概述
给定整数数组 nums,从中选一对 (x, y)。如果满足 |x - y| <= min(x, y),则称其为强数对。返回所有强数对中 x XOR y 的最大值。
核心思路
这道题的 I 版本数据范围较小,直接枚举数对即可。重点在于“强数对条件”不要写错,并且遍历要完整。
条件变形
设 a=min(x,y)、b=max(x,y),则 |x-y|=b-a。条件可写成:
b-a <= a ⟺ b <= 2a。
实现时直接用 abs(x-y) <= min(x,y) 最直观。
算法步骤
1)初始化答案 ans=0。
2)双重循环枚举所有数对(可用 i ≤ j)。
3)若当前数对是强数对,就计算 x ^ y 并更新答案。
4)返回 ans。
复杂度分析
时间复杂度:O(n²)。
空间复杂度:O(1)。
常见陷阱
- 忽略自配对(同一元素和自己配对,异或值为 0,但依然是合法候选)。
- 排序后只看相邻元素会漏解,不能保证异或最大值。
- 把条件误写成严格小于 <,正确是 <=。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int maximumStrongPairXor(int[] nums) {
int ans = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int x = nums[i], y = nums[j];
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
}
}func maximumStrongPairXor(nums []int) int {
ans := 0
n := len(nums)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
x, y := nums[i], nums[j]
diff := x - y
if diff < 0 {
diff = -diff
}
mn := x
if y < mn {
mn = y
}
if diff <= mn {
v := x ^ y
if v > ans {
ans = v
}
}
}
}
return ans
}class Solution {
public:
int maximumStrongPairXor(vector<int>& nums) {
int ans = 0;
int n = (int)nums.size();
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
int x = nums[i], y = nums[j];
if (abs(x - y) <= min(x, y)) {
ans = max(ans, x ^ y);
}
}
}
return ans;
}
};class Solution:
def maximumStrongPairXor(self, nums: list[int]) -> int:
ans = 0
n = len(nums)
for i in range(n):
for j in range(i, n):
x, y = nums[i], nums[j]
if abs(x - y) <= min(x, y):
ans = max(ans, x ^ y)
return ansvar maximumStrongPairXor = function(nums) {
let ans = 0;
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
const x = nums[i], y = nums[j];
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
};
Comments