Skip to content

Commit 00cced9

Browse files
solves #2475: Number of Unequal Triplets in Array in java
1 parent 96d65c4 commit 00cced9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@
781781
| 2465 | [Number of Distinct Averages](https://leetcode.com/problems/number-of-distinct-averages) | [![Java](assets/java.png)](src/NumberOfDistinctAverages.java) | |
782782
| 2466 | [Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings) | | |
783783
| 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature) | [![Java](assets/java.png)](src/ConvertTheTemperature.java) | |
784-
| 2475 | [Number of Unequal Triplets in Array](https://leetcode.com/problems/number-of-unequal-triplets-in-array) | | |
784+
| 2475 | [Number of Unequal Triplets in Array](https://leetcode.com/problems/number-of-unequal-triplets-in-array) | [![Java](assets/java.png)](src/NumberOfUnequalTripletsInArray.java) | |
785785
| 2481 | [Minimum Cuts to Divide a Circle](https://leetcode.com/problems/minimum-cuts-to-divide-a-circle) | | |
786786
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer) | | |
787787
| 2490 | [Circular Sentence](https://leetcode.com/problems/circular-sentence) | | |
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/number-of-unequal-triplets-in-array
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class NumberOfUnequalTripletsInArray {
9+
public int unequalTriplets(int[] array) {
10+
final Map<Integer, Integer> frequency = new HashMap<>();
11+
int pairs = 0, triplets = 0;
12+
for (int index = 0 ; index < array.length ; index++) {
13+
frequency.put(array[index], frequency.getOrDefault(array[index], 0) + 1);
14+
final int pairsWithElement = index + 1 - frequency.getOrDefault(array[index], 0);
15+
pairs += pairsWithElement;
16+
final int pairsWithoutElement = pairs - pairsWithElement * frequency.getOrDefault(array[index], 0);
17+
triplets += pairsWithoutElement;
18+
}
19+
return triplets;
20+
}
21+
}

0 commit comments

Comments
 (0)