[LeetCode] Number of Arithmetic Triplets

2367. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int arithmeticTriplets(vector<int>& A, int diff) {
int n = A.size(), res = 0;
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
for(int k = j + 1; k < n; k++) {
if(A[j] - A[i] == diff and A[k] - A[j] == diff) res++;
}
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/07/PS/LeetCode/number-of-arithmetic-triplets/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.