Problem: give an array A of N (1 <= N <= 2 * 10^6) integer. Find the number of good triplets (i, j, k), where i, j, k are all distinct indices such that 1 <= i < j < k <= N. A good triplet (i, j, k) is a triplet such that A[i] = x, A[j] = y, A[k] = z(x, y, z are given numbers).
Example 1:
Input: 5
1 2 2 1 2
1 2 1
Output:
2
Explanation: two triplets are (1,2,4) and (1,3,4)
Example 2:
Input:
5
1 2 2 1 2
2 1 1
Output:
0
Explanation: there are no good triplets
For every j, calculate the number of i and k, and add their product to the answer. You can find the number of i and k by precalculating either by using map or some array.
you can use frequency array or map as you wish and use product to calculate the answer freq[x]*freq[y]*freq[z]if they are distinct.