Fixing an Array
Time limit2sMemory limit128 MB
For each array value, find the number within a given range that has the smallest Hamming distance in binary, breaking ties by choosing the smallest value.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
You are given an integer array A and two integers low and high. Construct an array B with the same length as A.
Every element X of B must satisfy low <= X <= high. For each index i, choose B[i] so that the bit difference between A[i] and B[i] is minimized. If multiple arrays B satisfy these minimum distances, output the lexicographically smallest one.
The bit difference between two integers a and b is computed as follows. Write both numbers in binary. If their lengths differ, pad the shorter binary representation on the left with 0s until the lengths match. Then count the positions where the two bits are different.
Input
The first line contains the size N of array A and two integers low and high.
The second line contains the elements A_i of array A, separated by spaces.
Output
Print the elements of array B on one line, separated by spaces.
Constraints
- 1 <= N <= 50
- 0 <= low <= high <= 2^30 - 1
- 0 <= A_i <= 2^30 - 1