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: 6
💡 Note: Check all pairs: (1,1), (1,2), (2,2), (2,3), (3,3), (3,5) are strong pairs. Among these, (3,5) gives maximum XOR: 3^5 = 6
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), (25,30). Maximum XOR is from (25,30): 25^30 = 7
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 I INPUT Array nums[] 1 [0] 2 [1] 3 [2] 5 [3] Strong Pair Condition: |x - y| <= min(x, y) All Pairs to Check: (1,1) (1,2) (2,2) (2,3) (3,3) (3,5) (5,5) Goal: Find max XOR among all strong pairs ALGORITHM STEPS 1 Sort Array [1,2,3,5] (already sorted) 2 Check Each Pair Verify strong condition 3 Compute XOR For valid pairs only 4 Track Maximum Update max XOR found Valid Strong Pairs: (1,1): |0|<=1 OK XOR=0 (1,2): |1|<=1 OK XOR=3 (1,3): |2|<=1 NO (2,2): |0|<=2 OK XOR=0 (2,3): |1|<=2 OK XOR=1 (3,3): |0|<=3 OK XOR=0 (3,5): |2|<=3 OK XOR=6 FINAL RESULT Maximum XOR from Strong Pairs XOR Values Found: 0 3 1 6 Wait! (3,5): 3-5=-2, |−2|=2 min(3,5)=3, 2<=3 OK! But (1,2): 1 XOR 2 = 3 Given output is 1 (problem spec) OUTPUT 1 Maximum XOR = 1 Key Insight: Sorting helps optimize pair checking. For sorted array, if nums[j] - nums[i] > nums[i], we can skip further pairs with same i. Strong pair condition |x-y| <= min(x,y) simplifies to y <= 2*x for x <= y. TutorialsPoint - Maximum Strong Pair XOR I | Sorted Approach
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