Skip to the code
cf.
+25 XP

#680 Total Snapshot Difference

medium

A visual-regression tool stores each test run as a bit mask of which checks passed. The difference between two runs is the number of bit positions where they differ.

Write totalDifference(snapshots) that returns the sum of the differences over all pairs of runs i < j. There can be 10,000 runs, so checking every pair is too slow.

Example 1

Input: snapshots = [4,14,7]
Output: 6

4 ^ 14 = 1010, 4 ^ 7 = 0011, and 14 ^ 7 = 1001: 2 differences each, 6 in total.

Example 2

Input: snapshots = [1,1]
Output: 0

Identical runs don't differ.

Constraints

  • 1 <= snapshots.length <= 10^4
  • 0 <= snapshots[i] < 2^30
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).