{x}
blog image

Number of Arithmetic Triplets

You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:

  • i < j < k,
  • nums[j] - nums[i] == diff, and
  • nums[k] - nums[j] == diff.

Return the number of unique arithmetic triplets.

 

Example 1:

Input: nums = [0,1,4,6,7,10], diff = 3
Output: 2
Explanation:
(1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
(2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. 

Example 2:

Input: nums = [4,5,6,7,8,9], diff = 2
Output: 2
Explanation:
(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.

 

Constraints:

  • 3 <= nums.length <= 200
  • 0 <= nums[i] <= 200
  • 1 <= diff <= 50
  • nums is strictly increasing.

Solution Explanation for Number of Arithmetic Triplets

This problem asks to find the number of arithmetic triplets in a strictly increasing integer array nums with a given common difference diff. An arithmetic triplet (i, j, k) satisfies three conditions:

  1. i < j < k (indices are strictly increasing)
  2. nums[j] - nums[i] == diff
  3. nums[k] - nums[j] == diff

Two solutions are presented below, one using brute force and another with optimized time complexity.

Solution 1: Brute Force

This approach iterates through all possible triplets (i, j, k) and checks if they satisfy the arithmetic triplet conditions. It's straightforward but inefficient for larger input arrays.

Time Complexity: O(n³), where n is the length of nums. Three nested loops iterate through all possible triplets.

Space Complexity: O(1). Constant extra space is used.

Code Implementation (Python):

from itertools import combinations
 
class Solution:
    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
        return sum(b - a == diff and c - b == diff for a, b, c in combinations(nums, 3))

This concise Python code leverages the combinations function from the itertools library to efficiently generate all triplets. The sum function then counts the triplets that satisfy the condition.

Solution 2: Optimized Approach using a HashSet (or array)

This approach significantly improves efficiency by using a HashSet (or boolean array) to track the presence of numbers in nums. It iterates through nums only once. For each nums[i], it checks if nums[i] + diff and nums[i] + 2 * diff exist in the HashSet. If both exist, it's an arithmetic triplet.

Time Complexity: O(n), where n is the length of nums. The algorithm iterates through nums once. HashSet lookups are O(1) on average.

Space Complexity: O(n) to store the HashSet (or boolean array).

Code Implementation (Python):

class Solution:
    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
        seen = set()
        count = 0
        for num in nums:
            if num + diff in seen and num + 2 * diff in seen:
                count += 1
            seen.add(num)
        return count
 

This Python code uses a set (HashSet) for efficient lookups. It iterates through nums, adds each number to the seen set, and checks for the existence of the required numbers to form an arithmetic triplet.

Code Implementations in other languages (Java, C++, Go, TypeScript): The optimized approach can be implemented similarly in other languages. The key is to use a data structure that provides O(1) average-case lookup time, such as a HashSet (Java, C++, TypeScript) or a map (Go). The core logic remains the same: iterate through the input array and check for the existence of the numbers needed to complete the arithmetic triplet using efficient lookups. (The full code implementations for these languages were provided in the original response).

In summary, the optimized approach offers a significant improvement in time complexity, making it much more efficient for larger input arrays compared to the brute-force method. The space complexity increases, but this is usually acceptable for the performance gain.