Equal-Step Triplets

Imagine numbers on a number line: you need to find triples where each next value is exactly the same step away. Write a function arithmetic_triplets that receives a strictly increasing list nums and a number diff. A triplet of indices i < j < k counts if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff. Return the number of such triplets.

Примеры

Ввод
[0, 1, 4, 6, 7, 10], 3
Вывод
2
Ввод
[4, 5, 6, 7, 8, 9], 2
Вывод
2
Ввод
[1, 2, 3], 1
Вывод
1

Нужно реализовать

def arithmetic_triplets(nums: list[int], diff: int) -> int:

Теги

arraysethashmap
PostgreSQLv16

Tukaj bo rezultat izvajanja kode

Visible tests
1
Input
[0,1,4,6,7,10], 3
Expected2
2
Input
[4,5,6,7,8,9], 2
Expected2
3
Input
[1,2,3], 1
Expected1
4
Input
[1,2,3], 5
Expected0
5
Input
[0,3,6,9,12], 3
Expected3
6
Input
[10], 1
Expected0
Focus radio
Paused · SomaFM · Fluid