Integer distribution

2.6

14 votes
Algorithms, Bit Manipulation, Dynamic Programming, Medium
Problem

You are given a list A of N integers. Here, N is an even number. These integers must be distributed into N/2 pairs and the power of each of the pairs is added.
The power of a pair P(i,j) consisting of the ith and jth number is defined as the XOR of the two numbers A[i] and A[j].
Write a program to determine the minimum and the maximum possible sum of the N/2 pairs.

Input format

  • First line: An integer N
  • Next N lines: An integer where each denotes an element of the list

Output format

The output consists of two space-separated integers. The first is the minimum possible sum and the second is the maximum possible sum.

Constraints

1N20;N is even
A[i] fits in 32bit integer value.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In a sample case, the following combinations are possible:

(1,2)(3,4) -> 3+7=10
(1,3)(2,4) -> 2+6=8
(1,4)(2,3) -> 5+1=6

Editor Image

?