Skip to main content

Maximum Strong Pair XOR II - Solution & Explanation

HardArrayHash TableBit ManipulationTrie27 min readAsked at: Zscaler
Practice this problem

Problem Statement

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 = [500,520,2500,3000]
Output: 1020
Explanation: There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.

 

Constraints:

  • 1 <= nums.length <= 5 * 104
  • 1 <= nums[i] <= 220 - 1

Approach Overview

Problem Overview: You are given an array of integers and must find the maximum XOR value among all strong pairs. A pair (x, y) is strong if |x - y| ≤ min(x, y). The goal is to efficiently evaluate valid pairs and compute the maximum x ^ y without checking every combination.

Approach 1: Sort and Two Pointer Approach with Trie (O(n log M) time, O(n) space)

Sort the array first. When the array is sorted and x ≤ y, the strong pair condition becomes y - x ≤ x, which simplifies to y ≤ 2x. This property lets you maintain a valid range using a sliding window. Move the right pointer through the array and keep the left pointer adjusted so that all values inside the window satisfy the strong pair constraint.

Within the window, the goal becomes finding the maximum XOR between the current value and any earlier value. A binary Trie stores the bit representation of numbers currently in the window. Each insertion and query takes O(log M), where M is the maximum value in the array (number of bits). For every new element, query the trie for the best XOR partner, update the answer, then insert the number into the trie.

The two-pointer window guarantees that only valid strong pairs remain in the trie. When the left pointer moves forward, remove outdated elements from the trie. Sorting costs O(n log n), and each element is inserted and removed once, leading to overall O(n log M) operations.

Approach 2: Bit Manipulation Optimization (O(n log M) time, O(n) space)

This method builds the XOR result greedily from the most significant bit to the least significant bit using bit manipulation. Instead of a full trie structure, maintain prefix sets of numbers while iteratively testing whether a candidate bit can be set in the answer.

For each bit position, assume the current bit of the answer can be 1 and check if two prefixes exist that produce this XOR. While performing this check, enforce the strong pair condition using the same sorted order and window constraint y ≤ 2x. Hash sets store prefixes efficiently and allow constant-time lookups.

This approach reduces structural overhead compared to an explicit trie while still leveraging bitwise properties of XOR. The complexity remains O(n log M) because each bit position is evaluated across the array.

Recommended for interviews: The sorted two-pointer + trie solution is the most common explanation in interviews. It clearly separates the constraints: sorting enforces the strong pair condition via a sliding window, and the trie efficiently computes maximum XOR. Interviewers expect you to derive the y ≤ 2x transformation and combine it with a bitwise structure. The bit manipulation variant demonstrates deeper understanding of XOR optimization techniques.

Approach 1: Sort and Two Pointer Approach

By sorting the array, the problem of finding pairs becomes simpler. The key observation is that if two numbers are close enough in value, i.e., their difference is less than or equal to their minimum value, they form a strong pair. Sorting the array ensures that once a valid strong pair is found, subsequent numbers are only larger, making them incoming candidates for a strong pair condition. By employing a two-pointer technique, we efficiently navigate through the pairs to find the maximum XOR.

The function first sorts the array, making it easier to identify valid 'strong' pairs using the two-pointer method. After sorting, for each potential starting number, it checks whether subsequent numbers satisfy the strong pair condition. If so, XORs are computed, and the maximum is updated. The process efficiently navigates through possible pairs by breaking inner loops early when conditions aren't met.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) due to the nested loop structure, even though the sorting itself is O(n log n).
Space Complexity: O(1) as no additional data structures proportional to input size are used.

Try this approach in the editor →

Approach 2: Bit Manipulation Optimization Approach

This approach focuses on leveraging properties of bits to directly veer toward results that maximize XOR values. By storing numbers as nodes in a trie structure (radix tree) based on their bit patterns, it's possible to find potential maximum XOR pairs by aligning complementary bits.

