Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.
A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:

0 <= i < j < k < arr.length
|arr[i] - arr[j]| <= a
|arr[j] - arr[k]| <= b
|arr[i] - arr[k]| <= c

Where |x| denotes the absolute value of x.
Return the number of good triplets.
 
Example 1:
Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
Output: 4
Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].

Example 2:
Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1
Output: 0
Explanation: No triplet satisfies all conditions.

 
Constraints:

3 <= arr.length <= 100
0 <= arr[i] <= 1000
0 <= a, b, c <= 1000

Difficulty: EASY
Tags: Data structures

Complete search
Skill types: Data structures

Complete search

Starter code:
class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        

Solutions:
class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		da = defaultdict(set)
		db = defaultdict(set)
		dc = defaultdict(set)
		for i in range(len(arr) - 1):
			for j in range(i + 1, len(arr)):
				dis = abs(arr[j] - arr[i])
				if dis <= a:
					da[i].add(j)
				if dis <= b:
					db[i].add(j)
				if dis <= c:
					dc[i].add(j)
		count = 0
		for i in range(len(arr) - 2):
			for j in da[i]:
				for k in db[j]:
					if k in dc[i]:
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		res = (1 for (i, j, k) in combinations(arr, 3) if abs(i - j) <= a and abs(j - k) <= b and (abs(i - k) <= c))
		return sum(res)


import itertools

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		triplets = 0
		for (i, j, k) in itertools.combinations(arr, 3):
			if abs(i - j) <= a and abs(j - k) <= b and (abs(i - k) <= c):
				triplets += 1
		return triplets


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		(n, result) = (len(arr), 0)
		for i in range(n):
			for k in range(i + 1, n):
				if abs(arr[i] - arr[k]) > c:
					continue
				result += sum((abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b for j in range(i + 1, k)))
		return result


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				if abs(arr[i] - arr[j]) <= a:
					for k in range(j + 1, len(arr)):
						if abs(arr[k] - arr[j]) <= b and abs(arr[k] - arr[i]) <= c:
							count += 1
		return count


from itertools import combinations

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ans = (1 for (i, j, k) in combinations(arr, 3) if abs(i - j) <= a and abs(j - k) <= b and (abs(i - k) <= c))
		return sum(ans)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(0, len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				a_bool = abs(arr[i] - arr[j]) <= a
				if a_bool:
					for k in range(j + 1, len(arr)):
						b_bool = abs(arr[j] - arr[k]) <= b
						c_bool = abs(arr[i] - arr[k]) <= c
						if all((b_bool, c_bool)):
							count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr, a, b, c):
		(ln, res) = (len(arr), 0)
		for i in range(ln - 2):
			n = arr[i]
			for j in range(i + 1, ln - 1):
				n2 = arr[j]
				for k in range(j + 1, ln):
					n3 = arr[k]
					if abs(n - n2) <= a and abs(n2 - n3) <= b and (abs(n - n3) <= c):
						res += 1
		return res


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		s = 0
		for (i, m) in enumerate(arr):
			for (j, n) in enumerate(arr[i + 1:]):
				for (k, q) in enumerate(arr[i + j + 2:]):
					if abs(m - n) <= a and abs(n - q) <= b and (abs(m - q) <= c):
						s += 1
		return s


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		goodPairs = 0
		for (item, val) in enumerate(arr):
			restNums = arr[item + 1:]
			for (item2, val2) in enumerate(restNums):
				restNums2 = restNums[item2 + 1:]
				for (item3, val3) in enumerate(restNums2):
					if abs(val - val2) <= a:
						if abs(val2 - val3) <= b:
							if abs(val - val3) <= c:
								goodPairs += 1
		return goodPairs


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for (i, x) in enumerate(arr):
			for (j, y) in enumerate(arr[i + 1:], i + 1):
				for (k, z) in enumerate(arr[j + 1:], j + 1):
					if abs(x - y) <= a and abs(y - z) <= b and (abs(x - z) <= c):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:

		def isGood(i, j, a):
			return abs(arr[i] - arr[j]) <= a
		ret = 0
		if not arr:
			return ret
		n = len(arr)
		for i in range(0, n):
			for j in range(i + 1, n):
				if isGood(i, j, a):
					for k in range(j + 1, n):
						if isGood(j, k, b) and isGood(i, k, c):
							ret += 1
		return ret


