Skip to content
Decoder

Total Hamming Distance

Medium

Problem

Return the sum of the Hamming distances (the number of differing bit positions) between every pair of numbers in nums.

Example 1

Input
nums = [4, 14, 2]
Output
6

Example 2

   4 = 0100, 14 = 1110, 2 = 0010

Example 3

   d(4, 14) + d(4, 2) + d(14, 2) = 2 + 2 + 2

Example 4

Input
nums = [4, 14, 4]
Output
4

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^9
  • The answer fits in a 32-bit integer.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.