Given a stream of integers represented as arr[]. For each index i from 0 to n-1, print the multiplication of largest, second largest, third largest element of the subarray arr[0…i]. If i < 2 print -1.

Examples:

Input : arr[] = {1, 2, 3, 4, 5} Output :-1 -1 6 24 60 Explanation : for i = 2 only three elements are there {1, 2, 3} so answer is 6. For i = 3 largest three elements are {2, 3, 4} their product is 2*3*4 = 24 ....so on

We will use priority queue here.

- Insert arr[i] in the priority queue
- As the top element in priority queue is largest so pop it and store it as x. Now the top element in the priority queue will be the second largest element in subarray arr[0…i] pop it and store as y. Now the top element is third largest element in subarray arr[0…i] so pop it and store it as z.
- Print x*y*z
- Reinsert x, y, z.

## C++

`// C++ implementation of largest triplet ` `// multiplication ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Prints the product of three largest numbers ` `// in subarray arr[0..i] ` `void` `LargestTripletMultiplication(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// call a priority queue ` ` ` `priority_queue<` `int` `> q; ` ` ` ` ` `// traversing the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// pushing arr[i] in array ` ` ` `q.push(arr[i]); ` ` ` ` ` `// if less than three elements are present ` ` ` `// in array print -1 ` ` ` `if` `(q.size() < 3) ` ` ` `cout << ` `"-1"` `<< endl; ` ` ` `else` `{ ` ` ` `// pop three largest elements ` ` ` `int` `x = q.top(); ` ` ` `q.pop(); ` ` ` `int` `y = q.top(); ` ` ` `q.pop(); ` ` ` `int` `z = q.top(); ` ` ` `q.pop(); ` ` ` ` ` `// Reinsert x, y, z in priority_queue ` ` ` `int` `ans = x * y * z; ` ` ` `cout << ans << endl; ` ` ` `q.push(x); ` ` ` `q.push(y); ` ` ` `q.push(z); ` ` ` `} ` ` ` `} ` ` ` `return` `; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `LargestTripletMultiplication(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of largest triplet ` `// multiplication ` `import` `java.util.Collections; ` `import` `java.util.PriorityQueue; ` ` ` `class` `GFG { ` ` ` ` ` `// Prints the product of three largest numbers ` ` ` `// in subarray arr[0..i] ` ` ` `static` `void` `LargestTripletMultiplication(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// call a priority queue ` ` ` `PriorityQueue<Integer> q = ` `new` `PriorityQueue(Collections.reverseOrder()); ` ` ` ` ` `// traversing the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `// pushing arr[i] in array ` ` ` `q.add(arr[i]); ` ` ` ` ` `// if less than three elements are present ` ` ` `// in array print -1 ` ` ` `if` `(q.size() < ` `3` `) ` ` ` `System.out.println(` `"-1"` `); ` ` ` `else` `{ ` ` ` `// pop three largest elements ` ` ` `int` `x = q.poll(); ` ` ` `int` `y = q.poll(); ` ` ` `int` `z = q.poll(); ` ` ` ` ` `// Reinsert x, y, z in priority_queue ` ` ` `int` `ans = x * y * z; ` ` ` `System.out.println(ans); ` ` ` `q.add(x); ` ` ` `q.add(y); ` ` ` `q.add(z); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` `LargestTripletMultiplication(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by shubham96301 ` |

*chevron_right*

*filter_none*

**Output:**

-1 -1 6 24 60

This article is contributed by **Ayush Jha**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest lexicographic triplet from a given Array that forms a triangle
- Maximum product of a triplet (subsequence of size 3) in array
- Find distinct integers for a triplet with given product
- K'th largest element in a stream
- Find a triplet that sum to a given value
- Pythagorean Triplet in an array
- Smallest Difference Triplet from Three arrays
- Find a triplet such that sum of two equals to third element
- Maximum triplet sum in array
- Triplet pair (a, b, c) such that a+b, b+c and a+c are all divisible by K
- Find a triplet in an array whose sum is closest to a given number
- Find triplet with minimum sum
- Check whether there exists a triplet (i, j, k) such that arr[i] < arr[k] < arr[j] for i < j < k
- Minimum possible sum of prices of a Triplet from the given Array
- Maximum value of expression (arr[i] + arr[j] * arr[k]) formed from a valid Triplet
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Largest product of a subarray of size k
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Check if the product of the K largest sums of subarrays is greater than M