diff --git a/solution/1500-1599/1534.Count Good Triplets/README.md b/solution/1500-1599/1534.Count Good Triplets/README.md index 1b8c85b232947..32b81f8b8b21f 100644 --- a/solution/1500-1599/1534.Count Good Triplets/README.md +++ b/solution/1500-1599/1534.Count Good Triplets/README.md @@ -183,6 +183,52 @@ function countGoodTriplets(arr: number[], a: number, b: number, c: number): numb } ``` +#### Rust + +```rust +impl Solution { + pub fn count_good_triplets(arr: Vec, a: i32, b: i32, c: i32) -> i32 { + let n = arr.len(); + let mut ans = 0; + + for i in 0..n { + for j in i + 1..n { + for k in j + 1..n { + if (arr[i] - arr[j]).abs() <= a && (arr[j] - arr[k]).abs() <= b && (arr[i] - arr[k]).abs() <= c { + ans += 1; + } + } + } + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int CountGoodTriplets(int[] arr, int a, int b, int c) { + int n = arr.Length; + int ans = 0; + + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + for (int k = j + 1; k < n; ++k) { + if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) { + ++ans; + } + } + } + } + + return ans; + } +} +``` + diff --git a/solution/1500-1599/1534.Count Good Triplets/README_EN.md b/solution/1500-1599/1534.Count Good Triplets/README_EN.md index cdec96dfb8cd3..e1d64f2f7cc9b 100644 --- a/solution/1500-1599/1534.Count Good Triplets/README_EN.md +++ b/solution/1500-1599/1534.Count Good Triplets/README_EN.md @@ -183,6 +183,52 @@ function countGoodTriplets(arr: number[], a: number, b: number, c: number): numb } ``` +#### Rust + +```rust +impl Solution { + pub fn count_good_triplets(arr: Vec, a: i32, b: i32, c: i32) -> i32 { + let n = arr.len(); + let mut ans = 0; + + for i in 0..n { + for j in i + 1..n { + for k in j + 1..n { + if (arr[i] - arr[j]).abs() <= a && (arr[j] - arr[k]).abs() <= b && (arr[i] - arr[k]).abs() <= c { + ans += 1; + } + } + } + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int CountGoodTriplets(int[] arr, int a, int b, int c) { + int n = arr.Length; + int ans = 0; + + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + for (int k = j + 1; k < n; ++k) { + if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) { + ++ans; + } + } + } + } + + return ans; + } +} +``` + diff --git a/solution/1500-1599/1534.Count Good Triplets/Solution.cs b/solution/1500-1599/1534.Count Good Triplets/Solution.cs new file mode 100644 index 0000000000000..7f3c1d3ea34c9 --- /dev/null +++ b/solution/1500-1599/1534.Count Good Triplets/Solution.cs @@ -0,0 +1,18 @@ +public class Solution { + public int CountGoodTriplets(int[] arr, int a, int b, int c) { + int n = arr.Length; + int ans = 0; + + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + for (int k = j + 1; k < n; ++k) { + if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) { + ++ans; + } + } + } + } + + return ans; + } +} diff --git a/solution/1500-1599/1534.Count Good Triplets/Solution.rs b/solution/1500-1599/1534.Count Good Triplets/Solution.rs new file mode 100644 index 0000000000000..a1ed8b89cf826 --- /dev/null +++ b/solution/1500-1599/1534.Count Good Triplets/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn count_good_triplets(arr: Vec, a: i32, b: i32, c: i32) -> i32 { + let n = arr.len(); + let mut ans = 0; + + for i in 0..n { + for j in i + 1..n { + for k in j + 1..n { + if (arr[i] - arr[j]).abs() <= a + && (arr[j] - arr[k]).abs() <= b + && (arr[i] - arr[k]).abs() <= c + { + ans += 1; + } + } + } + } + + ans + } +}