The Number of Arithmetic Triplets problem asks you to count the number of triplets (i, j, k) where i < j < k such that arr[j] - arr[i] == arr[k] - arr[j] == diff (given constant difference). The array is guaranteed to be sorted and distinct. This easy coding problem has both O(n²) and O(n) solutions.
Meta and Google ask this as a quick assessment of hash set / two-pointer thinking on sorted arrays. The O(n) solution using a hash set to check for the two required neighbors is the expected answer. The array, hash table, two pointers, and enumeration interview pattern is demonstrated.
Hash set for O(n) lookup. Build a set of all array values. For each element v, check if v - diff and v - 2*diff are also in the set. If both exist, it's a valid triplet. Count all such v values.
Alternatively, two pointers: for each middle element j, use two pointers for i and k. But the hash set approach is simpler.
Array: [0, 1, 4, 6, 7, 10], diff=3.
v + diff without verifying v - diff also exists.Arithmetic triplet problems reduce to "given middle element, can I find left and right?" A hash set makes this O(1) per element. Build the set first, then check each element as a potential middle. This approach extends to arithmetic sequences of any length: for k-length sequences, check k-1 progressions from any starting element. Practice similar "count sequences satisfying arithmetic property" problems to develop fast hash-set-based thinking.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Special Quadruplets | Easy | Solve | |
| Merge Two 2D Arrays by Summing Values | Easy | Solve | |
| Form Smallest Number From Two Digit Arrays | Easy | Solve | |
| Recover the Original Array | Hard | Solve | |
| Find the Maximum Number of Elements in Subset | Medium | Solve |