Skip to content

Commit 4a70412

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Maximum Xor. Initial (brute-force) solution. It doesn't pass all test-cases.
1 parent 24321b5 commit 4a70412

File tree

3 files changed

+246
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)