|
| 1 | +<?php |
| 2 | +class Solution |
| 3 | +{ |
| 4 | + /** |
| 5 | + * @param Integer[] $arr |
| 6 | + * @param Integer $a |
| 7 | + * @param Integer $b |
| 8 | + * @param Integer $c |
| 9 | + * @return Integer |
| 10 | + */ |
| 11 | + public function countGoodTriplets($arr, $a, $b, $c) |
| 12 | + { |
| 13 | + $count = 0; |
| 14 | + $n = count($arr); |
| 15 | + for ($i = 0; $i < $n - 2; $i++) { |
| 16 | + for ($j = $i + 1; $j < $n - 1; $j++) { |
| 17 | + |
| 18 | + if (abs($arr[$i] - $arr[$j]) > $a) { |
| 19 | + continue; |
| 20 | + } |
| 21 | + |
| 22 | + for ($k = $j + 1; $k < $n; $k++) { |
| 23 | + |
| 24 | + if ( |
| 25 | + abs($arr[$j] - $arr[$k]) <= $b && abs(($arr[$i] - $arr[$k])) <= $c) { |
| 26 | + $count++; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + return $count; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +$solution = new Solution(); |
| 36 | + |
| 37 | +$arr = [3, 0, 1, 1, 9, 7]; |
| 38 | + |
| 39 | +$a = 7; |
| 40 | + |
| 41 | +$b = 2; |
| 42 | +$c = 3; |
| 43 | +$result = $solution->countGoodTriplets($arr, $a, $b, $c); |
| 44 | + |
| 45 | +echo "The number of good triplets is: " . $result . "\n"; // Output: 4 |
0 commit comments