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.lengthnums[i],nums[j], andnums[k]are pairwise distinct.- In other words,
nums[i] != nums[j],nums[i] != nums[k], andnums[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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code