class Solution:

	def countGoodTripletsCase1(self, arr: List[int], a: int, b: int) -> int:
		count = 0
		for j in range(1, len(arr) - 1):
			l = sum((abs(arr[i] - arr[j]) <= a for i in range(0, j)))
			r = sum((abs(arr[j] - arr[k]) <= b for k in range(j + 1, len(arr))))
			count += l * r
		return count

	def countGoodTripletsCase2(self, arr: List[int], a: int, b: int, c: int) -> int:
		index = sorted(range(len(arr)), key=lambda k: arr[k])
		count = 0
		for j in range(len(arr) - 1, 0, -1):
			for i in range(j - 1, -1, -1):
				if index[j] == j:
					break
				(index[j], index[i]) = (index[i], index[j])
			for k in range(j + 2, len(arr)):
				if arr[index[k - 1]] > arr[index[k]]:
					(index[k - 1], index[k]) = (index[k], index[k - 1])
			i_min = next((i for i in range(0, j) if arr[index[i]] >= arr[j] - a), j)
			i_max = next((i for i in range(i_min, j) if arr[index[i]] > arr[j] + a), j)
			(ic_min, ic_max) = (i_min, i_min)
			k = next((i for i in range(j + 1, len(arr)) if arr[index[i]] >= arr[j] - b), len(arr))
			while k < len(arr) and arr[index[k]] <= arr[j] + b:
				while ic_min < i_max and arr[index[ic_min]] < arr[index[k]] - c:
					ic_min += 1
				while ic_max < i_max and arr[index[ic_max]] <= arr[index[k]] + c:
					ic_max += 1
				count += ic_max - ic_min
				k += 1
		return count

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		if c >= a + b:
			return self.countGoodTripletsCase1(arr, a, b)
		else:
			return self.countGoodTripletsCase2(arr, a, b, c)


from typing import List

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		total_good_triplets = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					if abs(arr[i] - arr[j]) <= a and abs(arr[k] - arr[j]) <= b and (abs(arr[i] - arr[k]) <= c):
						total_good_triplets += 1
		return total_good_triplets


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		(ans, n) = (0, len(arr))
		for x in range(n - 2):
			for y in range(x + 1, n - 1):
				for z in range(y + 1, n):
					if abs(arr[x] - arr[y]) <= a and abs(arr[y] - arr[z]) <= b and (abs(arr[x] - arr[z]) <= c):
						ans += 1
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		pair = {p: dict() for p in (a, b, c)}
		n = len(arr)
		for i in range(n):
			for j in range(i + 1, n):
				d = abs(arr[i] - arr[j])
				for p in (a, b, c):
					if d <= p:
						if i not in pair[p]:
							pair[p][i] = set()
						pair[p][i].add(j)
		ans = 0
		empty = set()
		for (i, js) in list(pair[a].items()):
			for j in js:
				ans += len(pair[b].get(j, empty) & pair[c].get(i, empty))
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		(count, n) = (0, len(arr))
		for i in range(0, n - 2):
			for j in range(i + 1, n - 1):
				for k in range(j + 1, n):
					(x, y, z) = (arr[i], arr[j], arr[k])
					if abs(x - y) <= a and abs(y - z) <= b and (abs(z - x) <= c):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		from itertools import combinations
		result = 0
		for x in combinations(arr, 3):
			if abs(x[0] - x[1]) <= a and abs(x[1] - x[2]) <= b and (abs(x[2] - x[0]) <= c):
				result += 1
		return result


class Solution:

	def countGoodTriplets(self, arr, a, b, c):
		return sum((abs(n - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(n - arr[k]) <= c) for (i, n) in enumerate(arr) for j in range(i + 1, len(arr) - 1) for k in range(j + 1, len(arr))))


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		combs = combinations(arr, 3)
		count = 0
		for i in combs:
			if abs(i[0] - i[1]) <= a and abs(i[1] - i[2]) <= b and (abs(i[0] - i[2]) <= c):
				count += 1
		return count


