New update is available. Click here to update.

Last Updated: 11 Oct, 2020

Difficulty: Easy

```
If the array is [2, 3, 3, 4], the answer should be:-
-1
-1
18 (3 * 3 * 2)
36 (4 * 3 * 3)
```

```
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains an integer N, representing the number of elements in the array.
The second line contains N single space-separated integers X[0],X[1],X[2].... X[N-1] where X[i] is an element of the array.
```

```
For each test case, return the required list. If there is no second largest or third largest number in the array X up to that index then print "-1", without the quotes.
```

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

```
1 <= T <= 5
1 <= N <= 10^5
1 <= X[i] <= 10^6
Time limit: 1 sec
```

- The idea is to work with the max priority queue.
- Insert the elements of array ‘ARR’ one by one.
- Once you add the element, extract 3 elements from the max priority queue. They will, of course, be largest (the one extracted the first time), second largest (the one extracted the second time) and third largest (the one extracted the third time).
- It should be noted that while executing point 3, one should take care of the time when the size of the priority queue will be less than 2.
- Now, all you need to do is find the product of the three numbers and print the answer.

SIMILAR PROBLEMS

Min Heap

Posted: 5 May, 2022

Difficulty: Moderate

Max tasks in the given Budget

Posted: 10 Jun, 2022

Difficulty: Moderate

Time to publish comic books

Posted: 10 Jun, 2022

Difficulty: Moderate

Min Heap Implementation

Posted: 22 Jun, 2022

Difficulty: Moderate

Find Median from Data Stream

Posted: 9 Sep, 2022

Difficulty: Moderate

Popular Interview Problems: