# Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.

Find the maximum result of ai XOR aj, where 0 ≤ ij < n.

Could you do this in O(n) runtime?

Example:

`Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum result is 5 ^ 25 = 28.`

result += any(result ^ 1 ^ p in prefixes for p in prefixes)

``````class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
result = 0

for i in range(32)[::-1]:
result <<= 1
prefixes = {num >> i for num in nums}
result += any(result ^ 1 ^ p in prefixes for p in prefixes)

return result

``````

# Piles of Boxes

Several days ago, I’ve joined a Hackathon that sponsored by Facebook.

As qualifying people to this event, they hand out an easy algorithm question. Shortly, you need to calculate how less time to move a bunch of boxes in same heigh.

Here is a solution for reference:

``````import collections

def box(boxesInPiles):
sortBoxes = collections.OrderedDict(sorted(collections.Counter(boxesInPiles).items()))
count = 0
for index, key in enumerate(sortBoxes):
count += index * sortBoxes[key]
return count

print(box([4, 5, 5, 2, 4]))``````

# Haskell moment

Here are some Haskell code chunks, which both are simple recursion algorithms.

Quicksort is a sort of poster child for Haskell because everyone does it to showcase how elegant Haskell is.

``````quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSoted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSoted``````
``````maximum' :: (Ord a) => [a] -> a
maximum' [] = error "List is empty!"
maximum' [x] = x
maximum' (x:xs) = (max x (maximum' xs))

replicate' :: Int  -> b -> [b]
replicate' n x
| n <= 0 = []
| otherwise = (x:(replicate' (n-1) x))

take' :: Int -> [a] -> [a]
take' n (x:xs)
| n <= 0 = []
| otherwise = x:(take' (n-1) xs)

reserve' :: [a] -> [a]
reserve' [] = []
reserve' (x:xs) = (reserve' xs) ++ [x]``````

# How to swap two variables without extra space

Let’s say, we have two variables, A and B, and our task is swapping these two variables without extra space.

First, we can this:

A = A + B

and,

B = A – B = A + B – B = A (import A from Step 1)

finally,

A = A – B = A + B – A = B (import A, B from Step 1 and 2 respectively)

That’s it, Dude!

# Be a survivor of a disaster

11月1号考完了这学期的第一门Final，在皇家展览馆考的。看了一下考场的座次表，三千人一起考试真的是美滋滋。考场的“服务人员”态度也特别好，看我手画Burndown Chart，就贴心的给我递过来了一把尺子 (可能是看我手画的太惨不忍睹了)。