from collections import defaultdict

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		da = defaultdict(set)
		db = defaultdict(set)
		dc = defaultdict(set)
		count = 0
		for i in range(n - 1):
			for j in range(i + 1, n):
				if abs(arr[i] - arr[j]) <= a:
					da[i].add(j)
				if abs(arr[i] - arr[j]) <= b:
					db[i].add(j)
				if abs(arr[i] - arr[j]) <= c:
					dc[i].add(j)
		for i in da:
			for j in da[i]:
				for k in db[j]:
					if k in dc[i]:
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		di = defaultdict(set)
		dj = defaultdict(set)
		dk = defaultdict(set)
		arr = [(i, x) for (i, x) in enumerate(arr)]
		arr = sorted(arr, key=lambda x: x[1])
		n = len(arr)
		for (i, tup_a) in enumerate(arr):
			(iorig, x) = tup_a
			j = i + 1
			while True:
				if j == n:
					break
				if abs(x - arr[j][1]) <= a:
					di[iorig].add(arr[j][0])
				if abs(x - arr[j][1]) <= b:
					dj[iorig].add(arr[j][0])
				if abs(x - arr[j][1]) <= c:
					dk[iorig].add(arr[j][0])
				j += 1
			j = i - 1
			while True:
				if j < 0:
					break
				if abs(x - arr[j][1]) <= a:
					di[iorig].add(arr[j][0])
				if abs(x - arr[j][1]) <= b:
					dj[iorig].add(arr[j][0])
				if abs(x - arr[j][1]) <= c:
					dk[iorig].add(arr[j][0])
				j -= 1
		o = 0
		for i in range(n):
			for j in di[i]:
				for k in dj[j]:
					if k in dk[i]:
						if i >= 0 and j > i and (k > j) and (k < n):
							o += 1
		return o


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		return sum([abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c) for i in range(len(arr)) for j in range(i + 1, len(arr)) for k in range(j + 1, len(arr))])


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		combos = combinations(arr, 3)
		final = []
		for i in combos:
			if abs(i[0] - i[1]) <= a and abs(i[1] - i[2]) <= b and (abs(i[0] - i[2]) <= c):
				final.append(i)
		return len(final)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ans = []
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c):
						ans.append((arr[i], arr[j], arr[k]))
		return len(ans)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		pointer1 = 0
		pointer2 = 1
		pointer3 = 2
		res = 0
		length = len(arr)
		while pointer1 < length - 2:
			while pointer2 < length - 1:
				while pointer3 < length:
					if abs(arr[pointer1] - arr[pointer2]) <= a and abs(arr[pointer2] - arr[pointer3]) <= b and (abs(arr[pointer1] - arr[pointer3]) <= c):
						res += 1
					pointer3 += 1
				pointer2 += 1
				pointer3 = pointer2 + 1
			pointer1 += 1
			pointer2 = pointer1 + 1
			pointer3 = pointer2 + 1
		return res


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		dict_a = defaultdict(set)
		dict_b = defaultdict(set)
		dict_c = defaultdict(set)
		for i in range(len(arr) - 1):
			for j in range(i + 1, len(arr)):
				if abs(arr[i] - arr[j]) <= a:
					dict_a[i].add(j)
				if abs(arr[i] - arr[j]) <= b:
					dict_b[i].add(j)
				if abs(arr[i] - arr[j]) <= c:
					dict_c[i].add(j)
		for i in range(len(arr) - 2):
			for j in dict_a[i]:
				for k in dict_b[j]:
					if k in dict_c[i]:
						count = count + 1
		return count


from collections import defaultdict

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		lt_a = defaultdict(set)
		lt_b = defaultdict(set)
		lt_c = defaultdict(set)
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				if abs(arr[j] - arr[i]) <= a:
					lt_a[i].add(j)
				if abs(arr[j] - arr[i]) <= b:
					lt_b[i].add(j)
				if abs(arr[j] - arr[i]) <= c:
					lt_c[i].add(j)
		num_good_triplets = 0
		for i in lt_a:
			for j in lt_a[i]:
				for k in lt_b[j]:
					if k in lt_c[i]:
						num_good_triplets += 1
		return num_good_triplets


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		print(arr)
		print((a, b, c))
		resp = []
		count = 0
		comb = itertools.combinations(arr, 3)
		for i in comb:
			if abs(i[0] - i[1]) <= a and abs(i[1] - i[2]) <= b and (abs(i[0] - i[2]) <= c):
				resp.append(i)
				count = count + 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		return sum((abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[k] - arr[i]) <= c) for (i, j, k) in itertools.combinations(list(range(len(arr))), 3)))


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		good = 0
		arrLen = len(arr)
		for i in range(arrLen - 2):
			for j in range(i + 1, arrLen - 1):
				for k in range(j + 1, arrLen):
					if arr[i] - arr[j] <= a and arr[j] - arr[i] <= a:
						if arr[j] - arr[k] <= b and arr[k] - arr[j] <= b:
							if arr[i] - arr[k] <= c and arr[k] - arr[i] <= c:
								good += 1
		return good


from itertools import combinations

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		combs = combinations(arr, 3)
		return sum((abs(comb[0] - comb[1]) <= a and abs(comb[1] - comb[2]) <= b and (abs(comb[0] - comb[2]) <= c) for comb in combs))


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		goods = 0
		for i in range(1, len(arr) - 1):
			first = arr[:i]
			last = arr[i + 1:]
			(ij_pairs, jk_pairs) = ([], [])
			for n in first:
				if abs(arr[i] - n) <= a:
					ij_pairs.append((n, arr[i]))
			for n in last:
				if abs(arr[i] - n) <= b:
					jk_pairs.append((arr[i], n))
			for ij in ij_pairs:
				for jk in jk_pairs:
					if abs(ij[0] - jk[1]) <= c:
						goods += 1
		return goods


