Two elements in array whose xor is maximum
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The XOR (exclusive OR) operation is a fundamental bitwise operation that plays a crucial role in computer science, particularly in cryptography, error detection, and digital logic design. Given an array of integers, a common problem is finding two elements whose XOR yields the maximum possible value. This article will explore the technical aspects of this problem, provide examples, and summarize key points in tabular form.
Understanding XOR Operation
The XOR operation is a binary operation that takes two bits and returns 1 if the bits are different, and 0 if they are the same. Formally, the XOR operation between two bits is defined as follows:
For integers, the XOR operation is performed bit by bit. For instance, the XOR of two integers 6 (110 in binary) and 3 (011 in binary) is 5 (101 in binary).
Problem Statement
Given an array of integers, the goal is to find two elements, say a and b, such that their XOR a ⊕ b is maximum. This problem can be addressed using straightforward and optimized approaches.
Technical Approach
Naive Approach
A simple solution is to use a nested loop to calculate the XOR for every possible pair of elements in the array. The time complexity of this approach is , which is not efficient for large arrays.
- Naive Approach: time complexity due to the double loop.
- Optimized Approach: time complexity due to single insertion and lookup of each number in the Trie.
- Naive Approach: Examine all pairs:
3 ⊕ 10 = 93 ⊕ 5 = 63 ⊕ 25 = 26- ...
- Maximum XOR is
28, obtained from5 ⊕ 25.
- Optimized Approach: Same conclusion as above using the Trie.
- Cryptography: XOR is a fundamental operation in many cryptographic algorithms due to its reversible nature.
- Error Detection: One-time pads and XOR-based parity checks can help identify errors by manipulating bits.
- Digital Logic: Hardware implementations often use XOR gates for circuits which require toggling states.
Related reading
- Two player grid traversal game
- Two salesmen - one always visits the nearest neighbour, the other the farthest
- UIImage - implementing an auto levels algorithm
- Ukkonen's suffix tree algorithm in plain English
- Type List vs type ArrayList in Java
- type mismatch error, expected type LIST for querying a one-to-many relationship in AppSync
- Two single-column indexes vs one two-column index in MySQL?
- Types in MySQL BigInt20 vs Int20

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.