Back to questions
You are given a strictly increasing array of integers , and a positive integer . An arithmetic triplet with a gap of is defined as triplet of integers from where:
Return the number of arithmetic triplets with a gap of that exist in .
Input: nums = [1, 2, 4, 6, 7, 11, 12] ; diff = 5
Output: 2
Explanation: For a diff of 5, there are two valid triplets: (1,6,11) and (2,7,12)
Notice how we can rearrange the equations relating the triplet values to such that and are defined in terms of . In other words, for a given value of , we know exactly what and we need.
Therefore, we can iterate over each value in and just check if the corresponding values of and exist in . A set of all the values in will allow us to perform this check efficiently. Be sure to remember that sets and dictionaries in python are very efficient for lookups!