-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution421.cs
41 lines (33 loc) · 893 Bytes
/
Solution421.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Text;
namespace LeetCode.Solutions;
public class Solution421
{
/// <summary>
/// 421. Maximum XOR of Two Numbers in an Array - Medium
/// <a href="https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array">See the problem</a>
/// </summary>
public int FindMaximumXOR(int[] nums)
{
var max = 0;
var mask = 0;
for (var i = 31; i >= 0; i--)
{
mask |= 1 << i;
var set = new HashSet<int>();
foreach (var num in nums)
{
set.Add(num & mask);
}
var temp = max | (1 << i);
foreach (var prefix in set)
{
if (set.Contains(temp ^ prefix))
{
max = temp;
break;
}
}
}
return max;
}
}