The solution constructs a trie with each bit of the numbers. During querying for maximum XOR, it explores complimentary branches of trie nodes to achieve maximum XOR. The trie's essence is to facilitate efficient searching of numbers by their binary representation, which is highly conducive for XOR maximization.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) for insertion and querying in trie.
Space Complexity: O(n * bit_length), essentially storing bits of each number.

Try this approach in the editor →

Approach 3: Sorting + Binary Trie

Observing the inequality |x - y| leq min(x, y), which involves absolute value and minimum value, we can assume x leq y, then we have y - x leq x, that is, y leq 2x. We can enumerate y from small to large, then x must satisfy the inequality y leq 2x.

Therefore, we sort the array nums, and then enumerate y from small to large. We use two pointers to maintain a window so that the elements x in the window satisfy the inequality y leq 2x. We can use a binary trie to maintain the elements in the window, so we can find the maximum XOR value in the window in O(1) time. Each time we add y to the trie, and remove the elements at the left end of the window that do not satisfy the inequality, this can ensure that the elements in the window satisfy the inequality y leq 2x. Then query the maximum XOR value from the trie and update the answer.

The time complexity is O(n times log M), and the space complexity is O(n times log M). Here, n is the length of the array nums, and M is the maximum value in the array nums. In this problem, M = 2^{20}.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sort and Two Pointer Approach

Time Complexity: O(n^2) due to the nested loop structure, even though the sorting itself is O(n log n).
Space Complexity: O(1) as no additional data structures proportional to input size are used.

Bit Manipulation Optimization Approach

Time Complexity: O(n) for insertion and querying in trie.
Space Complexity: O(n * bit_length), essentially storing bits of each number.

Sorting + Binary Trie

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sort + Two Pointers + TrieO(n log M)O(n)Best general solution. Efficiently maintains valid strong pairs and computes maximum XOR using a trie.
Bit Manipulation Prefix OptimizationO(n log M)O(n)Useful when optimizing XOR computation without an explicit trie structure.

Video Solution

Leetcode Weekly contest 371 - Hard - Maximum Strong Pair XOR IIPrakhar Agrawal1,937 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Maximum Strong Pair XOR II easy or hard?
Maximum Strong Pair XOR II is classified as Hard on LeetCode. The difficulty comes from combining multiple techniques: transforming the strong pair constraint, maintaining a sliding window, and using a trie or bit manipulation to compute maximum XOR efficiently.
Maximum Strong Pair XOR II Python/Java solution
Typical implementations use sorting, two pointers, and a binary trie. Python and Java solutions both insert numbers into the trie while scanning the array and query the trie to find the best XOR partner for each element.
How to solve Maximum Strong Pair XOR II in O(n)?
A strict O(n) solution is generally not achievable because computing maximum XOR requires checking bits across values. The closest efficient solution is O(n log M) using a trie or bitwise prefix technique combined with sorting and a sliding window.
What is the best approach for Maximum Strong Pair XOR II?
The most practical approach sorts the array and uses a sliding window with a binary trie. Sorting allows the strong pair condition to be simplified to y ≤ 2x, which lets you maintain valid candidates using two pointers. The trie then finds the maximum XOR for each number in O(log M) time.
Is Maximum Strong Pair XOR II asked at Google/Amazon/Meta?
Problems combining XOR maximization with constraints such as sliding windows or tries appear frequently in interviews at companies like Google, Amazon, and Meta. This problem tests knowledge of bit manipulation, data structures, and constraint optimization.
What data structure is used in Maximum Strong Pair XOR II?
The key data structure is a binary trie (prefix tree) used to store numbers in bit form and compute maximum XOR efficiently. The algorithm also uses sorting and a sliding window to enforce the strong pair condition.
What is the time complexity of Maximum Strong Pair XOR II?
The optimal solution runs in O(n log M) time, where n is the number of elements and M is the maximum value in the array. Sorting takes O(n log n), and each insertion or query in the trie takes O(log M) due to bit traversal.

Ready to solve this problem?

Practice Maximum Strong Pair XOR II with our built-in code editor and test cases.

Practice on FleetCode