Maximum Hamming Distances - Problem

Given an array nums and an integer m, with each element nums[i] satisfying 0 ≤ nums[i] < 2^m, return an array answer.

The answer array should be of the same length as nums, where each element answer[i] represents the maximum Hamming distance between nums[i] and any other element nums[j] in the array.

The Hamming distance between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,1,4,2], m = 3
Output: [3,3,3,3]
💡 Note: For nums[0]=3 (011): max distance is with nums[2]=4 (100), differing in 3 positions. Each number can achieve distance 3 with some other number.
Example 2 — Small Array
$ Input: nums = [1,0], m = 1
Output: [1,1]
💡 Note: 1 (binary: 1) and 0 (binary: 0) differ in 1 position, so maximum distance for both is 1.
Example 3 — Same Numbers
$ Input: nums = [2,2,2], m = 2
Output: [0,0,0]
💡 Note: All numbers are identical, so Hamming distance between any pair is 0.

Constraints

  • 2 ≤ nums.length ≤ 104
  • 1 ≤ m ≤ 20
  • 0 ≤ nums[i] < 2m

Visualization

Tap to expand
Maximum Hamming Distances ProblemInput: nums = [3,1,4,2], m = 33142011001100010Find maximum Hamming distance for each number3 ⊕ 4 = 011 ⊕ 100 = 111 → 3 bits differentOutput: [3,3,3,3]3333
Understanding the Visualization
1
Input Array
Array of numbers with their binary representations
2
Compare Pairs
For each number, find maximum Hamming distance with others
3
Output Result
Array showing maximum distance for each position
Key Takeaway
🎯 Key Insight: Maximum Hamming distance occurs when two numbers differ in the most bit positions
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 23
23.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen