Skip to content

Commit 3c3d114

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Maximum Xor. First steps.
1 parent fc1a884 commit 3c3d114

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# [Miscellaneous: Maximum Xor](https://www.hackerrank.com/challenges/maximum-xor)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#Miscellaneous`
5+
6+
Find the maximum xor value in the array.
7+
8+
You are given an array `arr` of `n` elements.
9+
A list of integers, `queries` is given as an input,
10+
find the maximum value of
11+
`queries[j]` $ \oplus $ `each arr[i]`
12+
for all $ 0 \leq i < n $,
13+
where \oplus represents [xor](https://en.wikipedia.org/wiki/Exclusive_or)
14+
of two elements.
15+
16+
Note that there are multiple test cases in one input file.
17+
18+
For example:
19+
20+
$ \begin{array}{ll}
21+
arr &= [3, 7, 15, 10] \\
22+
queries[j] &= 3 \\
23+
\end{array} $
24+
25+
$ \begin{array}{l:l}
26+
3 \oplus 3 = 0 & max = 0 \\
27+
3 \oplus 7 = 4 & max = 4 \\
28+
3 \oplus 15 = 12 & max = 12 \\
29+
3 \oplus 10 = 9 & max = 12 \\
30+
\end{array} $
31+
32+
## Function Description
33+
34+
Complete the maxXor function in the editor below.
35+
It must return an array of integers,
36+
each representing the maximum xor value for each element `queries[j]`
37+
against all elements of `arr`.
38+
39+
maxXor has the following parameter(s):
40+
41+
- arr: an array of integers
42+
- queries: an array of integers to query
43+
44+
## Input Format
45+
46+
The first line contains an integer `n`, the size of the array `arr`.
47+
48+
The second line contains `n` space-separated integers,
49+
`arr[i]` from $ 0 \leq i < n $.
50+
51+
The third line contain `m`, the size of the array `queries`.
52+
53+
Each of the next `m` lines contains an
54+
integer `queries[j]` where $ 0 \leq j < m $.
55+
56+
## Constraints
57+
58+
- $ 1 \leq n,m \leq 10^5 $
59+
- $ 0 \leq arr[i], queries[j] \leq 10^9 $
60+
61+
## Output Format
62+
63+
The output should contain `m` lines with each line representing output
64+
for the corresponding input of the testcase.
65+
66+
## Sample Input 0
67+
68+
```text
69+
3
70+
0 1 2
71+
3
72+
3
73+
7
74+
2
75+
```
76+
77+
## Sample Output 0
78+
79+
```text
80+
3
81+
7
82+
3
83+
```
84+
85+
## Explanation 0
86+
87+
$ arr = [0, 1, 2] $
88+
89+
$ queries[0] = 3 $
90+
91+
$ \begin{array}{l:l}
92+
3 \oplus 0 = 3 & max = 3 \\
93+
3 \oplus 1 = 2 & max = 3 \\
94+
3 \oplus 2 = 1 & max = 3 \\
95+
\end{array} $
96+
97+
$ queries[1] = 7 $
98+
99+
$ \begin{array}{l:l}
100+
7 \oplus 0 = 7 & max = 7 \\
101+
7 \oplus 1 = 6 & max = 7 \\
102+
7 \oplus 2 = 5 & max = 7 \\
103+
\end{array} $
104+
105+
$ queries[2] = 2 $
106+
107+
$ \begin{array}{l:l}
108+
2 \oplus 0 = 2 & max = 3 \\
109+
2 \oplus 1 = 3 & max = 3 \\
110+
2 \oplus 2 = 0 & max = 3 \\
111+
\end{array} $
112+
113+
## Sample Input 1
114+
115+
```text
116+
5
117+
5 1 7 4 3
118+
2
119+
2
120+
0
121+
```
122+
123+
## Sample Output 1
124+
125+
```text
126+
7
127+
7
128+
```
129+
130+
## Explanation 1
131+
132+
$ arr = [5, 1, 7, 4, 3] $
133+
134+
$ queries[0] = 2 $
135+
136+
$ \begin{array}{l:l}
137+
2 \oplus 5 = 7 & max = 7 \\
138+
2 \oplus 1 = 3 & max = 7 \\
139+
2 \oplus 7 = 5 & max = 7 \\
140+
2 \oplus 4 = 7 & max = 7 \\
141+
2 \oplus 3 = 3 & max = 7 \\
142+
\end{array} $
143+
144+
$ queries[1] = 7 $
145+
146+
$ \begin{array}{l:l}
147+
0 \oplus 5 = 7 & max = 7 \\
148+
0 \oplus 1 = 3 & max = 7 \\
149+
0 \oplus 7 = 5 & max = 7 \\
150+
0 \oplus 4 = 7 & max = 7 \\
151+
0 \oplus 3 = 3 & max = 7 \\
152+
\end{array} $
153+
154+
## Sample Input 2
155+
156+
```text
157+
4
158+
1 3 5 7
159+
2
160+
17
161+
6
162+
```
163+
164+
## Sample Output 2
165+
166+
```text
167+
22
168+
7
169+
```
170+
171+
## Explanation 2
172+
173+
$ arr = [1, 2, 5, 7] $
174+
175+
$ queries[0] = 17 $
176+
177+
$ \begin{array}{l:l}
178+
17 \oplus 16 = 16 & max = 16 \\
179+
17 \oplus 18 = 18 & max = 18 \\
180+
17 \oplus 20 = 20 & max = 20 \\
181+
17 \oplus 22 = 22 & max = 22 \\
182+
\end{array} $
183+
184+
$ queries[1] = 6 $
185+
186+
$ \begin{array}{l:l}
187+
6 \oplus 7 = 7 & max = 7 \\
188+
6 \oplus 5 = 5 & max = 7 \\
189+
6 \oplus 3 = 3 & max = 7 \\
190+
6 \oplus 1 = 1 & max = 7 \\
191+
\end{array} $
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# pylint: disable=line-too-long
2+
# @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/maximum-xor.md]] # noqa
3+
# pylint: enable=line-too-long
4+
5+
def maxXor(arr: list[int], queries: list[int]) -> list[int]:
6+
result = []
7+
8+
for j in queries:
9+
maximum = 0
10+
for x in arr:
11+
maximum = max(maximum, j ^ x)
12+
result.append(maximum)
13+
14+
return result
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"arr": [0, 1, 2],
5+
"queries": [3, 7, 2],
6+
"expected": [3, 7, 3]
7+
},
8+
{
9+
"title": "Sample Test case 1",
10+
"arr": [5, 1, 7, 4, 3],
11+
"queries": [2, 0],
12+
"expected": [7, 7]
13+
},
14+
{
15+
"title": "Sample Test case 2",
16+
"arr": [1, 3, 5, 7],
17+
"queries": [17, 6],
18+
"expected": [22, 7]
19+
}
20+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from pathlib import Path
3+
4+
from ....lib.loader import loadTestCases
5+
from .maximum_xor import maxXor
6+
7+
FILE_PATH = str(Path(__file__).resolve().parent)
8+
9+
TEST_CASES = loadTestCases(
10+
FILE_PATH + '/maximum_xor.testcases.json')
11+
12+
13+
class TestMaximumXor(unittest.TestCase):
14+
15+
def test_maximum_xor(self):
16+
17+
for _, _tt in enumerate(TEST_CASES):
18+
19+
self.assertEqual(
20+
maxXor(_tt['arr'], _tt['queries']), _tt['expected'],
21+
f"{_} | maxXor({_tt['arr']}, {_tt['queries']}) must be "
22+
f"=> {_tt['expected']}")

0 commit comments

Comments
 (0)