# Minimum Bit Flips to Equalize Array
## Problem Statement
Given an array of $n$ integers, $a_1, a_2, \dots, a_n$. We want to choose a target integer $X$ and transform all elements of the array to $X$ by flipping bits. The cost of transforming an integer $A$ to an integer $B$ is defined as the number of bit positions where their binary representations differ. This is also known as the Hamming distance between $A$ and $B$. Our goal is to find the minimum total number of bit flips required to make all elements of the array equal to some chosen integer $X$.
For example, to transform $1 (01_2)$ to $3 (11_2)$, one bit flip is needed (the second bit from $0$ to $1$). The integers are considered as standard 32-bit signed integers in two's complement representation.
## Input Format
- First line: an integer $n$ ($1 \le n \le 10^5$) - the number of elements in the array.
- Second line: $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) - the elements of the array, separated by spaces.
## Output Format
A single integer representing the minimum total number of bit flips required.
## Sample Test Cases
### Sample Input 1
```
3
1 1 3
```
### Sample Output 1
```
1
```
### Sample Input 2
```
4
5 2 7 0
```
### Sample Output 2
```
6
```
## Constraints
- $1 \le n \le 10^5$
- $-10^9 \le a_i \le 10^9$
- Time limit: 1 second
- Memory limit: 256 MB
## Explanation
For Sample Input 1: $n=3$, array is $[1, 1, 3]$.
Let's consider the binary representations of these numbers (using a few bits for illustration):
$1 = \dots0001_2$
$1 = \dots0001_2$
$3 = \dots0011_2$
We iterate through each bit position (from $0$ to $31$ for 32-bit integers):
- **Bit 0 (LSB)**: All numbers ($1, 1, 3$) have their 0-th bit as $1$. If we choose the target $X$'s 0-th bit to be $1$, the cost for this bit position is $0$. If we choose $0$, the cost would be $3$.
- **Bit 1**: Numbers $1$ and $1$ have their 1st bit as $0$. Number $3$ has its 1st bit as $1$. There are two $0$s and one $1$. If we choose the target $X$'s 1st bit to be $0$, the cost for this bit position is $1$ (to flip $3$'s bit from $1$ to $0$). If we choose $1$, the cost would be $2$ (to flip $1$'s bits from $0$ to $1$). So, we choose $0$, incurring a cost of $1$.
- **Other bits**: For all higher bit positions (e.g., bit 2 onwards), all numbers $1, 1, 3$ have their bits as $0$. Thus, the cost for these positions is $0$.
Summing the minimum costs for each bit position: $0 + 1 + 0 + \dots + 0 = 1$. The minimum total bit flips is $1$. The target integer $X$ could be $1$ in this case.
For Sample Input 2: $n=4$, array is $[5, 2, 7, 0]$.
Binary representations:
$5 = \dots0101_2$
$2 = \dots0010_2$
$7 = \dots0111_2$
$0 = \dots0000_2$
- **Bit 0**: Two $1$s (from $5, 7$) and two $0$s (from $2, 0$). Minimum cost is $2$ (either make all $0$s or all $1$s).
- **Bit 1**: Two $1$s (from $2, 7$) and two $0$s (from $5, 0$). Minimum cost is $2$.
- **Bit 2**: Two $1$s (from $5, 7$) and two $0$s (from $2, 0$). Minimum cost is $2$.
- **Other bits**: All are $0$. Minimum cost is $0$.
Summing the minimum costs: $2 + 2 + 2 + 0 + \dots + 0 = 6$. The minimum total bit flips is $6$.
Loading problem statement...