Erase the n least significant bits from each integer in an array

Quick Overview

Given an array of non-negative integers and a bit count n, remove the n least significant bits from each value and return the new values in their original order. It tests binary representation, right shifts and the link between shifting and floor division by a power of two.

Erase the n least significant bits from each integer in an array

Company: Deloitte

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ${2^n}$). Return a new array containing the transformed values, preserving the original order. ### Function Signature ```python 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 ```text 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. ```text 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. ```text 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.

Overview: Given an array of non-negative integers and a bit count n, remove the n least significant bits from each value and return the new values in their original order. It tests binary representation, right shifts and the link between shifting and floor division by a power of two.

Deloitte logo
Deloitte
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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{2^n}).

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.