#C11581. Equalizing Array Elements
Equalizing Array Elements
Equalizing Array Elements
You are given an array of positive integers. In a single move, you may perform one of the following operations on any element any number of times:
- Multiply the element by 2.
- Add 1 to the element.
However, a careful observation shows that these operations almost preserve a key invariant. In particular, if you repeatedly divide a number by 2 until it becomes odd, the resulting normalized value will remain unchanged provided that you only multiply by 2. (Note that adding 1 can change the normalized value.)
It turns out that in order to make all array elements equal by applying these operations independently to each element, the normalized (odd) parts of all numbers must be the same. Your task is to check whether this condition holds.
Example:
- For the array [1, 2, 4]: The normalized values are 1, 1, and 1 respectively, so the answer is YES.
- For the array [3, 5, 6]: The normalized values are 3, 5, and 3 respectively, so the answer is NO.
- For the array [16, 8, 4, 2]: All normalized values become 1, hence the answer is YES.
Note: Even though at first glance it might seem that the addition operation could help in equalizing the numbers, it may actually alter the fundamental invariant required for a solution. Thus, the condition for success is that all numbers, after removing all factors of 2, are equal.
inputFormat
The first line of input contains a single integer n (1 ≤ n ≤ 10^5), representing the number of elements in the array. The second line contains n space-separated positive integers.
outputFormat
Output a single line containing "YES" if it is possible to make all the array elements equal by applying the allowed operations, or "NO" otherwise.## sample
3
1 2 4
YES