from itertools import combinations

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		combs = combinations(arr, 3)
		counter = 0
		for i in list(combs):
			if abs(i[0] - i[1]) <= a and abs(i[1] - i[2]) <= b and (abs(i[0] - i[2]) <= c):
				counter += 1
		return counter


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		return len(list(filter(lambda x: abs(x[0] - x[1]) <= a and abs(x[1] - x[2]) <= b and (abs(x[0] - x[2]) <= c), combinations(arr, 3))))


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		d = 0
		e = len(arr)
		for i in range(e):
			for j in range(e):
				if j > i:
					for k in range(e):
						if k > j:
							if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c):
								d += 1
						else:
							pass
				else:
					pass
		return d


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		ans = 0
		for i in range(n):
			for j in range(i + 1, n):
				for k in range(j + 1, n):
					val1 = arr[j] - arr[k]
					val2 = arr[i] - arr[k]
					val = arr[i] - arr[j]
					if abs(val) <= a and abs(val1) <= b and (abs(val2) <= c):
						ans += 1
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		total = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for z in range(j + 1, len(arr)):
					new_arr = [arr[i], arr[j], arr[z]]
					if abs(new_arr[0] - new_arr[1]) <= a and abs(new_arr[1] - new_arr[2]) <= b and (abs(new_arr[0] - new_arr[2]) <= c):
						total += 1
		return total


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		from itertools import combinations
		l = list(combinations(arr, 3))
		res = []
		for tup in l:
			if abs(tup[0] - tup[1]) <= a and abs(tup[0] - tup[2]) <= c and (abs(tup[2] - tup[1]) <= b):
				res.append(tup)
		return len(res)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count_ = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					diff1 = abs(arr[i] - arr[j])
					diff2 = abs(arr[j] - arr[k])
					diff3 = abs(arr[i] - arr[k])
					if diff1 <= a and diff2 <= b and (diff3 <= c):
						count_ += 1
		return count_


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ans = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					one = abs(arr[i] - arr[j])
					two = abs(arr[j] - arr[k])
					three = abs(arr[i] - arr[k])
					if one <= a and two <= b and (three <= c):
						ans += 1
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		res = 0
		for x in range(len(arr)):
			for y in range(x + 1, len(arr)):
				for z in range(y + 1, len(arr)):
					if (abs(arr[x] - arr[y]) <= a) & (abs(arr[y] - arr[z]) <= b) & (abs(arr[x] - arr[z]) <= c):
						res += 1
		return res


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		L = len(arr)
		if L < 3:
			return 0
		count = 0

		def check(i, j, k):
			if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c):
				return True
			return False
		for i in range(L - 2):
			for j in range(i + 1, L - 1):
				for k in range(j + 1, L):
					if check(i, j, k):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		result = 0
		n = len(arr)
		for i1 in range(n):
			for i2 in range(i1 + 1, n):
				for i3 in range(i2 + 1, n):
					if abs(arr[i1] - arr[i2]) <= a and abs(arr[i2] - arr[i3]) <= b and (abs(arr[i1] - arr[i3]) <= c):
						result += 1
		return result


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		tup_list = []
		for idx1 in range(len(arr)):
			num1 = arr[idx1]
			arr_dict = {}
			for idx2 in range(idx1 + 1, len(arr)):
				num2 = arr[idx2]
				if abs(num1 - num2) <= c:
					if a in arr_dict:
						for pot_num2 in arr_dict[a]:
							if abs(pot_num2 - num2) <= b:
								tup = (num1, pot_num2, num2)
								tup_list.append(tup)
				if abs(num1 - num2) <= a:
					if a in arr_dict:
						num2_pots = arr_dict[a]
						num2_pots.append(num2)
					else:
						arr_dict[a] = [num2]
		return len(tup_list)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		cnt = 0
		n = len(arr)
		for i in range(n - 2):
			for j in range(i + 1, n - 1):
				for k in range(j + 1, n):
					d1 = abs(arr[i] - arr[j])
					d2 = abs(arr[j] - arr[k])
					d3 = abs(arr[k] - arr[i])
					if d1 <= a and d2 <= b and (d3 <= c):
						cnt += 1
		return cnt


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		if not arr:
			return 0
		result = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					first = abs(arr[i] - arr[j]) <= a
					second = abs(arr[j] - arr[k]) <= b
					third = abs(arr[i] - arr[k]) <= c
					if first and second and third:
						result += 1
		return result


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		l = len(arr)
		scount = 0
		for i in range(l):
			for j in range(i + 1, l):
				for k in range(j + 1, l):
					da = abs(arr[i] - arr[j])
					db = abs(arr[j] - arr[k])
					dc = abs(arr[i] - arr[k])
					if da <= a and db <= b and (dc <= c):
						scount += 1
		return scount


