Maximum Strong Pair XOR II - Problem
Maximum Strong Pair XOR II is a challenging bit manipulation problem that combines mathematical constraints with advanced data structures.

You're given a 0-indexed integer array nums. Two integers x and y form a "strong pair" if they satisfy the condition: |x - y| <= min(x, y).

Your goal is to find two integers from nums that form a strong pair and have the maximum possible XOR value among all valid strong pairs. Note that you can select the same element twice to form a pair.

Example: If nums = [1, 2, 3, 4], the pair (3, 4) is strong because |3 - 4| = 1 <= min(3, 4) = 3, and their XOR is 3 ^ 4 = 7.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1, 2, 3, 4]
› Output: 7
šŸ’” Note: The strong pairs are (1,1), (1,2), (2,2), (2,3), (2,4), (3,3), (3,4), (4,4). Among these, (3,4) gives the maximum XOR: 3 ^ 4 = 7.
example_2.py — Single Element
$ Input: nums = [5]
› Output: 0
šŸ’” Note: Only one element exists, so we can only pair it with itself: (5,5). The XOR of identical elements is always 0.
example_3.py — No Valid Pairs Except Self
$ Input: nums = [1, 10, 100]
› Output: 0
šŸ’” Note: The only strong pairs are (1,1), (10,10), and (100,100) since other combinations don't satisfy |x-y| <= min(x,y). All self-pairs have XOR = 0.

Visualization

Tap to expand
Maximum Strong Pair XOR - Complete SolutionStrong Pair Definition|x - y| ≤ min(x, y)Example: |3 - 4| = 1 ≤ min(3,4) = 3 āœ“Mathematical Insight:For x ≤ y: y ≤ 2xEnables sliding window!Algorithm Steps1Sort array: [1,2,3,4]2For each x, find range [x, 2x]3Build trie with candidates4Query trie for max XORExample Walkthrough: [1,2,3,4]x = 2, range = [2, 4]234Binary: 2=010, 3=011, 4=100Max XOR: 2^4 = 010^100 = 110 = 6Trie Structure VisualizationRoot01234šŸŽÆ Complete Algorithm Summary• Time: O(n² log C) - where C is max element value• Space: O(n log C) - for trie storage and sorting• Key Insight: Strong pair ⟺ y ≤ 2x (when x ≤ y)• Optimization: Trie for efficient XOR maximization
Understanding the Visualization
1
Understand Strong Pairs
Two numbers form a strong pair if their difference doesn't exceed the smaller number
2
Mathematical Insight
For x ≤ y, strong pair condition becomes y ≤ 2x, enabling sliding window
3
Trie Optimization
Use binary trie to efficiently find maximum XOR among valid candidates
4
Sliding Window
Process elements in sorted order, maintaining window of valid pairs
Key Takeaway
šŸŽÆ Key Insight: The constraint |x-y| ≤ min(x,y) for strong pairs, when combined with sorting, enables a sliding window approach where we only need to check elements in range [x, 2x]. Using a trie data structure allows us to efficiently find the maximum XOR among valid candidates.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

We check all n*(n+1)/2 pairs in the worst case

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ nums.length ≤ 5 * 104
  • 1 ≤ nums[i] ≤ 220
  • Strong pair condition: |x - y| ≤ min(x, y)
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.7K Views
Medium Frequency
~35 min Avg. Time
1.8K 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