Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: 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 is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [5,6,25,30]
Output: 7
Explanation: 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 is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation · Trie · Sliding Window
[1,2,3,4,5]
[10,100]
[5,6,25,30]
maximum-xor-of-two-numbers-in-an-array)maximum-xor-with-an-element-from-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2932: Maximum Strong Pair XOR I
class Solution {
public int maximumStrongPairXor(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;
}
}
// Accepted solution for LeetCode #2932: Maximum Strong Pair XOR I
func maximumStrongPairXor(nums []int) (ans int) {
for _, x := range nums {
for _, y := range nums {
if abs(x-y) <= min(x, y) {
ans = max(ans, x^y)
}
}
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2932: Maximum Strong Pair XOR I
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
return max(x ^ y for x in nums for y in nums if abs(x - y) <= min(x, y))
// Accepted solution for LeetCode #2932: Maximum Strong Pair XOR I
fn maximum_strong_pair_xor(nums: Vec<i32>) -> i32 {
let mut ret = 0;
let len = nums.len();
for i in 0..len {
for j in i..len {
if (nums[i] - nums[j]).abs() <= std::cmp::min(nums[i], nums[j]) {
ret = std::cmp::max(ret, nums[i] ^ nums[j]);
}
}
}
ret
}
fn main() {
let nums = vec![1, 2, 3, 4, 5];
let ret = maximum_strong_pair_xor(nums);
println!("ret={ret}");
}
#[test]
fn test() {
{
let nums = vec![1, 2, 3, 4, 5];
let ret = maximum_strong_pair_xor(nums);
assert_eq!(ret, 7);
}
{
let nums = vec![10, 100];
let ret = maximum_strong_pair_xor(nums);
assert_eq!(ret, 0);
}
{
let nums = vec![5, 6, 25, 30];
let ret = maximum_strong_pair_xor(nums);
assert_eq!(ret, 7);
}
}
// Accepted solution for LeetCode #2932: Maximum Strong Pair XOR I
function maximumStrongPairXor(nums: number[]): number {
let ans = 0;
for (const x of nums) {
for (const y of nums) {
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.