from collections import defaultdict

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ij = defaultdict(set)
		jk = defaultdict(set)
		ik = defaultdict(set)
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				if abs(arr[i] - arr[j]) <= a:
					ij[i].add(j)
				if abs(arr[i] - arr[j]) <= b:
					jk[i].add(j)
				if abs(arr[i] - arr[j]) <= c:
					ik[i].add(j)
		ans = 0
		for i in ij:
			for j in ij[i]:
				for k in jk[j]:
					if k in ik[i]:
						ans += 1
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		goods = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					condition1 = abs(arr[i] - arr[j]) <= a
					condition2 = abs(arr[j] - arr[k]) <= b
					condition3 = abs(arr[i] - arr[k]) <= c
					if condition1 and condition2 and condition3:
						goods += 1
		return goods


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		cnt = 0
		for (in_i, i) in enumerate(arr):
			for (in_j, j) in enumerate(arr):
				if in_i < in_j:
					for (in_k, k) in enumerate(arr):
						if in_i < in_j < in_k:
							if abs(i - j) <= a and abs(j - k) <= b and (abs(i - k) <= c):
								cnt += 1
		return cnt


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		good_count = 0
		i = 0
		j = 1
		k = 2
		if len(arr) > 2:
			while i + 2 < len(arr):
				if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c):
					good_count += 1
				if k != len(arr) - 1:
					k += 1
				elif k == len(arr) - 1 and j + 1 != k:
					j += 1
					k = j + 1
				elif j == len(arr) - 2:
					i += 1
					j = i + 1
					k = j + 1
		return good_count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					f = abs(arr[i] - arr[j]) <= a
					s = abs(arr[j] - arr[k]) <= b
					t = abs(arr[i] - arr[k]) <= c
					count += f and s and t
		return count


from itertools import permutations as p

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c):
						count += 1
		return count


from itertools import combinations

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		results = []
		all_indx = list(range(0, len(arr), 1))
		all_triplets = list(combinations(all_indx, 3))
		for idx_tuple in all_triplets:
			arr_i = arr[idx_tuple[0]]
			arr_j = arr[idx_tuple[1]]
			arr_k = arr[idx_tuple[2]]
			if abs(arr_i - arr_j) <= a and abs(arr_j - arr_k) <= b and (abs(arr_i - arr_k) <= c):
				results.append(idx_tuple)
		l_results = len(results)
		return l_results


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				check_a = abs(arr[i] - arr[j]) <= a
				for k in range(j + 1, len(arr)):
					check_b = abs(arr[j] - arr[k]) <= b
					check_c = abs(arr[i] - arr[k]) <= c
					if all([check_a, check_b, check_c]):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(0, len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					check1 = abs(arr[i] - arr[j]) <= a
					check2 = abs(arr[j] - arr[k]) <= b
					check3 = abs(arr[i] - arr[k]) <= c
					count += 1 if check1 and check2 and check3 else 0
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		L = len(arr)
		output = 0
		for i in range(L - 2):
			for j in range(i + 1, L - 1):
				for k in range(j + 1, L):
					if (abs(arr[i] - arr[j]) <= a) + (abs(arr[j] - arr[k]) <= b) + (abs(arr[i] - arr[k]) <= c) == 3:
						output += 1
		return output


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		cnt = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					test = 0
					if -a <= arr[i] - arr[j] <= a:
						test += 1
					if -b <= arr[j] - arr[k] <= b:
						test += 1
					if -c <= arr[i] - arr[k] <= c:
						test += 1
					if test == 3:
						cnt += 1
		return cnt


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for v in itertools.combinations([i for i in range(len(arr))], 3):
			if abs(arr[v[0]] - arr[v[1]]) <= a and abs(arr[v[1]] - arr[v[2]]) <= b and (abs(arr[v[0]] - arr[v[2]]) <= c):
				count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		comb = combinations(list(range(len(arr))), 3)
		condition = lambda i, j, k: i < j and j < k and (abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c)
		return len([(i, j, k) for (i, j, k) in comb if condition(i, j, k)])


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		combs = combinations(arr, 3)
		res = 0
		for comb in combs:
			if self.goodTriple(comb, a, b, c):
				res += 1
		return res

	def goodTriple(self, triplet, a, b, c):
		x = triplet[0]
		y = triplet[1]
		z = triplet[2]
		if -a <= x - y <= a and -b <= y - z <= b and (-c <= x - z <= c):
			return True
		return False


import itertools

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		num = 0
		for triplet in itertools.combinations(arr, 3):
			if abs(triplet[0] - triplet[1]) <= a and abs(triplet[1] - triplet[2]) <= b and (abs(triplet[0] - triplet[2]) <= c):
				num += 1
		return num


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		i = 0
		ans = 0

		def tripleIsGood(i, j, k):
			if 0 <= i < j < k and abs(arr[i] - arr[j]) <= a and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c):
				return True
		for i in range(0, len(arr) - 2):
			k = len(arr) - 1
			while i + 1 != k:
				if i + 1 != k:
					for j in range(i + 1, k):
						if tripleIsGood(i, j, k):
							ans += 1
				k -= 1
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		i = 0
		res = 0
		while i < len(arr) - 2:
			j = i + 1
			while j < len(arr) - 1:
				k = j + 1
				while k < len(arr):
					if (abs(arr[i] - arr[j]) <= a) & (abs(arr[j] - arr[k]) <= b) & (abs(arr[i] - arr[k]) <= c):
						res += 1
					k += 1
				j += 1
			i += 1
		return res


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		size = len(arr)
		good_count = 0
		for i in range(size - 2):
			for j in range(i + 1, size - 1):
				for k in range(j + 1, size):
					ok_a = abs(arr[i] - arr[j]) <= a
					ok_b = abs(arr[j] - arr[k]) <= b
					ok_c = abs(arr[i] - arr[k]) <= c
					if all((ok_a, ok_b, ok_c)):
						good_count += 1
		return good_count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					goodA = abs(arr[i] - arr[j]) <= a
					goodB = abs(arr[j] - arr[k]) <= b
					goodC = abs(arr[i] - arr[k]) <= c
					if all((goodA, goodB, goodC)):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					res1 = abs(arr[i] - arr[j]) <= a
					res2 = abs(arr[j] - arr[k]) <= b
					res3 = abs(arr[i] - arr[k]) <= c
					if all((res1, res2, res3)):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		ij_idx = [set() for _ in range(n)]
		jk_ids = [set() for _ in range(n)]
		ik_ids = [set() for _ in range(n)]
		for i in range(0, n - 1):
			for j in range(i + 1, n):
				d = abs(arr[j] - arr[i])
				if d <= a:
					ij_idx[i].add(j)
				if d <= b:
					jk_ids[i].add(j)
				if d <= c:
					ik_ids[j].add(i)
		result = 0
		for i in range(n - 2):
			for j in ij_idx[i]:
				for k in jk_ids[j]:
					if i in ik_ids[k]:
						result += 1
		return result


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		length = len(arr)
		initial_last_step_of_i = length - 2
		initial_first_step_of_i = 0
		initial_last_step_of_j = length - 1
		initial_first_step_of_j = 1
		initial_last_step_of_k = length
		initial_first_step_of_k = 2
		triplets = 0
		first_step_of_j = initial_first_step_of_j
		all_i = [arr[i] for i in range(initial_first_step_of_i, initial_last_step_of_i)]
		for i in range(initial_first_step_of_i, initial_last_step_of_i):
			first_step_of_k = initial_first_step_of_k
			for j in range(first_step_of_j, initial_last_step_of_j):
				for k in range(first_step_of_k, initial_last_step_of_k):
					if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[i] - arr[k]) <= c) and (i < j < k):
						triplets += 1
				first_step_of_k += 1
			first_step_of_j += 1
		return triplets


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ans = 0
		for i in range(0, len(arr)):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					conditions = [abs(arr[i] - arr[j]) <= a, abs(arr[j] - arr[k]) <= b, abs(arr[i] - arr[k]) <= c]
					if all(conditions):
						ans += 1
		return ans


class Solution:

	def isGood(self, ai, aj, ak, a, b, c, length) -> bool:
		if not abs(ai - aj) <= a:
			return False
		elif not abs(aj - ak) <= b:
			return False
		elif not abs(ai - ak) <= c:
			return False
		else:
			return True

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		res = 0
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				for k in range(j + 1, len(arr)):
					res += int(self.isGood(arr[i], arr[j], arr[k], a, b, c, len(arr)))
		return res


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:

		def helper(arr):
			if abs(arr[0] - arr[1]) <= a and abs(arr[1] - arr[2]) <= b and (abs(arr[0] - arr[2]) <= c):
				return True
			else:
				return False
		result = 0
		for i in range(len(arr) - 2):
			for j in range(i + 1, len(arr) - 1):
				for k in range(j + 1, len(arr)):
					idx = (arr[i], arr[j], arr[k])
					if helper(idx):
						result += 1
		return result


from itertools import combinations

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		num = len(arr)
		comb = combinations(arr, 3)
		counter = 0
		for m in comb:
			comb1 = list(m)
			i = comb1[0]
			j = comb1[1]
			k = comb1[2]
			if (abs(i - j) <= a) & (abs(j - k) <= b) & (abs(i - k) <= c):
				counter += 1
		return counter


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		C = list(combinations(arr, 3))
		C = [self.checkGood(x, a, b, c) for x in C]
		C = [x for x in C if x != None]
		return len(C)

	def checkGood(self, tup, a, b, c):
		if abs(tup[0] - tup[1]) <= a and abs(tup[0] - tup[2]) <= c and (abs(tup[2] - tup[1]) <= b):
			return tup


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		goodTriplet = 0
		for i in range(n - 2):
			for j in range(i + 1, n - 1):
				for k in range(j + 1, n):
					cond_1 = abs(arr[i] - arr[j]) <= a
					cond_2 = abs(arr[j] - arr[k]) <= b
					cond_3 = abs(arr[i] - arr[k]) <= c
					if all([cond_1, cond_2, cond_3]):
						goodTriplet += 1
		return goodTriplet


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ans = 0
		for (i1, v1) in enumerate(arr):
			for (i2, v2) in enumerate(arr[i1 + 1:]):
				if abs(v1 - v2) <= a:
					for v3 in arr[i1 + i2 + 2:]:
						if abs(v1 - v3) <= c and abs(v2 - v3) <= b:
							ans += 1
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		total = 0
		if n < 3:
			return None
		for i in range(n - 2):
			for j in range(i + 1, n - 1):
				for k in range(j + 1, n):
					A = abs(arr[i] - arr[j])
					B = abs(arr[j] - arr[k])
					C = abs(arr[i] - arr[k])
					if A <= a and B <= b and (C <= c):
						total += 1
		return total


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		nums = [0] * 1001
		ans = 0
		length = len(arr)
		for j in range(length):
			for k in range(j + 1, length):
				if abs(arr[j] - arr[k]) <= b:
					r = min(arr[j] + a, arr[k] + c, 1000)
					l = max(arr[j] - a, arr[k] - c, 0)
					if r >= l:
						ans += nums[r] if l == 0 else nums[r] - nums[l - 1]
			for m in range(arr[j], 1001):
				nums[m] += 1
		return ans


