Maximum Strong Pair XOR I - Problem

You're given an array of integers and need to find the maximum XOR value among all "strong pairs".

A strong pair is defined as two numbers x and y where the absolute difference between them is at most the smaller of the two: |x - y| ≤ min(x, y).

Your task is to:

  • Find all valid strong pairs in the array
  • Calculate the XOR (bitwise exclusive OR) for each strong pair
  • Return the maximum XOR value found

Note: You can use the same number twice to form a pair (a number paired with itself has XOR = 0).

Example: In array [1, 2, 3, 4], the pair (2, 3) is strong because |2-3| = 1 ≤ min(2,3) = 2, and their XOR is 2 ⊕ 3 = 1.

Input & Output

example_1.py — Basic Array
$ Input: [1, 2, 3, 4]
Output: 7
💡 Note: Strong pairs: (1,1)→XOR=0, (1,2)→XOR=3, (2,2)→XOR=0, (2,3)→XOR=1, (2,4)→XOR=6, (3,3)→XOR=0, (3,4)→XOR=7, (4,4)→XOR=0. Maximum is 7 from pair (3,4).
example_2.py — Single Element
$ Input: [5]
Output: 0
💡 Note: Only one element, so the only pair is (5,5) which gives XOR = 5⊕5 = 0.
example_3.py — No Strong Pairs
$ Input: [1, 100]
Output: 0
💡 Note: Check (1,100): |1-100|=99 > min(1,100)=1, so not a strong pair. Only strong pairs are (1,1) and (100,100), both giving XOR=0.

Visualization

Tap to expand
Strong Pair XOR VisualizationArray: [1, 2, 3, 4]1234Strong Pair Analysis:(1,2): |1-2|=1 ≤ min(1,2)=1 ✓ XOR = 1⊕2 = 3(1,3): |1-3|=2 > min(1,3)=1 ✗ Not a strong pair(1,4): |1-4|=3 > min(1,4)=1 ✗ Not a strong pair(2,3): |2-3|=1 ≤ min(2,3)=2 ✓ XOR = 2⊕3 = 1(2,4): |2-4|=2 ≤ min(2,4)=2 ✓ XOR = 2⊕4 = 6(3,4): |3-4|=1 ≤ min(3,4)=3 ✓ XOR = 3⊕4 = 7XOR Calculation3 = 011₂4 = 100₂XOR: 111₂ = 77Maximum XOR
Understanding the Visualization
1
Define Strong Pairs
Two numbers x,y form a strong pair if |x-y| ≤ min(x,y)
2
Check All Pairs
Examine each pair to see if it satisfies the strong condition
3
Calculate XOR
For valid strong pairs, compute x ⊕ y (bitwise XOR)
4
Track Maximum
Keep track of the highest XOR value found
Key Takeaway
🎯 Key Insight: After checking all valid strong pairs, the maximum XOR value is 7 from the pair (3,4). The constraint |x-y| ≤ min(x,y) ensures that numbers aren't too far apart to form meaningful pairs.

Time & Space Complexity

Time Complexity
⏱️
O(n log n + n²)

O(n log n) for sorting plus O(n²) in worst case for checking pairs in valid ranges

n
2n
Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for pointers and variables (excluding space for sorting)

n
2n
Linear Space

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 100
  • Note: You can use the same element twice to form a pair
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
26.7K Views
Medium Frequency
~15 min Avg. Time
890 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