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: introductory

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:
        # size = len(arr)
        # res = 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)):
        #                 res += 1   
        # return res
        
        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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        #print(arr)
        #print(a)
        #print(b)
        #print(c)
        count = 0
        lis = []
        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[j] - arr[k])<= b and abs(arr[i] - arr[k]) <= c:
                            lis.append((arr[i],arr[j],arr[k]))
        return(len(lis))
        
                        
                    
                   
                    #if abs(arr[i] - arr[j]) <= a  and  abs(arr[j] - arr[k]) <= b and  abs(arr[i] - arr[k]) <= c:
                        
                     
                        #lis.append((arr[i],arr[j],arr[k]))
        #print(lis)
                        
                        
            
                        


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

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)
                


# Brute Force Solution
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
                
                # break case
                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:
        totallen = len(arr)
        res = 0
        
#         for i in range(totallen-2):
#             for j in range(i+1,totallen-1):
                
#                 for k in range(j+1,totallen):
                    
#                     if abs(arr[i]-arr[j]) <=a and abs(arr[j]-arr[k]) <=b and abs(arr[i]-arr[k]) <=c:
#                         res += 1
                        
#         return res

        for i in range(totallen-2):
            for j in range(i+1,totallen-1):
                if abs(arr[i]-arr[j]) <=a :
                    for k in range(j+1,totallen):
                        if abs(arr[j]-arr[k]) <=b and abs(arr[i]-arr[k]) <=c:
                            res += 1
                        
        return res

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


class Solution:
    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)):
                if abs(arr[i]-arr[j] <= a):
                    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[k]-arr[i])<=c:
                            res+= 1
        return res

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
        length = len(arr)
        for i in range(0, length):
            for j in range(i+1, length):
                for k in range(j+1, length):
                    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 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)

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        output = 0
        l = len(arr)
        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) and (abs(arr[j]-arr[k]) <= b) and (abs(arr[i]-arr[k]) <= c):
                        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)) : 
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and 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:
        size = len(arr)
        ctr = 0
        for i in range(size-2):
            for j in range(i+1, size-1):
                for k in range(j+1, size):
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        ctr += 1
        return ctr

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        l = len(arr)
        n = 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 and 
                    abs(arr[j] - arr[k]) <= b and 
                    abs(arr[i] -arr[k]) <= c):
                        n += 1
        return n

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[k]-arr[i])<=c:
                        count+=1
        return count

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:
        res = 0
        for i in range(len(arr)):
            for j in range(i+1, len(arr)):
                for k in range(j+1, len(arr)):
                    if abs(arr[i] - arr[j]) <= a and abs(arr[i] - arr[k]) <= c and abs(arr[j]-arr[k]) <= b:
                        res += 1
        return res


from collections import defaultdict
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:
        ans = 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:
                        if abs(arr[j] - arr[k]) <= b:
                            if abs(arr[i] - arr[k]) <= c:
                                ans += 1
        return ans

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:
        ans = 0
        n = len(arr)
        for i in range(n):
            for j in range(i+1,n):
                for k in range(j+1, n):
                    if abs(arr[i] - arr[j]) <=a and abs(arr[j]-arr[k]) <=b and abs(arr[i]-arr[k]) <= c:
                        ans += 1
        return 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):
                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

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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        total = 0
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                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:
                        total += 1
                    
        return total

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        iterator = list(range(len(arr)))
        count = 0
        for i in iterator:
            for j in iterator:
                if i < j < len(arr):
                    if abs(arr[i]-arr[j]) <= a:
                        for k in iterator:
                            if i < j < k < len(arr):
                                if 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:
        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=count+1
                        
        
        return(count)    
    
   
    


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:
        output = 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)):
                    if (abs(arr[i] - arr[j]) <= a 
                        and abs(arr[j] - arr[k]) <= b
                        and abs(arr[i] - arr[k]) <= c):
                        output +=1
        return output


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:
        cnt =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:
                        cnt+=1
                        
                        
        return cnt

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)):
                    
                    if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[k]-arr[i]) <= c:
                        ans += 1
        
        return ans


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
              


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, len(arr)):
                for k in range(j, len(arr)):
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c and i != j and j != k:
                        goods += 1
        return goods

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


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:
        answer = []
        for i in range(len(arr)):
            for j in range((i+1), len(arr)):
                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):
                        answer.append((arr[i], arr[j], arr[k]))
                        
        return len(answer)

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        combs = combinations(arr, 3)
        fin = []
        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:
                fin.append(i)
                    
        return len(fin)
              


class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        goodtriplets=[]
        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:
                        goodtriplets.append((arr[i],arr[j],arr[k]))
        return len(goodtriplets)

                    


class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count=0
        list1=[]

        for i in range(0,len(arr)):
            for j in range(i+1,len(arr)):
                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:
                        list1.append([arr[i],arr[j],arr[k]])
                        count+=1
                        
                        
        return count               


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
                        
                        


class Solution:
    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)):
                    
                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-                              arr[k])<=c:
                        res += 1
        return res
    
    # [ 1  1  4 ]


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

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

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
#             if abs(arr[left] - arr[right]) <= c:
#                 for i in range(left+1, right):
#                     if abs(arr[left] - arr[i]) <= a and abs(arr[i] - arr[right]) <= b:
#                         cou += 1
#                 right -= 1
#                 continue
                
#         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)
        #arr = list(sorted(arr))
        count = 0
        for i in range(n):
            for j in range(i+1, n):
                for k in range(j+1, n):
                    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:
        
        res = 0
        
        for i in range(len(arr)):
            for j in range(i, len(arr)):
                if j <= i:
                    continue
                for k in range(j, len(arr)):
                    if k <= i or k <= j:
                        continue
                        
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        res += 1
                        
        return res
                    
                    


class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        combs = combinations(arr, 3)
        fin = []
        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:
                fin.append(i)
                    
        return len(fin)

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

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


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_triplets = 0
        
        for i in range(0, len(arr)):
            for j in range(i+1, len(arr)):
                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:
                        good_triplets += 1
        
        return good_triplets

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count=0
        for i in range(0,len(arr)):
            for j in range(i+1,len(arr)):
                for k in range(j+1,len(arr)):
                    if(abs(arr[i]-arr[j])<=a):
                        if(abs(arr[j]-arr[k])<=b):
                            if(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:
        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

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)):
                    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 = len(arr)
        count = 0
        for i in range(n):
            for j in range(i, n):
                for k in range(j, n):
                    if i != j != k and j != k:
                        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:
        
        goodcount = 0
        
        for i in range(len(arr)-2):
            
            for j in range (i+1 , len(arr)-1):
                
                k = j+1
                
                while k < len(arr):
                
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c :
                    
                        goodcount += 1
                    k += 1
        
        return goodcount

from itertools import combinations

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count = 0
    
        # Brute Force
        # Time: O(n^3)   
    
#         size = len(arr)
#         for i in range(size):
#             for j in range(size):
#                 if i >= j:
#                     continue
#                 for k in range(size):
#                     if i >= k or j >= k:
#                         continue
                    
#                     if (abs(arr[i] - arr [j]) <= a) and (abs(arr[j] - arr [k]) <= b) and (abs(arr[i] - arr [k]) <= c) :
#                         count += 1



        # Using itertools.combinations
    
        # triplets = combinations(arr, 3)
        # for triplet in triplets:
        #     if (abs(triplet[0] - triplet[1]) <= a) \\
        #             and (abs(triplet[1] - triplet[2]) <= b) \\
        #             and (abs(triplet[0] - triplet[2]) <= c):
        #         count += 1
        # return count

        
        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

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)):
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c: ans += 1
        return ans 

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:
        n = len(arr)
        ans = 0
        for i in range(n):
            for j in range(i, n):
                for k in range(j, n):
                    if (i == j) or (j == k) or (i == k):
                        continue
                    if (abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c):
                        ans += 1

        return ans

from itertools import combinations
class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        res = list(combinations(arr,3))
        count = 0
        for i in res :
            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

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:
#         #li = []
#         count = 0
#         for x in range(len(arr)):
#             #print(x)
#             for i in range(x+1,len(arr)):
#                 if (abs(arr[x]-arr[i]) <= a):
#                     for j in range(i+1,len(arr)):
#                         if (abs(arr[i]-arr[j])<=b):
#                             if (abs(arr[j]-arr[x])<=c):
#                                 count += 1
#         # for x in li:
#         #     if abs(x[0]-x[1]) <= a and abs(x[1]-x[2]) <=b and abs(x[0]-x[2]) <=c:
#         #         count += 1
#         return count

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        
        out=0
        
        for i in range(len(arr)):
                for j in range(i+1,len(arr)):        
                        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):
                                
                                    out+=1
        return out

from collections import defaultdict
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:
        result = 0
        for i in range(len(arr)):
            for j in range(i+1, len(arr)):
                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:
                        result += 1
        return result

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        i = 0
        j = i + 1
        k = j + 1
        return_arr = []
        for i in range(len(arr)):
            for j in range(i, len(arr)):
                for k in range(j, len(arr)):
                    if 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_arr.append([arr[i], arr[j], arr[k]])
                    
            
            
        
        return len(return_arr)
                
                


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)):
                    if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c :
                        ans = ans+1
                        
        return ans


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:
        res = list(combinations(arr,3))
        count = 0
        for i in res :
            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


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)):
                    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:
        cnt = 0
        for i in range(0, len(arr)):
            for j in range(i + 1, len(arr)):
                for k in range(j + 1, len(arr)):
                    if i == j or j == k or i == k or i == j == k: continue
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and 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:
        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)
        counter = 0
        for i in range(n):
            for j in range(i+1,n):
                for k in range(j+1,n):
                    if abs(arr[i] - arr[j]) <=a and abs(arr[j] - arr[k]) <=b and abs(arr[i] - arr[k]) <=c:
                        counter += 1
        return(counter)

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)):
                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
                        count = count+1
        
        return count

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

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


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:
        print(a,b,c)
        count = 0
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        temp = [x for x in combinations(arr, 3)]
        count = 0
        
        for i in temp:
            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


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):
                for k in range(j + 1, n):
                    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:
        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              
            


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:
        final = 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)):
                    if (0 <= i < j < k < len(arr)) and (abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c):
                        final += 1
        return final

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

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


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:
        ans = 0
        for i in range(len(arr)):
            for j in range(i+1, len(arr)):
                for k in range(j+1, len(arr)):
                    if 0 <= i < j < k < len(arr) and abs(arr[i] - arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i] - arr[k]) <= c: ans+=1
        return ans


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:
        count = 0
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                for k in range(j+1,len(arr)):
                    if (abs(arr[i]-arr[j]) <= a) & (abs(arr[j]-arr[k]) <= b) & (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:
        pair = dict()
        for p in (a, b, c):
            pair[p] = dict()
        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:
        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:
        cnt = 0
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                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:
                        cnt += 1
        
        return cnt


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:
        if len(arr) < 3:
            return 0
        
        ct = 0
        
        for i in range(0, len(arr)):
            for j in range(i + 1, len(arr)):
                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:
                        ct += 1

        return ct

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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count = 0
        for i in range(0, len(arr)):
            
            for j in range(i+1, len(arr)):
                
                                
                for k in range(j+1, len(arr)):
                    
                    if (abs(arr[i] - arr[j]) <=a) &  (abs(arr[j] - arr[k]) <=b) & (abs(arr[i] - arr[k]) <=c):
                        
                        count = 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:
        
        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)):
                    
                    a_bool = abs(arr[i] - arr[j]) <= a
                    b_bool = abs(arr[j] - arr[k]) <= b
                    c_bool = abs(arr[i] - arr[k]) <= c
                    
                    if a_bool and b_bool and c_bool:
                        count += 1
        
        return count

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: # condition for num3
                    # check num3 against all possible num2s so far
                    if a in arr_dict:
                        for pot_num2 in arr_dict[a]:
                            if abs(pot_num2 - num2) <= b:
                                # this is a good tuple
                                tup = (num1, pot_num2, num2)
                                tup_list.append(tup)

                if abs(num1 - num2) <= a: # condition for num2
                    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:
        
        res = 0
        
        for i in range(len(arr)):
            for j in range(len(arr)):
                if j <= i:
                    continue
                for k in range(len(arr)):
                    if k <= i or k <= j:
                        continue
                        
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        res += 1
                        
        return res
                    
                    


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
                    
                    
                    


class Solution:
    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)):
                    if (abs(arr[i] - arr[j]) <= a) & (abs(arr[j] - arr[k]) <= b) & (abs(arr[k] -     arr[i]) <= c):
                        res += 1
        return res


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)):
                    
                    if (abs(arr[i] - arr[j]) <=a) &  (abs(arr[j] - arr[k]) <=b) & (abs(arr[i] - arr[k]) <=c):
                        
                        count = count+1
                        
                        
                        
        return count
            
            
            
            
        


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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count = 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):
                    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:
        
        good_triplets = 0
        arr_len = len(arr)
        for i in range(arr_len):
            # print(\"i: \" + str(i))
            for j in range(i + 1, arr_len):
                # print(\"\\tj: \" + str(j))
                for k in range( j + 1, arr_len):
                    # print(\"\\t\\tk: \" + str(k))
                    if((0 <= i and i < j and j < k and k < len(arr)) and (abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and (abs(arr[i] - arr[k]) <= c)):
                        triplet = [i, j, k]
                        # print(triplet)
                        good_triplets += 1
        return good_triplets

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:
        mylist = []
        for i in range(len(arr)):
            for j in range(i, len(arr)):
                for k in range(j, len(arr)):
                    if 0 <= i < j < k < len(arr) and abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        mylist.append((arr[i],arr[j], arr[k]))
        return len(mylist)

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:
        ans=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)):
                    x=arr[i]-arr[j]
                    y=arr[j]-arr[k]
                    z=arr[i]-arr[k]
                    if abs(x)<=a and abs(y)<=b and abs(z)<=c:
                        ans+=1
        return ans

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)):

                    # print(f'{arr[i]}, {arr[j]}, {arr[k]}')

                    f = abs(arr[i] - arr[j]) <= a
                    # print(f'<{abs(arr[i] - arr[j])}, {a}, {f}>')

                    s = abs(arr[j] - arr[k]) <= b
                    # print(f'<{abs(arr[j] - arr[k])}, {b}, {s}>')

                    t = abs(arr[i] - arr[k]) <= c
                    # print(f'<{abs(arr[i] - arr[k])}, {c}, {t}>')

                    if f and s and t:
                        # print(f'{arr[i]}, {arr[i+1]}, {arr[i+2]}')
                        count += 1

                    # print()

        return count

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
                    #print(\"i:\",i,\" j: \",j,\" k: \",k, \" good_count: \",good_count,\" ijk:\",i,j,k)
                # move k if k != arr.len - 1
                if k != len(arr) - 1:
                    k += 1
                # move j and reset k = j +1 if k == arr.len - 1
                elif k == len(arr) - 1 and j+1 != k:
                    j += 1
                    k = j + 1
                # move i and reset j = i+1, k = + j+1 if j == arr.len - 2
                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:
        
        ans=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:
                        ans+=1
        return ans


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)):

                    # print(f'{arr[i]}, {arr[j]}, {arr[k]}')

                    f = abs(arr[i] - arr[j]) <= a
                    # print(f'<{abs(arr[i] - arr[j])}, {a}, {f}>')

                    s = abs(arr[j] - arr[k]) <= b
                    # print(f'<{abs(arr[j] - arr[k])}, {b}, {s}>')

                    t = abs(arr[i] - arr[k]) <= c
                    # print(f'<{abs(arr[i] - arr[k])}, {c}, {t}>')

                    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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        ans=[]
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                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 :
                        x=(arr[i] , arr[j] , arr[k])
                        ans.append(x)
        
        
        if len(arr)==0:
                return 0
        return len(ans)   


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:
            #print(idx_tuple)
            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)-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:
                        #print(i,j,k)
                        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) - 2):
            for j in range(i+1, len(arr) - 1):
                for k in range(j+1, len(arr)):

                    # print(f'{arr[i]}, {arr[j]}, {arr[k]}')

                    f = abs(arr[i] - arr[j]) <= a
                    # print(f'<{abs(arr[i] - arr[j])}, {a}, {f}>')

                    s = abs(arr[j] - arr[k]) <= b
                    # print(f'<{abs(arr[j] - arr[k])}, {b}, {s}>')

                    t = abs(arr[i] - arr[k]) <= c
                    # print(f'<{abs(arr[i] - arr[k])}, {c}, {t}>')

                    count += f and s and t

        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(len(arr)):
                if i<j:
                    for k in range(len(arr)):
                        if j<k:
                            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 diff(self, a, b, limit):
        return ( abs(a-b) <= limit ) 
    
    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)):
                    
                    if self.diff(arr[i], arr[j], a ) and self.diff(arr[j], arr[k], b) and self.diff(arr[i], arr[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 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:
        total_triplets = 0
        
        for i in range(0, len(arr)):
            for j in range(i + 1, len(arr)):
                for k in range(j + 1, len(arr)):
                    if (0 <= i < j and i <= j < k and j < k < len(arr) 
                        and abs(arr[i] - arr[j]) <= a 
                        and abs(arr[j] - arr[k]) <= b
                        and abs(arr[i] - arr[k]) <= c):
                        total_triplets += 1
        return total_triplets


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_count,jk_count,ik_count=0,0,0
            ij_pairs,jk_pairs=[],[]
            for n in first:
                if abs(arr[i]-n)<=a:
                    ij_count+=1
                    ij_pairs.append((n,arr[i]))
            for n in last:
                if abs(arr[i]-n)<=b:
                    jk_count+=1
                    jk_pairs.append((arr[i],n))
            print(ij_pairs,jk_pairs)
            for ij in ij_pairs:
                for jk in jk_pairs:
                    if abs(ij[0]-jk[1])<=c:
                        goods+=1
        return goods

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


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:
        count = 0
        for i in range(len(arr)-2):
            for j in range(i+1,len(arr)-1):
                for k in range(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 and i<j<k:
                        count += 1
        return count


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:
        n = len(arr)
        
        ans = 0
        for i in range(n):
            for j in range(i+1, n):
                for k in range(j+1, n):
                    x, y, z = arr[i], arr[j], arr[k]
                    ans += int(abs(x - y) <= a and abs(y - z) <= b and abs(x - z) <= c)
        return ans

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        # triplets_num = 0
        # for i in range(len(arr)-2):
        #     for j in range(i+1, len(arr)-1):
        #         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:
        #                     triplets_num += 1
        # return triplets_num
        
        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:
        count = 0
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                for k in range(j+1,len(arr)):
                    if self.isGood(arr[i], arr[j], a) and self.isGood(arr[j], arr[k],b) and self.isGood(arr[i], arr[k], c):
                        count+=1
        return count
        
    def isGood(self, x, y, a):
        return abs(x - y) <= a


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:
        count=0
        for i in range(0,len(arr)):
            for j in range(i+1,len(arr)):
                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=count+1
        return count

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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        # |arr[i] - arr[j]| <= a
        # |arr[j] - arr[k]| <= b
        # |arr[i] - arr[k]| <= c
        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[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        triplets+=1
                        # print((arr[i],arr[j],arr[k]))
        return triplets

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:
        count=0
        for i in range(len(arr)):
            for j in range(i, len(arr)):
                for k in range(j, len(arr)):
                    num1 =arr[i] - arr[j]
                    num2 = arr[j] - arr[k]
                    num3 = arr[i] - arr[k]
                    if num1<0:
                        num1 = -num1
                    if num2<0:
                        num2 = -num2
                    if num3<0:
                        num3 = -num3
                    if num1<=a and num2<=b and num3<=c and 0 <= i < j < k < len(arr):
                        count+=1
        return count

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:
            # for k in range(k, i+2, -1):
                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:
        triplets = 0
        
        for i in range(len(arr)):
            for j in range (i+1, len(arr)):
                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:
                        triplets +=1
                
        return triplets

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)):
                    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:
        i = 0
        res = []
        while i <= len(arr)-3:
            j = i+1
            while j <= len(arr)-2:
                k = j+1
                while k <= len(arr)-1:
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        ans = (arr[i],arr[j],arr[k])
                        res.append(ans)
                    k += 1
                j += 1
            i += 1
        return len(res)
                        
                        


from itertools import combinations
class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        comb = combinations(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:
        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:
        
        # first solution
        N = len(arr)
        count = 0
        for i in range(N-2):
            for j in range(i+1,N-1):
                for k in range(j+1,N):
                    '''
                    if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c: count+=1
                    '''                    
                    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)):
                        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) - 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)-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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        comb = combinations(arr, 3)
        fin = []
        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:
                fin.append(i)
                    
        return len(fin)

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:
        numGoodTrips = 0
        for i in range(len(arr)):
            for j in range(i+1, len(arr)):
                for k in range(j+1, len(arr)):
                    if self.isGoodTriplet(arr, i, j, k, a, b, c):
                        numGoodTrips += 1
        return numGoodTrips

                    
    def isGoodTriplet(self, arr: List[int], i: int, j: int, k: int, a: int, b: int, c: int) -> bool:
        return i < j and j < k and k < len(arr) and abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <=b and abs(arr[i] - arr[k]) <= c

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:
        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:
        
        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:
        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:
#         triplets = []
#         arr_len = len(arr) - 1
        
#         for i in range(arr_len):
#             for j in range(i+1, arr_len):
#                 for k in range(j+1, arr_len):
#                     if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:  
#                         triplets.append((arr[i], arr[j], arr[k]))
#                         # triplets += 1
        
#         return len(triplets)

        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]
        # print(C)
        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) # panjang dari array
        goodTriplet = 0 # jumlah goodTriplet
        
        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:
        def absolute(x):
            if(x>=0):
                return x
            return (-x)
        count=0
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                for k in range(j+1,len(arr)):
                    if(absolute(arr[i]-arr[j])<=a and absolute(arr[j]-arr[k])<=b and absolute(arr[i]-arr[k])<=c):
                        count+=1
        return count

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:
        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
        
        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 getdiff(self, x: int, y: int, m: int) -> int:
        return(abs(x-y) <= m)
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count = 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):
                    x = arr[i]
                    y = arr[j]
                    z = arr[k]
                    # print(x,y,z)
                    if (self.getdiff(x,y,a) and self.getdiff(y,z,b) and self.getdiff(x,z,c)):
                        count += 1
        return(count)

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        size = len(arr)
        res = 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)):
                        res += 1   
        return res

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(1,len(arr)-1):
                for k in range(2,len(arr)):
                    if i<j and j<k:
                        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:
        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]
        # print(C)
        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:

    
        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

from itertools import combinations

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[1]-tup[2]) <= b and abs(tup[2]-tup[0]) <= c:
            return tup


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:
        i = 0
        res = 0
        while i <= len(arr)-3:
            j = i+1
            while j <= len(arr)-2:
                k = j+1
                while k <= len(arr)-1:
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and 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:
        
        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:
        good_count = 0
        for i in range(len(arr)):
            for j in range(len(arr)):
                for k in range(len(arr)):
                    if i < j:
                        if j < k:
                            if abs(arr[i] - arr[j]) <=a:
                                if abs(arr[j] - arr[k]) <= b:
                                    if abs(arr[i] - arr[k]) <= c:
                                        good_count += 1
        return good_count    


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


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):
                for k in range(e):
                    if k>j and j>i:
                        if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
                            d+=1
        return d

# class Solution:
#     def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
#         result = 0
        
#         for i, num in enumerate(arr):
#             for j, num in enumerate(arr):
#                 for k, num in enumerate(arr):
#                     if i < j < k and abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
#                         result += 1
#         return result


from itertools import combinations
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]
        # print(C)
        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:
        count = 0
        for i in range(len(arr)):
            for j in range(len(arr)):
                for k in range(len(arr)):
                    if i<j<k and 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 = 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:
        result = []
        for i in range(0,len(arr)-2):
            for j in range(i+1,len(arr)-1):
                for k in range(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) and i < j and j< k:
                        result.append([arr[i], arr[j],arr[k]])
        return len(result) 

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, arr: List[int], a: int, b: int, c: int) -> int:
        res = 0
        l = len(arr)
        for i in range(l):
            for j in range(l):
                for k in range(l):
                    if i < j < k:
                        if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                            res += 1
        return res

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


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(len(arr)):
                for k in range(len(arr)):
                    if i<j and j<k:
                        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:
        triplets = []
        for i in range(len(arr)):
            for j in range(i+1, len(arr)):
                for k in range(j+1, len(arr)):
                    triplets.append([arr[i], arr[j], arr[k]])
        count = 0                                                                  
        for i in triplets:
            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                                                                  

class Solution:
    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(len(arr)):
                for k in range(len(arr)):
                    if i < j < k:
                        if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                            res += 1
        return res

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


class Solution:
    def countGoodTriplets(self, array: List[int], a: int, b: int, c: int) -> int:
        # get all valid triplets
        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]))

        # determine which ones are good
        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:
        n=len(arr)
        final=[]
        for i in range(0,n):
            for j in range(i+1,n):
                for k in range(j+1,n):
                    x=abs(arr[i]-arr[j])
                    y=abs(arr[j]-arr[k])
                    z=abs(arr[i]-arr[k])
                    if (x<=a and y<=b and z<=c):
                        final.append(arr[i:k+1])
        return len(final)


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


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



class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        
        ans = []
        
        for i in range(len(arr)):
            for j in range(i + 1, len(arr)):
                for k in range(j + 1, len(arr)):
                    ij = abs(arr[i] - arr[j])
                    jk = abs(arr[j] - arr[k])
                    ik = abs(arr[i] - arr[k])
                    if ij <= a and jk <= b and ik <= 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:
        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] # r = j, q = i
                if v <= b: # q = j, r = k, ij[q] = i
                    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

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):]):
                if (abs(x-y) <= a):
                    for z in arr[(j+i+2):]:
                        count += ((abs(y-z) <= b) and (abs(x-z) <= c))
        return count 

from collections import defaultdict as d
class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        da,db,dc=d(set),d(set),d(set)
        for i in range(len(arr)):
            for j in range(i+1,len(arr)):
                k=abs(arr[i]-arr[j])
                if k<=a:
                    da[i].add(j)
                if k<=b:
                    db[i].add(j)
                if k<=c:
                    dc[i].add(j)
        res=0
        for i in da:
            for j in da[i]:
                res+=len(db[j]&dc[i])
        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,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]
                if abs(n-n2)<=a:
                    for k in range(j+1,ln):
                        n3 = arr[k]
                        if abs(n2-n3)<=b:
                            if abs(n-n3)<=c:
                                res+=1

        return res

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

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

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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        
        # i < j < k
        # |arr[i] - arr[j]| <= a
        # |arr[j] - arr[k]| <= b
        # |arr[i] - arr[k]| <= c
        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:
        solution = 0
        for i in range(len(arr)-2):
            for j in range(i+1, len(arr)-1):
                if abs(arr[i] - arr[j]) > a:
                    continue
                for k in range(j+1, len(arr)):
                    if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        solution += 1
        return solution

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:
        x= list(range(len(arr)))
        score = 0
        for i in x:
            for j in x[i+1:]:
                if abs(arr[i] -arr[j])<= a:
                    for k in x[j+1:]:
                        if (abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c):
                            score+=1
        return score


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

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        total_triplets = 0
        
        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):
                            total_triplets += 1
        return total_triplets


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)):
                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):
                            ans += 1
        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:
        n = len(arr)
        result = 0
        for i in range (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 and abs(arr[i] - arr[k]) <= c:
                        result += 1
        return result


class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        
        out=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[j]-arr[k])<=b):
                                if(abs(arr[i]-arr[k])<=c):
                                    out+=1
                                
                                
                                
        return out

# # Brute Force Solution
# 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
                
#                 # break case
#                 if a_bool:
#                     for k in range(j+1,len(arr)):

#                         b_bool = abs(arr[j] - arr[k]) <= b
                        
#                         if b_bool:
                            
#                             c_bool = abs(arr[i] - arr[k]) <= c
                            
#                             if c_bool:
#                                 count += 1
        
#         return count


class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count    = 0
        size_arr = len(arr)

        for i in range(size_arr - 2):
            for j in range(i + 1, size_arr - 1):
                if abs(arr[i] - arr[j]) > a:
                    continue

                for k in range(j + 1, size_arr):
                    if abs(arr[j] - arr[k]) > b:
                        continue

                    if abs(arr[i] - arr[k]) > c:
                        continue

                    count += 1

        return count

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)

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        result = 0
        
        for i in range(len(arr)-2):
            for j in range(i+1, len(arr)-1):
                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:
                            result += 1
                            
        return result
                            


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
                    


# Brute Force Solution
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
                
                # break case
                if a_bool:
                    for k in range(j+1,len(arr)):

                        b_bool = abs(arr[j] - arr[k]) <= b
                        
                        if b_bool:
                            
                            c_bool = abs(arr[i] - arr[k]) <= c
                            
                            if c_bool:
                                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)-2):
            for j in range(i+1,len(arr)-1):
                if abs(arr[j]-arr[i])<=a:
                    for k in range(j+1,len(arr)):
                        if abs(arr[k]-arr[j])<=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:
        count = 0
        for i in range(0, len(arr)-2):
            
            for j in range(i+1, len(arr)-1):
                if (abs(arr[i] - arr[j]) <=a):
                    for k in range(j+1, len(arr)):
                        if (abs(arr[j] - arr[k]) <=b) & (abs(arr[i] - arr[k]) <=c):
                            count = count+1
                        
                        
                        
        return count
            
            
            
            
        


class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        count = 0
        arrsize = len(arr)
        for i in range(0, arrsize - 2):
            for j in range(i + 1, arrsize - 1):
                if abs(arr[i] - arr[j]) <= a:
                    for k in range(j + 1, arrsize):
                        if 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:
        size = len(arr)
        result = 0
        for i in range(0, 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
        return result

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 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 = 0
        for k in range(len(arr)):
            for j in range(k):
                if abs(arr[j] - arr[k]) <= b:
                    for i in range(j):
                        if abs(arr[i] - arr[k]) <= c and abs(arr[j] - arr[i]) <= a:
                            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)):
                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:
                            count+=1
        return count

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  - 2):
            for j in range(i + 1, n - 1):
                k = n - 1
                if abs(arr[i] - arr[j]) <= a:
                    while j < k:
                        if (abs(arr[j] - arr[k]) <= b and
                            abs(arr[i] - arr[k]) <= c):
                            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):
            j = i+1
            while j < len(arr):
                if abs(arr[j] - arr[i]) <= a:
                    k = j + 1
                    while k < len(arr):
                        if abs(arr[k] - arr[j]) <= b and 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:
        ans=0
        LEN=len(arr)
        for i in range(LEN-2):
            for j in range(i+1,LEN-1):
                if abs(arr[i]-arr[j])>a:
                    continue
                for k in range(j+1,LEN):
                    if (abs(arr[j]-arr[k])>b) | (abs(arr[i]-arr[k])>c):
                        continue
                    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)):
                condition1 = abs(arr[i] - arr[j]) <= a
                if not condition1:
                    continue
                for k in range(j + 1, len(arr)):
                    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:
        goods = 0
        for i in range(len(arr)):
            for j in range(i + 1, len(arr)):
                condition1 = abs(arr[i] - arr[j]) <= a
                if not condition1:
                    continue
                for k in range(j + 1, len(arr)):
                    condition2 = abs(arr[j] - arr[k]) <= b
                    condition3 = abs(arr[i] - arr[k]) <= c
                    if condition2 and condition3:
                        goods += 1
                        
        return goods

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:
        count = 0

        for i in range(len(arr) - 2):
            for j in range(i+1, len(arr) - 1):
                if not abs(arr[i] - arr[j]) <= a:
                    continue

                for k in range(j+1, len(arr)):

                    # print(f'{arr[i]}, {arr[j]}, {arr[k]}')

                    # f = abs(arr[i] - arr[j]) <= a
                    # print(f'<{abs(arr[i] - arr[j])}, {a}, {f}>')

                    s = abs(arr[j] - arr[k]) <= b
                    # print(f'<{abs(arr[j] - arr[k])}, {b}, {s}>')

                    t = abs(arr[i] - arr[k]) <= c
                    # print(f'<{abs(arr[i] - arr[k])}, {c}, {t}>')

                    count += s and t

        return count

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        #li = list(filter(lambda x: 0<=x<len(arr), arr))
        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:

        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):
                            #t =  [arr[i],arr[j],arr[k]]
                            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:
        ans=[]
        for i in range(len(arr)):
            # taking the remaining elements of arr into A, excluding upto i 
            for j in range(i+1,len(arr)):
                if abs(arr[i]-arr[j])<=a:
                    # taking the remaining elements of A into B, excluding upto j
                    for k in range(j+1,len(arr)):
                        if abs(arr[j]-arr[k])<=b:
                            if 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:
        answer = 0
        for i in range(len(arr)-2):
            for j in range(i+1,len(arr)-1):
                if abs(arr[i]-arr[j])<=a:
                    for k in range(j+1,len(arr)):
                        if abs(arr[j]-arr[k])<=b:
                            if abs(arr[i]-arr[k])<=c:
                                answer+=1
        return answer


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:
        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[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:
        if len(arr) < 3:
            return 0
        good_trip = []
        for i in range(0, len(arr)-2):
            for j in range(i+1, len(arr)-1):
                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:
                            good_trip.append([arr[i], arr[j], arr[k]])
        
        return len(good_trip)

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:
        arr_len = len(arr)
        count = 0
        for i in range(arr_len):
            for j in range(i + 1, arr_len):
                if abs(arr[i] - arr[j]) > a:
                    continue
                
                for k in range(j + 1, arr_len):
                    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:
        
        result = 0
        length = len(arr)
        
        for i in range(length - 2):
            for j in range(i + 1, length - 1):
                if abs(arr[i] - arr[j]) <= a :
                    for k in range(j + 1, length):
                        if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                            result += 1
        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) - 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:
                        break
                    if 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:
        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

# slight optimization: first check
class Solution:
    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)):
                if abs(arr[i] - arr[j]) <= a:
                    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:
                            res += 1
        return res

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        # Base case
        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

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[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                            count += 1
        return count

# Brute Force Solution
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):
                
                # break case
                if abs(arr[i] - arr[j]) <= a:
                    for k in range(j+1,len(arr)):

                        if all((abs(arr[j] - arr[k]) <= b,abs(arr[i] - arr[k]) <= c)):
                            count += 1
        
        return count