class Solution:

	def countGoodTriplets(self, array: List[int], a: int, b: int, c: int) -> int:
		triplets = []
		for i in range(len(array) - 2):
			for j in range(i + 1, len(array) - 1):
				for k in range(j + 1, len(array)):
					triplets.append((array[i], array[j], array[k]))
		count = 0
		for (first, second, third) in triplets:
			if abs(first - second) <= a and abs(second - third) <= b and (abs(first - third) <= c):
				count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ij = {}
		total = 0
		for q in range(len(arr) - 1):
			for r in range(q + 1, len(arr)):
				v = abs(arr[q] - arr[r])
				if v <= a:
					if r in ij:
						ij[r].append(q)
					else:
						ij[r] = [q]
				if v <= b:
					if q in ij:
						for i in ij[q]:
							if abs(arr[i] - arr[r]) <= c:
								total += 1
		return total


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for (i, n) in enumerate(arr):
			if i > len(arr) - 3:
				break
			tmp = arr[i + 1:]
			for (j, m) in enumerate(tmp[:-1]):
				if abs(n - m) > a:
					continue
				for o in tmp[j + 1:]:
					if abs(m - o) <= b and abs(n - o) <= c:
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		cnt = 0
		length = len(arr)
		for idx in range(length):
			first = arr[idx]
			for ydx in range(idx + 1, length):
				second = arr[ydx]
				if abs(first - second) <= a:
					for kdx in range(ydx + 1, length):
						third = arr[kdx]
						if abs(first - third) <= c and abs(second - third) <= b:
							cnt += 1
		return cnt


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		res = 0
		for (i, ai) in enumerate(arr):
			for (k, ak) in enumerate(arr[i + 2:], i + 2):
				if abs(ak - ai) <= c:
					for (j, aj) in enumerate(arr[i + 1:k], i + 1):
						if abs(ai - aj) <= a and abs(ak - aj) <= b:
							res += 1
		return res


def bruteforce(arr, a, b, c):
	count = 0
	for i in range(len(arr) - 2):
		arri = arr[i]
		for j in range(i + 1, len(arr) - 1):
			if abs(arri - arr[j]) > a:
				continue
			arrj = arr[j]
			for k in range(j + 1, len(arr)):
				if abs(arrj - arr[k]) <= b and abs(arri - arr[k]) <= c:
					count += 1
	return count

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		return bruteforce(arr, a, b, c)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		good = 0
		for i in range(0, n):
			ai = arr[i]
			for j in range(i + 1, n):
				aj = arr[j]
				if abs(ai - aj) > a:
					continue
				for k in range(j + 1, n):
					ak = arr[k]
					if abs(aj - ak) > b:
						continue
					if abs(ak - ai) > c:
						continue
					good += 1
		return good


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		la = len(arr)
		cnt = 0
		for i in range(la - 2):
			for j in range(i + 1, la - 1):
				if abs(arr[i] - arr[j]) <= a:
					for k in range(j + 1, la):
						if abs(arr[j] - arr[k]) <= b:
							if abs(arr[i] - arr[k]) <= c:
								cnt += 1
		return cnt


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ma = {}
		(d, psum) = (set(), [])

		def add(p):
			for i in range(ma[p], len(psum)):
				psum[i] += 1

		def get(l, r):
			if l > r:
				return 0
			return psum[ma[r]] - psum[ma[l] - 1]
		n = len(arr)
		d.add(-sys.maxsize)
		for i in arr:
			d.add(i)
			d.add(i + a)
			d.add(i - a)
			d.add(i + c)
			d.add(i - c)
		d = sorted(list(d))
		for (i, v) in enumerate(d):
			ma[v] = i
		psum = [0] * len(d)
		ans = 0
		for j in range(n):
			for k in range(j + 1, n):
				if abs(arr[j] - arr[k]) > b:
					continue
				l = max(arr[j] - a, arr[k] - c)
				r = min(arr[j] + a, arr[k] + c)
				ans += get(l, r)
			add(arr[j])
		return ans


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		ot = 0
		n = len(arr)
		for i in range(0, n - 2):
			for j in range(i + 1, n - 1):
				if abs(arr[i] - arr[j]) > a:
					continue
				for k in range(j + 1, n):
					if abs(arr[j] - arr[k]) > b:
						continue
					if abs(arr[i] - arr[k]) > c:
						continue
					ot += 1
		return ot


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		cou = 0
		(left, right) = (0, len(arr) - 1)
		while left < len(arr) - 2:
			if left == right - 1:
				left += 1
				right = len(arr) - 1
				continue
			if abs(arr[left] - arr[right]) > c:
				right -= 1
				continue
			cou += sum([abs(arr[left] - arr[i]) <= a and abs(arr[i] - arr[right]) <= b for i in range(left + 1, right)])
			right -= 1
		return cou
		return sum([abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and (abs(arr[k] - arr[i]) <= c) for i in range(len(arr)) for j in range(i + 1, len(arr)) for k in range(j + 1, len(arr))])


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n = len(arr)
		count = 0
		for i in range(n):
			for j in range(i + 1, n):
				val1 = abs(arr[i] - arr[j])
				if val1 <= a:
					for k in range(j + 1, n):
						val2 = abs(arr[j] - arr[k])
						if val2 <= b:
							val3 = abs(arr[i] - arr[k])
							if val3 <= c:
								count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		good_triplets = 0
		arr_len = len(arr)
		for i in range(arr_len - 2):
			for j in range(i + 1, arr_len - 1):
				if j < i:
					break
				if abs(arr[i] - arr[j]) <= a:
					for k in range(j + 1, arr_len):
						if k < j:
							break
						if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
							triplet = [i, j, k]
							good_triplets += 1
		return good_triplets


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		size = len(arr)
		result = 0
		res = []
		for i in range(size - 2):
			for j in range(i + 1, size - 1):
				if abs(arr[i] - arr[j]) <= a:
					for k in range(j + 1, size):
						if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
							result += 1
							res.append((arr[i], arr[j], arr[k]))
		return result


class Solution:

	def countGoodTriplets(self, arr, a, b, c):
		val = []
		for i in range(0, len(arr)):
			for j in range(i + 1, len(arr)):
				if abs(arr[i] - arr[j]) <= a:
					for k in range(j + 1, len(arr)):
						if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
							val.append([arr[i], arr[j], arr[k]])
		return len(val)


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for (i, n_i) in enumerate(arr[:-2]):
			for j in range(i + 1, len(arr) - 1):
				n_j = arr[j]
				for n_k in arr[j + 1:]:
					if abs(n_i - n_j) <= a and abs(n_j - n_k) <= b and (abs(n_i - n_k) <= c):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for (i, arr_i) in enumerate(arr):
			for (j, arr_j) in enumerate(arr[i + 1:]):
				for arr_k in arr[i + j + 2:]:
					if abs(arr_i - arr_j) <= a and abs(arr_j - arr_k) <= b and (abs(arr_i - arr_k) <= c):
						count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		n_good = 0
		for (n, i) in enumerate(arr):
			for (o, j) in enumerate(arr[n + 1:]):
				for k in arr[n + o + 2:]:
					if abs(i - j) <= a and abs(j - k) <= b and (abs(i - k) <= c):
						n_good = n_good + 1
		return n_good


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		good_triplet_count = 0
		for ind_1 in range(0, len(arr)):
			if ind_1 + 2 <= len(arr):
				for ind_2 in range(ind_1 + 1, len(arr)):
					if abs(arr[ind_1] - arr[ind_2]) <= a:
						for ind_3 in range(ind_2 + 1, len(arr)):
							if abs(arr[ind_1] - arr[ind_2]) <= a and abs(arr[ind_2] - arr[ind_3]) <= b and (abs(arr[ind_1] - arr[ind_3]) <= c):
								good_triplet_count = good_triplet_count + 1
		return good_triplet_count


from itertools import combinations

class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		count = 0
		for (A, B, C) in combinations(arr, 3):
			if abs(A - B) <= a and abs(B - C) <= b and (abs(C - A) <= c):
				count += 1
		return count


class Solution:

	def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
		if len(arr) < 3:
			return 0
		result = 0
		for i in range(len(arr) - 2):
			num1 = arr[i]
			for j in range(i + 1, len(arr) - 1):
				num2 = arr[j]
				for k in range(j + 1, len(arr)):
					num3 = arr[k]
					if abs(num1 - num2) <= a and abs(num2 - num3) <= b and (abs(num3 - num1) <= c):
						result += 1
		return result