Ctrl K

Consistent Reading Triples

Easy
A sensor logged a series of values readings. An engineer wants to find triples of measurements that agree well with each other. A triple of indices i < j < k is consistent when all of these hold: - |readings[i] - readings[j]| ≤ limit_ij; - |readings[j] - readings[k]| ≤ limit_jk; - |readings[i] - readings[k]| ≤ limit_ik. Return the number of consistent triples. Triples with different sets of indices count separately, even if their values are the same.
Example 1
readings=[5, 2, 4, 9, 3], limit_ij=3, limit_jk=2, limit_ik=2 → 4

The index triples (0, 1, 2) with values 5, 2, 4, (0, 1, 4) with 5, 2, 3, (0, 2, 4) with 5, 4, 3 and (1, 2, 4) with 2, 4, 3 fit. Any triple that includes 9 breaks at least one limit.

Example 2
readings=[7, 7, 7, 7], limit_ij=0, limit_jk=0, limit_ik=0 → 4

All readings are equal, so every one of the C(4, 3) = 4 triples fits.

Example 3
readings=[1, 10, 20], limit_ij=5, limit_jk=5, limit_ik=50 → 0

The only triple fails: 1 and 10 differ by 9, which is more than limit_ij.

Constraints
  • 3 ≤ readings.length ≤ 100
  • 0 ≤ readings[i] ≤ 1000
  • 0 ≤ limit_ij, limit_jk, limit_ik ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepeated Block Run EasyXOR of Every Subset EasyThree Addends and a Later Total EasyEven Numbers From Digit Cards EasyEvenly Spaced Triples Easy
loading Python…
Input
readings=[5, 2, 4, 9, 3], limit_ij=3, limit_jk=2, limit_ik=2
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.