Maximum Strong Pair XOR I - Problem

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: You can pick the same integer twice to form a pair.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,5]
Output: 1
💡 Note: Check all pairs: (1,1), (1,2), (2,2), (2,3), (3,3) are strong pairs. Among these, (2,3) gives maximum XOR: 2^3 = 1
Example 2 — Same Elements
$ Input: nums = [5,6,25,30]
Output: 7
💡 Note: Strong pairs: (5,5), (6,6), (5,6), (25,25), (30,30). Maximum XOR is from (5,6): 5^6 = 3. Wait, let me recalculate: (5,6) gives 5^6=3, but we also need to check other combinations
Example 3 — Single Element
$ Input: nums = [10]
Output: 0
💡 Note: Only one element, so only pair is (10,10). XOR of same numbers is 0: 10^10 = 0

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Maximum Strong Pair XOR: nums = [1,2,3,5]Input Array:1235Strong Pairs Check:(1,1): |1-1|=0 ≤ min(1,1)=1 ✓ XOR=0(1,2): |1-2|=1 ≤ min(1,2)=1 ✓ XOR=3(2,2): |2-2|=0 ≤ min(2,2)=2 ✓ XOR=0(2,3): |2-3|=1 ≤ min(2,3)=2 ✓ XOR=1Maximum XOR = 3
Understanding the Visualization
1
Input Array
Given array with integers
2
Find Strong Pairs
Identify pairs where |x-y| ≤ min(x,y)
3
Calculate XOR
Find maximum XOR among valid pairs
Key Takeaway
🎯 Key Insight: Strong pairs have the constraint |x - y| ≤ min(x, y), which significantly limits the search space
Asked in
Google 25 Amazon 18 Microsoft 15
26.3K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen