From 4a704124262c134f5f27a77feba989d6dd0d55f1 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 21 Mar 2024 15:56:15 -0300 Subject: [PATCH] [Hacker Rank] Interview Preparation Kit: Miscellaneous: Maximum Xor. Initial (brute-force) solution. It doesn't pass all test-cases. --- .../miscellaneous/maximum-xor.md | 212 ++++++++++++++++++ .../miscellaneous/maximum_xor.py | 11 + .../miscellaneous/maximum_xor_test.py | 23 ++ 3 files changed, 246 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/miscellaneous/maximum-xor.md create mode 100644 src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor.py create mode 100644 src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor_test.py diff --git a/docs/hackerrank/interview_preparation_kit/miscellaneous/maximum-xor.md b/docs/hackerrank/interview_preparation_kit/miscellaneous/maximum-xor.md new file mode 100644 index 00000000..c8d514e4 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/miscellaneous/maximum-xor.md @@ -0,0 +1,212 @@ +# [Miscellaneous: Maximum Xor](https://www.hackerrank.com/challenges/maximum-xor) + +- Difficulty: `#medium` +- Category: `#Miscellaneous` + +You are given an array `arr` of `n` elements. +A list of integers, `queries` is given as an input, +find the maximum value of +$ \textsf{\textbf{ queries[j] }} + \oplus + \textsf{\textbf{ each arr[i] }} +$ +for all $ 0 \leq i < n $ , where $ \oplus $ represents +[xor](https://www.hackerrank.com/challenges/maximum-xor/problem#:~:text=represents-,xor,-of%20two%20elements) of two elements. + +Note that there are multiple test cases in one input file. + +For example: + +```C +arr = [3, 7, 15, 10] +queries[j] = 3 +``` + +$ 3 \oplus 3 = 0, max = 0 $ + +$ 3 \oplus 7 = 4, max = 4 $ + +$ 3 \oplus 15 = 12, max = 12 $ + +$ 3 \oplus 10 = 9, max = 12 $ + +## Function Description + +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`. + +maxXor has the following parameter(s): + +- `arr`: an array of integers +- `queries`: an array of integers to query + +## Input Format + +The first line contains an integer `n`, the size of the array `arr`. + +The second line contains `n` space-separated integers, `arr[i]` from $ 0 \leq i \leq n $. + +The third line contain `m`, the size of the array `queries`. + +Each of the next `m` lines contains an integer `queries[j]` where $ 0 \leq j \leq m $. + +## Constraints + +- $ 1 \leq n, m \leq 10^5 $ +- $ 0 \leq arr[i], queries[j] \leq 10^9 $ + +## Output Format + +The output should contain `m` lines with each line representing output for the corresponding input of the testcase. + +## Sample Input 0 + +```text +3 +0 1 2 +3 +3 +7 +2 +``` + +## Sample Output 0 + +```text +3 +7 +3 +``` + +## Explanation 0 + +```C +arr = [0, 1, 2] +``` + +```C +queries[0] = 3 +``` + +$ 3 \oplus 0 = 3, max = 3 $ + +$ 3 \oplus 1 = 2, max = 3 $ + +$ 3 \oplus 2 = 1, max = 3 $ + +```C +queries[1] = 7 +``` + +$ 7 \oplus 0 = 7, max = 7 $ + +$ 7 \oplus 1 = 6, max = 7 $ + +$ 7 \oplus 2 = 5, max = 7 $ + +```C +queries[2] = 2 +``` + +$ 7 \oplus 0 = 7, max = 7 $ + +$ 7 \oplus 1 = 6, max = 7 $ + +$ 7 \oplus 2 = 5, max = 7 $ + +## Sample Input 1 + +```text +5 +5 1 7 4 3 +2 +2 +0 +``` + +## Sample Output 1 + +```text +7 +7 +``` + +## Explanation 1 + +```C +arr = [5, 1, 7, 4, 3] +``` + +```C +queries[0] = 2 +``` + +$ 2 \oplus 5 = 7, max = 7 $ + +$ 2 \oplus 1 = 3, max = 7 $ + +$ 2 \oplus 7 = 5, max = 7 $ + +$ 2 \oplus 4 = 6, max = 7 $ + +$ 2 \oplus 3 = 1, max = 7 $ + +```C +queries[1] = 0 +``` + +$ 0 \oplus 5 = 5, max = 5 $ + +$ 0 \oplus 1 = 1, max = 5 $ + +$ 0 \oplus 7 = 7, max = 7 $ + +$ 0 \oplus 4 = 4, max = 7 $ + +$ 0 \oplus 3 = 3, max = 7 $ + +## Sample Input 2 + +```text +4 +1 3 5 7 +2 +17 +6 +``` + +## Sample Output 2 + +```text +22 +7 +``` + +## Explanation 2 + +```C +arr = [1, 3, 5, 7] +``` + +```C +queries[0] = 17 +``` + +$ 17 \oplus 1 = 16, max = 16 $ + +$ 17 \oplus 3 = 18, max = 18 $ + +$ 17 \oplus 5 = 20, max = 20 $ + +$ 17 \oplus 7 = 22, max = 22 $ + +```C +queries[1] = 6 +``` + +$ 6 \oplus 1 = 7, max = 7 $ + +$ 6 \oplus 3 = 5, max = 7 $ + +$ 6 \oplus 5 = 3, max = 7 $ + +$ 6 \oplus 1 = 1, max = 7 $ diff --git a/src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor.py b/src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor.py new file mode 100644 index 00000000..d2ba8c8a --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor.py @@ -0,0 +1,11 @@ +def max_xor(arr, queries): + + result = [] + + for j in queries: + maximum = 0 + for x in arr: + maximum = max(maximum, j ^ x) + result.append(maximum) + + return result diff --git a/src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor_test.py b/src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor_test.py new file mode 100644 index 00000000..0c6b23a5 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/miscellaneous/maximum_xor_test.py @@ -0,0 +1,23 @@ +import unittest +from .maximum_xor import max_xor + +TEST_CASES = [ + { + 'title': 'Sample Test case 0', + 'arr': [0, 1, 2], + 'queries': [3, 7, 2], + 'answer': [3, 7, 3] + } +] + + +class TestMaximumXor(unittest.TestCase): + + def test_max_xor(self): + + for _, _tt in enumerate(TEST_CASES): + + self.assertEqual( + max_xor(_tt['arr'], _tt['queries']), _tt['answer'], + f"{_} | max_xor({_tt['arr']}, {_tt['queries']}) must be " + f"=> {_tt['answer']}")