KatInterview

Recover Total Weight From All Pairwise Crate Readings

Math & Bit ManipulationAuto-graded · 16 test cases
EasyAuto-gradedMath & Bit Manipulationmathcountingpairwise-sums

Last updated

Tests the double-counting insight that each item appears in a fixed number of pair sums, so the total equals the sum of the readings divided by a constant.

A warehouse worker has exactly 5 crates. Every possible pair of crates is placed on a scale once, which produces 10 readings in total. The readings are given as a list, and you do not know which reading belongs to which pair of crates.

Write a function that returns the combined weight of all 5 crates.

Details

  • Each of the 10 distinct pairs of crates is weighed exactly once.
  • The scale is exact. Treat the readings as given data, and do not try to check whether five real weights could actually produce them.
  • Individual crate weights may be non-integers.
  • The order of the readings in the list carries no information.
  • Return the total as a float. For example, a result of 63.75 should be returned as 63.75.

Constraints

  • readings contains exactly 10 numbers (integers or floats).
  • Each reading is a real number.
  • Every crate appears in exactly 4 of the 10 pairs.
  • No validation of the input is required.

Examples

Example 1

Input: total_crate_weight([21, 22, 23, 24, 25, 26, 27, 28, 29, 30])

Output: 63.75

Hints

Hint 1

You do not need to figure out which reading belongs to which pair. Think about what happens if you add all the readings together.

Hint 2

Pick one crate and count how many of the 10 pairs include it. Each crate is paired once with every other crate.

Hint 3

If every crate shows up in exactly 4 pairs, the sum of all readings equals 4 times the combined weight. Divide accordingly.