#K84347. Counting Arithmetic Triplets
Counting Arithmetic Triplets
Counting Arithmetic Triplets
Given an array of ( n ) integers and an integer ( d ), count the number of triplets ( (a_i, a_j, a_k) ) that form an arithmetic progression with a common difference ( d ). In other words, find the number of triplets satisfying ( a_j - a_i = d ) and ( a_k - a_j = d ) with ( i < j < k ).
The input consists of two lines. The first line contains two integers ( n ) and ( d ). The second line contains ( n ) space-separated integers representing the array elements. Your program should output a single integer, the number of valid triplets.
inputFormat
Input is read from standard input. The first line contains two space-separated integers ( n ) (the number of elements in the array) and ( d ) (the common difference). The second line contains ( n ) space-separated integers representing the array.
outputFormat
Output a single integer to standard output: the count of triplets which form an arithmetic progression with common difference ( d ).## sample
5 2
1 3 5 7 9
3
</p>