Similar Problems

Similar Problems not available

Count Good Triplets - Leetcode Solution

Companies:

LeetCode:  Count Good Triplets Leetcode Solution

Difficulty: Easy

Topics: array  

The problem statement is:

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 called 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.

Solution:

The solution to this problem is to use brute force by checking all possible triplets in the given array and then checking whether they are good or not. The first approach that comes to mind is to generate all possible triplets using three nested loops and then check whether they satisfy the given conditions or not. The time complexity of this approach is O(n^3).

We can optimize the above approach by using two-pointer technique. We can fix the first element of the triplet (arr[i]), and then use two pointers (j, k) to find the second and third elements such that the conditions are satisfied. By doing this, we can reduce the time complexity to O(n^2).

Algorithm:

  1. Initialize a variable count to 0.
  2. Iterate over the array arr from index i = 0 to i = n-2.
  3. For each i, initialize two pointers j = i+1 and k = n-1.
  4. For each (i, j, k) triplet such that j < k, check whether the conditions are satisfied or not.
  5. If the conditions are satisfied, increment the count.
  6. If arr[j] - arr[i] <= a, increment j.
  7. If arr[k] - arr[j] <= b, decrement k.
  8. Otherwise, increment j.
  9. Return the count.

Here is the Python implementation of the above algorithm:

def countGoodTriplets(arr, a, b, c): count = 0 n = len(arr) for i in range(n-2): j = i+1 k = n-1 while j < k: if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c: count += 1 if arr[j] - arr[i] <= a: j += 1 elif arr[k] - arr[j] <= b: k -= 1 else: j += 1 return count

Time Complexity:

The time complexity of the above algorithm is O(n^2), where n is the length of the input array. This is because we are iterating over all possible pairs of indices (i, j) and using two-pointer technique to find the third index such that the conditions are satisfied.

Space Complexity:

The space complexity of the above algorithm is O(1), because we are not using any extra space to solve the problem.

Example:

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).

Conclusion:

The Count Good Triplets problem requires us to find all the triplets that satisfy a certain set of conditions. We can solve this problem by brute-force using three nested loops or using two-pointer technique to optimize the solution. In the given solution, we have used two-pointer technique to solve this problem efficiently. The time complexity of the solution is O(n^2), where n is the length of the input array, and the space complexity is O(1).

Count Good Triplets Solution Code

1