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.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.
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.
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.
readings.length ≤ 100readings[i] ≤ 1000limit_ij, limit_jk, limit_ik ≤ 1000readings=[5, 2, 4, 9, 3], limit_ij=3, limit_jk=2, limit_ik=2
4
Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.