|
| 1 | +<h2><a href="https://leetcode.com/problems/count-good-triplets">1656. Count Good Triplets</a></h2><h3>Easy</h3><hr><p>Given an array of integers <code>arr</code>, and three integers <code>a</code>, <code>b</code> and <code>c</code>. You need to find the number of good triplets.</p> |
| 2 | + |
| 3 | +<p>A triplet <code>(arr[i], arr[j], arr[k])</code> is <strong>good</strong> if the following conditions are true:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>0 <= i < j < k < arr.length</code></li> |
| 7 | + <li><code>|arr[i] - arr[j]| <= a</code></li> |
| 8 | + <li><code>|arr[j] - arr[k]| <= b</code></li> |
| 9 | + <li><code>|arr[i] - arr[k]| <= c</code></li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Where <code>|x|</code> denotes the absolute value of <code>x</code>.</p> |
| 13 | + |
| 14 | +<p>Return<em> the number of good triplets</em>.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<strong>Input:</strong> arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3 |
| 21 | +<strong>Output:</strong> 4 |
| 22 | +<strong>Explanation:</strong> There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)]. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> arr = [1,1,2,2,3], a = 0, b = 0, c = 1 |
| 29 | +<strong>Output:</strong> 0 |
| 30 | +<strong>Explanation: </strong>No triplet satisfies all conditions. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | +<p><strong>Constraints:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li><code>3 <= arr.length <= 100</code></li> |
| 38 | + <li><code>0 <= arr[i] <= 1000</code></li> |
| 39 | + <li><code>0 <= a, b, c <= 1000</code></li> |
| 40 | +</ul> |
0 commit comments