#K1236. Counting Arithmetic Triplets

    ID: 23673 Type: Default 1000ms 256MiB

Counting Arithmetic Triplets

Counting Arithmetic Triplets

Given a sequence of integers, your task is to count the number of triplets (i, j, k) with i < j < k such that the three elements form an arithmetic progression. In other words, for the triplet (a[i], a[j], a[k]), they must satisfy the equation:

$$a_i + a_k = 2a_j$$

where a_i, a_j, and a_k are the elements at positions i, j, and k respectively. If there are no such triplets, output 0.

Note: The input list may contain duplicate values and can be of any size, but the triplets must always follow the order i < j < k.

inputFormat

The input is given via standard input (stdin) and has the following format:

  1. The first line contains a single integer n which denotes the number of elements in the sequence.
  2. The second line contains n space-separated integers representing the sequence.

For example:

6
1 3 5 7 9 11

outputFormat

Output a single integer which is the count of arithmetic triplets in the sequence. The output should be printed to standard output (stdout).

For example, for the input above, the output should be:

6
## sample
6
1 3 5 7 9 11
6

</p>