[LeetCode] Number of Unequal Triplets in Array

2475. Number of Unequal Triplets in Array

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int unequalTriplets(vector<int>& nums) {
int res = 0;
for(int i = 0; i < nums.size(); i++) {
for(int j = i+1; j < nums.size(); j++) {
if(nums[i] == nums[j]) continue;
for(int k = j + 1; k < nums.size(); k++) {
if(nums[i] == nums[k]) continue;
if(nums[k] == nums[j]) continue;
res += 1;
}
}
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/20/PS/LeetCode/number-of-unequal-triplets-in-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.