## Count The Triplets

View as PDF

Points: 7
Time limit: 0.6s
Memory limit: 64M

Problem type

Given an array of integers, the task is to count all the triplets, where a valid triplet, , has and . When counting triplets, only count the unique ones. Two triplets are different when, after sorting the elements corresponding to the indices, the resulting lists are different. For example, if the elements corresponding to the triplets are 1 2 3 and 3 2 1, then they are not unique, but the triplets 8 5 3 and 8 4 4 are unique. The triplet elements themselves do not necessarily have to be unique, this means that 8 4 4 can be possible, as long as the 2 4s do not have the same index.

#### Input Specification

The first line of input contains , denoting the size of the array.

The second line of the input contains space separated elements , the elements of the array.

#### Output Specification

Output the number of unique triplets in the array, if there are no such triplets, output -1.

#### Sample Input 1

4
1 5 3 2

#### Sample Output 1

2

#### Explanation for Sample Output 1

There are such triplets, 1 2 3 and 2 3 5.

#### Sample Input 2

3
3 2 7

#### Sample Output 2

-1

#### Sample Input 3

6
4 1 2 3 1 2

#### Sample Output 3

4

• commented on March 22, 2022, 4:31 a.m.

Since the data were weak, more test cases were added.

• commented on Jan. 3, 2021, 9:08 p.m.

Got segmentation errors that made me so confused, I once tried adding a "cout" instruction and it changes the result, somehow. GG for everyone

• commented on Dec. 31, 2019, 6:18 p.m.

Hints to not TLE with PY3?

• commented on Jan. 19, 2020, 5:10 p.m. edited

Don't use PY3 and learn basic math.

• commented on July 24, 2019, 9:49 p.m.

Is the test data wrong or is everyone just bad?

• commented on July 24, 2019, 10:18 p.m.

I'll have to check the test-data