Similar Problems

Similar Problems not available

Number Of Unequal Triplets In Array - Leetcode Solution

Companies:

LeetCode:  Number Of Unequal Triplets In Array Leetcode Solution

Difficulty: Easy

Topics: hash-table sorting array  

Problem Statement:

Given an integer array nums, return the number of unique triplets (i, j, k) such that i < j < k and nums[i] != nums[j] != nums[k] != nums[i].

Example 1:

Input: nums = [1,2,3,4,5] Output: 10 Explanation: The unique triplets are [1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5], and [3,4,5].

Example 2:

Input: nums = [1,1,2,2,3] Output: 4 Explanation: The unique triplets are [1,2,3], [1,2,4], [1,3,4], and [2,3,4].

Solution:

The approach to solve this problem is to use a hash map to store the frequency of each element in the array. Then, we can loop through the array and find the number of unique triplets such that i < j < k and nums[i] != nums[j] != nums[k] != nums[i].

To find the unique triplets, we can loop through the array twice, except for the last two elements. For each loop, we can check whether the current element is already visited or not, if visited then we can leave that element otherwise we can proceed further. After that, we can loop through the remaining elements and find the element such that the frequency of the element equals to 1 and is not equal to the current element. Finally, we can increase the count of unique triplets by 1 for each such combination.

Here is the algorithm to solve the problem:

Algorithm:

  1. Initialize a hash map to store the frequency of each element in the array.
  2. Initialize a variable count to 0.
  3. Loop through the array twice, except for the last two elements. a. For each loop, check whether the current element is already visited or not by comparing it with the next element. If visited, then continue to the next step. b. Loop through the remaining elements and find the element such that the frequency of the element equals to 1 and is not equal to the current element. c. For each such combination, increase the count of unique triplets by 1.
  4. Return the count of unique triplets.

Here is the implementation of the solution in Python:

def count_triplets(nums): freq = {} n = len(nums) count = 0 for i in range(n): if nums[i] in freq: freq[nums[i]] += 1 else: freq[nums[i]] = 1

for i in range(n-1):
    if i > 0 and nums[i] == nums[i-1]:
        continue
    for j in range(i+1, n-1):
        if j > i+1 and nums[j] == nums[j-1]:
            continue
        for k in range(j+1, n):
            if k > j+1 and nums[k] == nums[k-1]:
                continue
            if freq[nums[i]] == 1 and freq[nums[j]] == 1 and freq[nums[k]] == 1:
                count += 1

return count

nums = [1, 2, 3, 4, 5] print(count_triplets(nums)) # Output: 10

nums = [1, 1, 2, 2, 3] print(count_triplets(nums)) # Output: 4

Time Complexity:

The time complexity of the solution is O(n^3), where n is the length of the array. The reason for this time complexity is that we have used three nested loops to iterate through the array.

Space Complexity:

The space complexity of the solution is O(n), where n is the length of the array. This is because we are using a hash map to store the frequency of each element in the array.

Number Of Unequal Triplets In Array Solution Code

1