Day 6 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1534.Count Good Triplets(Easy/JavaScript)
Tags: javascript, beginners, leetcode, algorithms
Published: 2022-02-16 16:01:35

**Intro:** I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. **Here is my approach:**
- Pick a leetcode problem randomly or Online Assessment from targeted companies.
- Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
- Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
- Code out the solution in LeetCode without looking at the solutions
- **Combat the forgetting curve:** Re-do the question for the next three days. And come back regularly to revisit the problem.

******************************************

## Problem#1534. Count Good Triplets
`Difficulty: Easy` `Language: JavaScript` 
<!--`Company: IBM Backend Developer OA -->

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`

******************************************

##Solution 1:
```
var countGoodTriplets = function(arr, a, b, c) {
    
    let output = []
    
/*create an empty array to store the triplets*/

    for (i = 0; i < arr.length; i++) {
        for (j = i + 1; j < arr.length; j++) {
            for (k = j + 1; k < arr.length; k++) {

/*loop (note 2) through three letters in the given 'arr'*/

                if (
                    Math.abs(arr[i] - arr[j]) <= a &&
                    Math.abs(arr[j] - arr[k]) <= b &&
                    Math.abs(arr[i] - arr[k]) <= c
                   ) 

/*use 'if statement' (note 4), 'Logical AND(&&)' (note 5) and
absolute value (note 3) to find the triplets meets the conditions*/

                 output.push((arr[i], arr[j], arr[k]))

/*Once triplets are found, push (note 6) it to the 'output'
array*/

            }
        }  
    }
    return output.length

/*return length (note 7) of the output array*/

};

```
**Solution Submission detail as of 2/16/2022** 
(Data below could vary since there are new submissions daily)
- Runtime: 143 ms
- Memory Usage: 49.7 MB
- Time complexity:Time complexity of the method is O(n3) which
is for sorting. Once the array of intervals is sorted, merging
takes linear time.
- Space complexity:O(n)

******************************************

##Solution 2 (improve run time (by half) and space by just changing 2 lines):
```
var countGoodTriplets = function(arr, a, b, c) {
    
    let count = 0

/*Since the problem is asking for the length and not the actual
array of the triplets. In this solution, we will use 'count'
instead of create an actual array to improve runtime and space. */
    
    for (i = 0; i < arr.length; i++) {
        for (j = i + 1; j < arr.length; j++) {
            for (k = j + 1; k < arr.length; k++) {
                if (
                    Math.abs(arr[i] - arr[j]) <= a &&
                    Math.abs(arr[j] - arr[k]) <= b &&
                    Math.abs(arr[i] - arr[k]) <= c
                   ) 
                    count ++

/*while previous steps are the same as solution one, we will
increase count (note 7) for every triplet found.*/

            }
        }
        
    }
    return count
};
```
**Solution Submission detail as of 2/16/2022** 
(Data below could vary since there are new submissions daily)
- Runtime: 76 ms
- Memory Usage: 42.1 MB
- Time complexity:Time complexity of the method is O(n3) which
is for sorting. Once the array of intervals is sorted, merging
takes linear time.
- Space complexity:O(1)

******************************************

**References:**
[LeetCode Problem Link](https://leetcode.com/problems/count-good-triplets/)
[Note 2: for loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)
[Note 3: Math.abs()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs)
[Note 4: if statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
[Note 5: Logical AND(&&)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND)
[Note 6: Array.push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)
[Note 6: Array.length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length)
[Note 7: Increment(++)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment)
[Blog Cover Image Credit](https://www.istockphoto.com/portfolio/monsitj?mediatype=photography)