Prem-Kumar-Dev / hacktoberfest2024-project

Welcome to the Hacktoberfest 2024 beginner coding problems repository! This project is designed for those who want to contribute to open-source while solving basic coding challenges. Whether you are a first-time contributor or looking to practice programming, you're in the right place.
MIT License
8 stars 77 forks source link

DSA Problem: Maximum XOR of Two Numbers in an Array #44

Open reynyx opened 1 month ago

reynyx commented 1 month ago

Problem Description:

Given an integer array nums, return the maximum result of nums[i] XOR nums[j], where 0 <= i <= j < n.

The XOR operation produces a 1 when the bits of two numbers differ and a 0 when they are the same. Thus, to maximize the XOR result, we want to pair bits that are as different as possible.

To find the maximum XOR of two numbers in an array, we utilize a binary Trie data structure to store the binary representations of the numbers. The XOR operation yields a 1 for differing bits, so we aim to maximize the difference in bits. We insert each number into the Trie bit by bit, from the most significant to the least significant bit.

For each number, we traverse the Trie to identify the number that will produce the highest XOR by following the opposite path for each bit. This method allows us to compute the maximum XOR efficiently in 𝑂(𝑁⋅𝐿) time, where L is the number of bits required for the largest number.

I request you to assign me this issue please.