Maximum Strong Pair XOR I — LeetCode 2932 Python Solution

EasyBit ManipulationTrieArrayHash TableSliding Window
Problem
#2932
Pattern
Trie
Reading time
2 min

The 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.

Example

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).

Python solution

Python
class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
        return max(x ^ y for x in nums for y in nums if abs(x - y) <= min(x, y))

Complexity

MeasureComplexity
TimeO(n^2), where n is the length of the array nums
SpaceO(1) auxiliary

Pattern: Trie

Store a set of words by their shared prefixes so lookups cost the length of the word. LeetCode 2932. Maximum Strong Pair XOR I is filed here because LeetCode tags it Trie, which is the vocabulary this hub collects.

The trie guide has the Python template for the pattern and the 49 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2932. Maximum Strong Pair XOR I?
LeetCode 2932. Maximum Strong Pair XOR I is rated Easy on LeetCode.
What is the time complexity of LeetCode 2932. Maximum Strong Pair XOR I?
The Python solution on this page runs in O(n^2), where n is the length of the array nums.
What is the space complexity of LeetCode 2932. Maximum Strong Pair XOR I?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2932. Maximum Strong Pair XOR I cover?
LeetCode 2932. Maximum Strong Pair XOR I is tagged Bit Manipulation, Trie, Array, Hash Table and Sliding Window on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview