Given an array of non-negative integers nums and a non-negative integer n, erase the n least significant bits from each integer's binary representation and return the resulting decimal values.
Erasing the n least significant bits is equivalent to shifting each integer right by n (that is, integer-dividing it by 2n).
Return a new array containing the transformed values, preserving the original order.
Function Signature
def erase_lsbs(nums: list[int], n: int) -> list[int]:
Constraints
-
1 <= len(nums) <= 10^5
-
0 <= nums[i] <= 10^9
-
0 <= n <= 30
Examples
Input: nums = [7, 15, 3], n = 1
Output: [3, 7, 1]
In binary, 7 is 111, 15 is 1111 and 3 is 11. Dropping the last bit leaves 11, 111 and 1, which are 3, 7 and 1.
Input: nums = [8, 16, 32, 64], n = 3
Output: [1, 2, 4, 8]
Removing three bits divides each value by 8 and rounds down. All four values are exact multiples of 8, so nothing is rounded away.
Input: nums = [1000000000, 6, 0], n = 2
Output: [250000000, 1, 0]
The value 6 is 110 in binary, and dropping two bits leaves 1. One billion divided by 4 is exactly 250,000,000, and 0 stays 0.