Number of Unequal Triplets in Array - Problem

You are given a 0-indexed array of positive integers nums. Find the number of triplets (i, j, k) that meet the following conditions:

  • 0 <= i < j < k < nums.length
  • nums[i], nums[j], and nums[k] are pairwise distinct.
  • In other words, nums[i] != nums[j], nums[i] != nums[k], and nums[j] != nums[k].

Return the number of triplets that meet the conditions.

Input & Output

Example 1 — Basic Case
$ Input: nums = [4,4,2,4,3]
Output: 3
💡 Note: Valid triplets are (0,2,4), (1,2,4), (2,3,4) with values (4,2,3). All have distinct values.
Example 2 — No Valid Triplets
$ Input: nums = [1,1,1,1,1]
Output: 0
💡 Note: All elements are the same, so no triplet can have pairwise distinct values.
Example 3 — All Unique
$ Input: nums = [1,2,3]
Output: 1
💡 Note: Only one triplet (0,1,2) with values (1,2,3), all distinct.

Constraints

  • 3 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Number of Unequal Triplets: Find distinct value combinations4424301234Valid triplets: (0,2,4), (1,2,4), (2,3,4)All have values (4,2,3) which are pairwise distinctOutput: 3
Understanding the Visualization
1
Input Array
Array with potentially duplicate values
2
Find Triplets
Locate all (i,j,k) where i<j<k and values are distinct
3
Count Result
Return total number of valid triplets
Key Takeaway
🎯 Key Insight: Count frequencies first, then use combinatorics instead of checking all O(n³) triplets
Asked in
Google 15 Amazon 10
8.5K Views
Medium Frequency
~15 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen