##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2367.html

# 2367. Number of Arithmetic Triplets

• Difficulty: Easy.
• Related Topics: Array, Hash Table, Two Pointers, Enumeration.
• Similar Questions: Two Sum, 3Sum.

## Problem

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 (Java, C++, Python)

• class Solution {
public int arithmeticTriplets(int[] nums, int diff) {
Set<Integer> set = new HashSet<>();
for (int x : nums) {
}
int ans = 0;
for (int x : nums) {
if (set.contains(x - diff) && set.contains(x + diff)) {
ans++;
}
}
return ans;
}
}

############

class Solution {
public int arithmeticTriplets(int[] nums, int diff) {
boolean[] vis = new boolean[310];
for (int v : nums) {
vis[v] = true;
}
int ans = 0;
for (int v : nums) {
if (vis[v + diff] && vis[v + diff + diff]) {
++ans;
}
}
return ans;
}
}

• class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
s = set(nums)
return sum(v + diff in s and v + diff + diff in s for v in nums)

############

# 2367. Number of Arithmetic Triplets
# https://leetcode.com/problems/number-of-arithmetic-triplets/

class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
n = len(nums)
res = 0

for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff:
res += 1

return res


• class Solution {
public:
int arithmeticTriplets(vector<int>& nums, int diff) {
vector<bool> vis(310);
for (int v : nums) vis[v] = true;
int ans = 0;
for (int v : nums) ans += vis[v + diff] && vis[v + diff + diff];
return ans;
}
};

• func arithmeticTriplets(nums []int, diff int) int {
vis := make([]bool, 310)
for _, v := range nums {
vis[v] = true
}
ans := 0
for _, v := range nums {
if vis[v+diff] && vis[v+diff+diff] {
ans++
}
}
return ans
}

• function arithmeticTriplets(nums: number[], diff: number): number {
let vis = new Array(310).fill(false);
for (const v of nums) {
vis[v] = true;
}
let ans = 0;
for (const v of nums) {
if (vis[v + diff] && vis[v + diff + diff]) {
++ans;
}
}
return ans;
}



Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).