Largest triplet product in a stream
Last Updated :
15 Dec, 2022
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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LargestTripletMultiplication( int arr[], int n)
{
priority_queue< int > q;
for ( int i = 0; i < n; i++) {
q.push(arr[i]);
if (q.size() < 3)
cout << "-1" << endl;
else {
int x = q.top();
q.pop();
int y = q.top();
q.pop();
int z = q.top();
q.pop();
int ans = x * y * z;
cout << ans << endl;
q.push(x);
q.push(y);
q.push(z);
}
}
return ;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
LargestTripletMultiplication(arr, n);
return 0;
}
|
Java
import java.util.Collections;
import java.util.PriorityQueue;
class GFG {
static void LargestTripletMultiplication( int arr[], int n)
{
PriorityQueue<Integer> q = new PriorityQueue(Collections.reverseOrder());
for ( int i = 0 ; i < n; i++) {
q.add(arr[i]);
if (q.size() < 3 )
System.out.println( "-1" );
else {
int x = q.poll();
int y = q.poll();
int z = q.poll();
int ans = x * y * z;
System.out.println(ans);
q.add(x);
q.add(y);
q.add(z);
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
LargestTripletMultiplication(arr, n);
}
}
|
Python3
from queue import PriorityQueue
def LargestTripletMultiplication(arr, n):
q = PriorityQueue()
for i in range (n):
q.put( - arr[i])
if (q.qsize() < 3 ):
print ( - 1 )
else :
x = q.get()
y = q.get()
z = q.get()
ans = x * y * z
print ( - ans)
q.put(x);
q.put(y);
q.put(z);
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
LargestTripletMultiplication(arr, n)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void LargestTripletMultiplication( int []arr, int n)
{
List< int > q = new List< int >();
for ( int i = 0; i < n; i++)
{
q.Add(arr[i]);
q.Sort();
q.Reverse();
if (q.Count < 3)
Console.WriteLine( "-1" );
else
{
int x = q[0];
int y = q[1];
int z = q[2];
q.RemoveRange(0, 3);
int ans = x * y * z;
Console.WriteLine(ans);
q.Add(x);
q.Add(y);
q.Add(z);
}
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
LargestTripletMultiplication(arr, n);
}
}
|
Javascript
<script>
function LargestTripletMultiplication(arr , n) {
var q = [];
for (i = 0; i < n; i++) {
q.push(arr[i]);
q.sort();
if (q.length < 3)
document.write( "-1<br/>" );
else {
q.sort();
var x = q.pop();
var y = q.pop();
var z = q.pop();
var ans = x * y * z;
document.write(ans+ "<br/>" );
q.push(x);
q.push(y);
q.push(z);
q.sort();
q.reverse();
}
}
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
LargestTripletMultiplication(arr, n);
</script>
|
Time Complexity: O(N * log N ).
Auxiliary Space: O(N), N is the length of the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...