Number of Arithmetic Triplets (LeetCode 2367): Solution and Explanation

Easy
Two Pointers
Array
Hash Table

Given a strictly increasing array nums and a step diff, count the index triplets i < j < k where nums[j] - nums[i] and nums[k] - nums[j] both equal diff.

Intuition

The array is strictly increasing, so values are distinct and the index order follows the value order. A triplet is therefore fixed by its first value x: the other two must be exactly x + diff and x + 2 * diff.

That reduces the problem to: for each x, are x + diff and x + 2 * diff both in nums? A hash set answers that instantly.

The sorted order gives a version without extra memory. When x moves to a larger value, its two targets move up too, so the places where they would be found only move right. Two trailing pointers j and k can follow them without ever stepping back.

Approach: Three pointers

  1. Set j = k = 0 and count = 0.
  2. For each value x in nums (this is the i pointer):
  3. Advance j while nums[j] < x + diff.
  4. Advance k while nums[k] < x + 2 * diff.
  5. If both pointers are in range and nums[j] == x + diff and nums[k] == x + 2 * diff, increment count.
  6. Return count.

Step-by-step example

nums = [0,1,4,6,7,10], diff = 3
  1. x = 0: j stops on 4 (needs 3), k stops on 6. 3 is missing, so no triplet.
  2. x = 1: j stays on 4, which is 1 + 3. k moves to 7, which is 1 + 6. Triplet (1, 4, 7), count = 1.
  3. x = 4: j moves to 7 = 4 + 3 and k moves to 10 = 4 + 6. Triplet (4, 7, 10), count = 2.
  4. x = 6, 7 and 10 need values above 10 for k, which run off the end. The answer is 2.

Code: Python, Java, C++

1class Solution:
2    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
3        n = len(nums)
4        j = k = 0
5        count = 0
6        for x in nums:
7            while j < n and nums[j] < x + diff:
8                j += 1
9            while k < n and nums[k] < x + 2 * diff:
10                k += 1
11            if j < n and k < n and nums[j] == x + diff and nums[k] == x + 2 * diff:
12                count += 1
13        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

The loop runs n times, and j and k each move right at most n times in total.

Space

O(1)

Only the three positions and the counter are stored.

Other approaches

Hash set of values (time O(n), space O(n))

Put every value in a set and count the x where x + diff and x + 2 * diff are both present. Just as fast and very short, at the cost of O(n) memory.

Brute force (time O(n^3), space O(1))

Try every i < j < k and check both differences. Correct and fine for n = 200, but it does about n^3 / 6 checks.

Common mistakes

  • Resetting j and k for every x. That turns the scan into O(n^2) and throws away the reason the pointers work: the targets only grow.
  • Reading nums[j] or nums[k] after the pointer has run off the end. Check j < n and k < n before comparing.
  • Counting pairs instead of triplets. A value with only x + diff present is not enough; x + 2 * diff must be there too.
  • Worrying about duplicates. The array is strictly increasing, so each value appears once and each x gives at most one triplet.

Frequently asked questions

Why can a triplet be identified by its first value alone?

With distinct sorted values, x + diff and x + 2 * diff each appear at most once, so once x is fixed there is at most one choice for j and one for k.

Why do j and k never need to move left?

The next x is larger, so its targets x + diff and x + 2 * diff are larger too. Every value the pointers already passed is below the old target and therefore below the new one.

Is the hash set solution better?

Both are O(n) time. The set is simpler to write, while the pointer version uses no extra memory and shows how sorted order can replace hashing.