Easy
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.
nums[i] != nums[j]
, nums[i] != nums[k]
, and nums[j] != nums[k]
.Return the number of triplets that meet the conditions.
Example 1:
Input: nums = [4,4,2,4,3]
Output: 3
Explanation: The following triplets meet the conditions:
Example 2:
Input: nums = [1,1,1,1,1]
Output: 0
Explanation: No triplets meet the conditions so we return 0.
Constraints:
3 <= nums.length <= 100
1 <= nums[i] <= 1000
public class Solution {
public int unequalTriplets(int[] nums) {
int trips = 0;
int pairs = 0;
int[] count = new int[1001];
for (int i = 0; i < nums.length; ++i) {
trips += pairs - count[nums[i]] * (i - count[nums[i]]);
pairs += i - count[nums[i]];
count[nums[i]] += 1;
}
return trips;
}
}