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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find all the 'good' combinations of three numbers from a larger set. A brute force approach simply checks every possible combination of three numbers to see if it meets the 'good' criteria.
Here's how the algorithm would work step-by-step:
def count_good_triplets_brute_force(arr, a_threshold, b_threshold, c_threshold):
good_triplet_count = 0
for first_index in range(len(arr)):
for second_index in range(first_index + 1, len(arr)):
# Iterate over all elements after the first one.
for third_index in range(second_index + 1, len(arr)):
# Iterate over all elements after the second one.
if abs(arr[first_index] - arr[second_index]) <= a_threshold:
if abs(arr[second_index] - arr[third_index]) <= b_threshold:
# Ensure the first two conditions are true.
if abs(arr[first_index] - arr[third_index]) <= c_threshold:
# Check the final condition.
good_triplet_count += 1
return good_triplet_count
The basic idea is to efficiently count valid combinations by focusing on the relationships between the numbers and pre-calculating information to avoid repeated checks. We'll build upon our knowledge as we progress through the list of numbers to reduce the overall work.
Here's how the algorithm would work step-by-step:
def count_good_triplets(arr, a, b, c):
number_of_good_triplets = 0
array_length = len(arr)
for first_index in range(array_length):
for second_index in range(first_index + 1, array_length):
# Only proceed if condition between first and second elements is met.
if abs(arr[first_index] - arr[second_index]) <= a:
for third_index in range(second_index + 1, array_length):
# Checking all three conditions to identify a good triplet
if abs(arr[first_index] - arr[third_index]) <= b and abs(arr[second_index] - arr[third_index]) <= c:
number_of_good_triplets += 1
# Return the total count of good triplets
return number_of_good_triplets
Case | How to Handle |
---|---|
Empty or null input array | Return 0, as there are no triplets to count. |
Array size less than 3 | Return 0, as a triplet requires at least three elements. |
Large array size causing potential performance issues (Time Limit Exceeded) | Optimize the solution using techniques like early exit or a sliding window if possible to reduce the number of comparisons. |
Integer overflow in calculations if array values are very large and a, b, c are large | Use a data type that can accommodate larger numbers, like long, to prevent overflow during absolute difference calculations. |
All elements in the array are identical, and the difference constraints are met. | Handle the combinatorial explosion correctly to avoid counting the same triplet multiple times, possibly using combinations formula. |
Extreme boundary values for a, b, and c (e.g., a, b, c are very small, or very large). | Ensure the absolute difference calculation does not result in unexpected behavior or errors due to boundary conditions. |
No good triplets exist in the array given the constraints. | The solution should correctly return 0 when no triplets satisfy the given conditions. |
Array contains negative numbers | The absolute difference function should handle negative numbers correctly ensuring correct counts. |