Skip to content

Latest commit

 

History

History
87 lines (72 loc) · 2.72 KB

File metadata and controls

87 lines (72 loc) · 2.72 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : April 14, 2025

Last updated : April 14, 2025


Related Topics : Array, Enumeration

Acceptance Rate : 85.54 %


Solutions

C

int countGoodTriplets(int* arr, int arrSize, int a, int b, int c){
    int cnt = 0;
    
    for (int i = 0; i < arrSize - 2; i++) {
        for (int j = i + 1; j < arrSize - 1; j++) {
            for (int k = j + 1; k < arrSize; k++) {
                if (
                    abs(arr[i] - arr[j]) <= a &&
                    abs(arr[j] - arr[k]) <= b &&
                    abs(arr[i] - arr[k]) <= c
                ) {
                    cnt++;
                }
            }
        }
    }

    return cnt;
}
int countGoodTriplets(int* arr, int arrSize, int a, int b, int c){
    int cnt = 0;
    for (int i = 0; i < arrSize - 2; i++) { for (int j = i + 1; j < arrSize - 1; j++) { for (int k = j + 1; k < arrSize; k++) {
        if (abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) { cnt++; }
    }}}
    return cnt;
}

Python

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