Number of Arithmetic Triplets - Problem

You are given a strictly increasing integer array nums and a positive integer diff. Your task is to find all arithmetic triplets in the array.

An arithmetic triplet is a set of three elements at indices (i, j, k) where:

  • i < j < k (indices are in order)
  • nums[j] - nums[i] == diff (first gap equals diff)
  • nums[k] - nums[j] == diff (second gap equals diff)

Example: In array [0, 1, 4, 6, 7, 10] with diff = 3, the triplet (1, 4, 7) forms an arithmetic sequence because 4 - 1 = 3 and 7 - 4 = 3.

Return the total count of unique arithmetic triplets.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [0,1,4,6,7,10], diff = 3
โ€บ Output: 2
๐Ÿ’ก Note: There are 2 arithmetic triplets: (0,3,6) doesn't exist since 3 is not in array, but (1,4,7) and (4,7,10) are valid triplets where consecutive differences equal 3.
example_2.py โ€” No Triplets
$ Input: nums = [4,5,6,7,8,9], diff = 2
โ€บ Output: 2
๐Ÿ’ก Note: The arithmetic triplets are (4,6,8) and (5,7,9). Each has differences of 2 between consecutive elements.
example_3.py โ€” Single Element
$ Input: nums = [1,5,7,12,15,18], diff = 3
โ€บ Output: 2
๐Ÿ’ก Note: Two arithmetic triplets exist: (12,15,18) with differences of 3, but we need to check all possibilities systematically.

Visualization

Tap to expand
๐ŸŽน Arithmetic Triplets as Musical HarmoniesArray: [0, 1, 4, 6, 7, 10] with diff = 3Piano Keys (Numbers):0146710Checking if 4 can be middle note:1+3โ†’4+3โ†’7โœ“ Found 1 in seen notesโœ“ Found 7 in available notes๐ŸŽต Perfect harmony (1,4,7)!Algorithm Steps:1. ๐ŸŽน Catalog all available notes (hash set)2. ๐ŸŽต For each note, check if it's the middle of a 3-note harmony3. ๐Ÿ” Look for note-3 in played notes, note+3 in catalog4. โœ… Count each perfect arithmetic progression foundTime Complexity: O(n) | Space Complexity: O(n)Just like finding musical chords, we need exact intervals between notes!
Understanding the Visualization
1
Build Lookup
Create hash set of all available notes (numbers)
2
Find Middle
For each note, check if it can be the middle of a harmony
3
Check Intervals
Verify that notes at exact intervals exist
4
Count Harmonies
Count all valid three-note arithmetic progressions
Key Takeaway
๐ŸŽฏ Key Insight: Use the middle element as anchor point - it's the most efficient way to verify arithmetic progressions in a sorted array!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array with O(1) hash set operations for each element

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash set stores up to n elements from the input array

n
2n
โšก Linearithmic Space

Constraints

  • 3 โ‰ค nums.length โ‰ค 1000
  • 0 โ‰ค nums[i] โ‰ค 1000
  • 1 โ‰ค diff โ‰ค 50
  • nums is strictly increasing
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen