From fa5869e519b1b53f2926258e5b71078a420c1ba4 Mon Sep 17 00:00:00 2001 From: manishh12 Date: Wed, 5 Jun 2024 12:23:24 +0530 Subject: [PATCH 1/2] Added Count Good Triplets --- .../1500-1599/1534-Count-good-triplets.md | 404 ++++++++++++++++++ 1 file changed, 404 insertions(+) create mode 100644 dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md diff --git a/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md b/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md new file mode 100644 index 000000000..ed343dfa8 --- /dev/null +++ b/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md @@ -0,0 +1,404 @@ +--- +id: count-good-triplets +title: Count Good Triplets Solution +sidebar_label: 1534-Count-Good-Triplets +tags: + - Count Good Triplets + - Array + - Brute Force + - Optimization + - LeetCode + - Python + - JavaScript +description: "This is a solution to the Count Good Triplets problem on LeetCode." +--- + +In this page, we will solve the Count Good Triplets problem using multiple approaches: brute-force and optimization. We will provide the implementation of the solution in Python, JavaScript, TypeScript, Java, and C++. + +## Problem Description + +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. + +### Examples + +**Example 1:** + +```plaintext +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:** + +```plaintext +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 for Count Good Triplets Problem + +### Intuition and Approach + +The problem can be solved using different approaches. We will start with a brute-force approach and then look at an optimized approach using early exits to reduce unnecessary comparisons. + + + + +### Approach 1: Brute Force + +We iterate through all possible triplets `(i, j, k)` where `0 <= i < j < k < arr.length` and check if they satisfy the conditions `|arr[i] - arr[j]| <= a`, `|arr[j] - arr[k]| <= b`, and `|arr[i] - arr[k]| <= c`. If they do, we count it as a good triplet. + +#### Implementation +```jsx live +function countGoodTriplets() { + const arr = [3, 0, 1, 1, 9, 7]; + const a = 7; + const b = 2; + const c = 3; + + const countGoodTriplets = function(arr, a, b, c) { + let count = 0; + for (let i = 0; i < arr.length - 2; i++) { + for (let j = i + 1; j < arr.length - 1; j++) { + if (Math.abs(arr[i] - arr[j]) > a) continue; + for (let k = j + 1; k < arr.length; k++) { + if (Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + }; + + const result = countGoodTriplets(arr, a, b, c); + return ( +
+

+ Input: arr = {JSON.stringify(arr)}, a = {a}, b = {b}, c = {c} +

+

+ Output: {result} +

+
+ ); +} +``` + +#### Codes in Different Languages + + + + + ```javascript + function countGoodTriplets(arr, a, b, c) { + let count = 0; + for (let i = 0; i < arr.length - 2; i++) { + for (let j = i + 1; j < arr.length - 1; j++) { + for (let 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++; + } + } + } + } + return count; + } + ``` + + + + + ```typescript + function countGoodTriplets(arr: number[], a: number, b: number, c: number): number { + let count = 0; + for (let i = 0; i < arr.length - 2; i++) { + for (let j = i + 1; j < arr.length - 1; j++) { + for (let 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++; + } + } + } + } + return count; + } + ``` + + + + + ```python + 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 + ``` + + + + + ```java + class Solution { + public int countGoodTriplets(int[] arr, int a, int b, int c) { + int count = 0; + for (int i = 0; i < arr.length - 2; i++) { + for (int j = i + 1; j < arr.length - 1; j++) { + for (int 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++; + } + } + } + } + return count; + } + } + ``` + + + + + ```cpp + class Solution { + public: + int countGoodTriplets(vector& arr, int a, int b, int c) { + int count = 0; + for (int i = 0; i < arr.size() - 2; i++) { + for (int j = i + 1; j < arr.size() - 1; j++) { + for (int k = j + 1; k < arr.size(); k++) { + if (abs(arr[i] - arr[j]) <= a && + abs(arr[j] - arr[k]) <= b && + abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + } + }; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: $$O(n^3)$$ +- Space Complexity: $$O(1)$$ +- Where `n` is the length of the array `arr`. +- The time complexity is determined by the three nested loops iterating through the array. +- The space complexity is constant as no additional space is required beyond a few variables. + +
+ + +### Approach 2: Optimized Brute Force + +To optimize the brute force solution, we can use early exits to avoid unnecessary comparisons. Specifically, if a pair does not satisfy one of the conditions, we can skip further comparisons for that triplet. + +#### Implementation + +```jsx live +function countGoodTriplets() { + const arr = [3, 0, 1, 1, 9, 7]; + const a = 7; + const b = 2; + const c = 3; + const countGoodTripletsOptimized = function(arr, a, b, c) { + let count = 0; + for (let i = 0; i < arr.length - 2; i++) { + for (let j = i + 1; j < arr.length - 1; j++) { + if (Math.abs(arr[i] - arr[j]) > a) continue; + for (let k = j + 1; k < arr.length; k++) { + if (Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + }; + + const resultOptimized = countGoodTripletsOptimized(arr, a, b, c); + + return ( +
+

+ Input: arr = {JSON.stringify(arr)}, a = {a}, b = {b}, c = {c} +

+

+ Output: {resultOptimized} +

+
+ ); +} +``` + +#### Codes in Different Languages + + + + + ```javascript + function countGoodTriplets(arr, a, b, c) { + let count = 0; + for (let i = 0; i < arr.length - 2; i++) { + for (let j = i + 1; j < arr.length - 1; j++) { + if (Math.abs(arr[i] - arr[j]) > a) continue; + for (let k = j + 1; k < arr.length; k++) { + if (Math.abs(arr[j] - arr[k]) <= b && + Math.abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + } + ``` + + + + + ```typescript + function countGoodTriplets(arr: number[], a: number, b: number, c: number): number { + let count = 0; + for (let i = 0; i < arr.length - 2; i++) { + for (let j = i + 1; j < arr.length - 1; j++) { + if (Math.abs(arr[i] - arr[j]) > a) continue; + for (let k = j + 1; k < arr.length; k++) { + if (Math.abs(arr[j] - arr[k]) <= b && + Math.abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + } + ``` + + + + + ```python + 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[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: + count += 1 + return count + ``` + + + + + ```java + class Solution { + public int countGoodTriplets(int[] arr, int a, int b, int c) { + int count = 0; + for (int i = 0; i < arr.length - 2; i++) { + for (int j = i + 1; j < arr.length - 1; j++) { + if (Math.abs(arr[i] - arr[j]) > a) continue; + for (int k = j + 1; k < arr.length; k++) { + if (Math.abs(arr[j] - arr[k]) <= b && + Math.abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + } + } + ``` + + + + + ```cpp + class Solution { + public: + int countGoodTriplets(vector& arr, int a, int b, int c) { + int count = 0; + for (int i = 0; i < arr.size() - 2; i++) { + for (int j = i + 1; j < arr.size() - 1; j++) { + if (abs(arr[i] - arr[j]) > a) continue; + for (int k = j + 1; k < arr.size(); k++) { + if (abs(arr[j] - arr[k]) <= b && + abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + } + }; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: $$O(n^3)$$ +- Space Complexity: $$O(1)$$ +- Where `n` is the length of the array `arr`. +- The time complexity remains cubic but the constant factor is reduced due to early exits. + +
+
+ +:::tip Note + +By using these approaches, we can efficiently solve the Count Good Triplets problem for the given constraints. + +::: +## References + +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/count-good-triplets/) +- **Solution Link:** [Count Good Triplets Solution on LeetCode](https://leetcode.com/problems/count-good-triplets/discuss/762344/Brute-Force-O(n3)-with-JavaC%2B%2B) +- **Authors LeetCode Profile:** [Manish Kumar Gupta](https://leetcode.com/_manishh12/) + +--- + From 543b12df6c037badd0cd5f82264d81710ce11520 Mon Sep 17 00:00:00 2001 From: Manish kumar gupta <97523900+manishh12@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:33:49 +0530 Subject: [PATCH 2/2] Update 1534-Count-good-triplets.md --- .../1500-1599/1534-Count-good-triplets.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md b/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md index ed343dfa8..4d8109d85 100644 --- a/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md +++ b/dsa-solutions/lc-solutions/1500-1599/1534-Count-good-triplets.md @@ -19,14 +19,14 @@ In this page, we will solve the Count Good Triplets problem using multiple appro 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: +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` +- $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`. +Where $|x|$ denotes the absolute value of `x`. Return the number of good triplets. @@ -50,9 +50,9 @@ Explanation: No triplet satisfies all conditions. ### Constraints -- `3 <= arr.length <= 100` -- `0 <= arr[i] <= 1000` -- `0 <= a, b, c <= 1000` +- $3 <= arr.length <= 100$ +- $0 <= arr[i] <= 1000$ +- $0 <= a, b, c <= 1000$ --- @@ -400,5 +400,5 @@ By using these approaches, we can efficiently solve the Count Good Triplets prob - **Solution Link:** [Count Good Triplets Solution on LeetCode](https://leetcode.com/problems/count-good-triplets/discuss/762344/Brute-Force-O(n3)-with-JavaC%2B%2B) - **Authors LeetCode Profile:** [Manish Kumar Gupta](https://leetcode.com/_manishh12/) ---- +