Maximum Strong Pair XOR I — LeetCode 2932 Python Solution
- Problem
- #2932
- Pattern
- Trie
- Reading time
- 2 min
- Source
- leetcode.com
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
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
| Measure | Complexity |
|---|---|
| Time | O(n^2), where n is the length of the array nums |
| Space | O(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.