829. Maximum Hamming Distances

You are given an integer array nums and an integer m. Each element in nums satisfies 0 <= nums[i] < 2m.

Your goal is to return an array answer of the same length as nums, where for each index i, answer[i] is the maximum Hamming distance between nums[i] and any other element in nums.

The Hamming distance between two numbers is the number of bit positions in which their binary representations (each padded to m bits) differ.

Example 1:

Input : nums = [5, 2, 7, 8], m = 4

Output : [3, 3, 4, 4]

Explanation :

Representing the numbers as 4-bit strings:

  • 5 → "0101"
  • 2 → "0010"
  • 7 → "0111"
  • 8 → "1000"

For instance, the maximum Hamming distance for 7 is 4 (with 8: "0111" vs "1000").

Example 2:

Input : nums = [3, 3, 0, 15], m = 4

Output : [2, 2, 4, 4]

Explanation :

4-bit representations:

  • 3 → "0011"
  • 0 → "0000"
  • 15 → "1111"

For the first 3, the largest distance is 2 (e.g., with 0 or 15).

Now Your Turn!

Pick the correct output for the given input

Input : nums = [1, 16, 8, 23], m = 5

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= m <= 17
  • 2 <= nums.length <= 2m
  • 0 <= nums[i] < 2m

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> maxHammingDistances(vector<int>& nums, int m) {
// Your code goes here
}
};
Test Case

Input:

M
Nums