Number of Arithmetic Triplets
The problem is asking to count the number of unique arithmetic triplets in the given list of numbers. A triplet is arithmetic if the difference between the second and first element is equal to the difference between the third and second element, and this difference equals a given value diff
.
Here’s an example: consider the list [1, 4, 7, 10] and diff = 3. The arithmetic triplets in this case would be (1, 4, 7) and (4, 7, 10), because in both cases the difference between each consecutive pair of elements is 3. So the output would be 2, because there are two such triplets.
To be considered a triplet, the three numbers have to be in the correct order in the array, not just any three numbers. For example, in the list [1, 7, 4, 10] with diff = 3, there are no arithmetic triplets, because even though 1, 4, and 7 could form a triplet, they are not in the right order in the array.
Also, the array nums
is strictly increasing, which means that each number is larger than the previous number.
The goal is to find an efficient way to count all such triplets in the given array.


The Python function arithmeticTriplets
is designed to find the count of arithmetic triplets in a given list of numbers nums
where the common difference is diff
.
An arithmetic triplet is a sequence of three numbers where the difference between consecutive numbers is constant. For example, in the sequence [3, 5, 7], the difference between consecutive numbers is 2, making it an arithmetic triplet.
Here’s how the function works:
The function starts by initializing a list
cnt
of size 201 with all elements set to 0. This list will be used to store the presence of each number innums
.It also initializes
res
to 0. This variable will hold the final count of arithmetic triplets.Then, it iterates over all the numbers in
nums
. For each numbern
, it checks ifn
is greater than or equal to2 * diff
. If it is, it meansn
can potentially be the third number in an arithmetic triplet.If
n
is greater than or equal to2 * diff
, the function incrementsres
if the numbersn  diff
andn  2 * diff
are present innums
. The presence of these numbers is checked using thecnt
list.After this, it marks the presence of
n
in thecnt
list by settingcnt[n]
to 1.Finally, it returns
res
, which is the count of arithmetic triplets innums
.
This function effectively uses a counting array to keep track of the presence of numbers in nums
and finds the count of arithmetic triplets in a single pass, resulting in a time complexity of O(n), where n is the number of elements in nums
. The space complexity is O(1) as the size of the cnt
list is fixed.
Q&A
Can u tell me why check n >= 2 * diff ?
In an arithmetic progression or sequence, the difference between consecutive terms is constant. If we consider a triplet (a, b, c) to be in arithmetic progression, it must satisfy the condition that b  a = c  b
, where b  a
and c  b
are the common differences.
In the context of this problem, we’re looking for triplets where the common difference is diff
. So, we need b  a = diff
and c  b = diff
. If we rearrange the terms, we get b = a + diff
and c = b + diff = a + 2 * diff
.
Now, when we’re iterating over nums
and checking for each number n
, n
could be the third term of an arithmetic triplet (i.e., n = c
). Before checking if n
forms a triplet with two previous numbers, we need to ensure that the two previous numbers (i.e., a
and b
) can exist. And for a
and b
to exist, n
should be at least 2 * diff
(since n = a + 2 * diff
).
So, the check n >= 2 * diff
is to ensure that n
can potentially be the third number in an arithmetic triplet. If n
is less than 2 * diff
, it cannot be the third number in a triplet with a common difference of diff
.