Differing Bits Over All Pairs
Two numbers differ at a bit position when one of them has a 1 there and the other has a 0. For every pair of positions i < j of nums, count the bit positions at which nums[i] and nums[j] differ.
Return the sum of these counts over all the pairs.
Example 1
nums [1,2,3]
returns 4
1 and 2 are 01 and 10: two bits differ. 3 is 11 and differs from each of them in one bit. 2 + 1 + 1 = 4.
Example 2
nums [7,7]
returns 0
The two numbers are equal, so no bit differs.
Constraints
1 <= len(nums) <= 3000 <= nums[i] < 2**30
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Flips to Make OR EqualMedium
- Insert a Bit FieldMedium
- Longest Pair With No Shared LetterMedium
- Next Gray CodeMedium
- Subsets With a Target XORMedium
- The One Not TripledMedium
- Two Unpaired NumbersMedium
- Undo the XOR ChainMedium