Along Pier 7, n shipping containers are lined up in a fixed left-to-right order and numbered 0 to n-1. Each container i is stamped with an integer routing code code[i]. The dockmaster wants to know in how many ways she can choose three containers at positions i < j < k such that code[i], code[j], and code[k] are pairwise different — no two of the three chosen codes may match.
n.n space-separated integers code[0] code[1] ... code[n-1].Print a single integer: the number of index triplets (i, j, k) with 0 <= i < j < k < n such that code[i], code[j], and code[k] are pairwise different.
3 <= n <= 1001 <= code[i] <= 1000Example 1
Input
5 1 2 3 1 2
Expected
4
Explanation
The codes are [1,2,3,1,2]. Checking all 10 position triplets, only (0,1,2)->1,2,3, (0,2,4)->1,3,2, (1,2,3)->2,3,1, and (2,3,4)->3,1,2 have three pairwise different codes; every other triplet repeats at least one code. That gives 4 valid triplets.
Example 2
Input
3 5 5 5
Expected
0
Explanation
There is only one possible triplet, (0,1,2), with codes 5, 5, and 5. Since all three codes are equal (not pairwise different), the count is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →