VOOZH about

URL: https://www.geeksforgeeks.org/dsa/largest-triplet-product-stream/

⇱ Largest triplet product in a stream - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Largest triplet product in a stream

Last Updated : 14 Feb, 2025

Given a stream of integers represented as arr[]. For each index i from 0 to n-1, print the multiplication of the largest, second largest, and 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

Input: arr[] = {-5, -2, 0, 3, 6, 7}
Output: -1 -1 0 0 0 126
Explanation: For i = 2: Three elements are {-5, -2, 0}, so their product = 0.
For i = 3: Three largest elements are {-2, 0, 3}, product = 0.
For i = 4: Three largest elements are {0, 3, 6}, product = 0.
For i = 5: Three largest elements are {3, 6, 7}, product = 3*6*7 = 126.

Input: arr[] = {2, 2, 2, 2, 2}
Output: -1 -1 8 8 8
Explanation: For i = 2: Three elements {2, 2, 2}, so 2*2*2 = 8.
For i = 3 and onwards, the largest three elements are always {2, 2, 2}, so output remains 8.

Using Heap - O(n * logn) Time and O(n) Space

The idea is to maintain the three largest elements encountered so far using a heap data structure. For every element in the input, if fewer than three elements are available, the output is -1. Otherwise, the product of the three largest elements is calculated and stored.

Steps to implement the above idea:

  • Initialize a max-heap to track the largest elements and a result list for storing outputs.
  • Traverse the input array and push each element into the max-heap.
  • If the heap size is less than 3, append -1 to the result list.
  • When the heap size is at least 3, extract the three largest elements from the heap.
  • Calculate the product of these three elements, append it to the result list, and push them back into the heap.
  • Finally, return or print the result list containing the outputs.

Output
-1 -1 6 24 60 

Without Using Heap - O(n) Time and O(1) Space

The idea is to track the three largest elements encountered so far using three variables. For each element in the input, update these variables if the current element is larger than any of them. If fewer than three elements exist or the third largest is uninitialized, append -1. Otherwise, calculate the product of the three largest and store it in the result.

Steps to implement the above idea:

  • Initialize three variables to store the largest, second largest, and third largest values, starting with the smallest possible values.
  • Create a result list to store the output for each step.
  • Traverse the input array, updating the three variables whenever a new larger element is encountered.
  • If fewer than three elements have been processed or the third variable remains uninitialized, append -1 to the result.
  • Otherwise, calculate the product of the three largest variables and append it to the result.
  • Finally, print the result list containing the products or -1 values.

Output
-1 -1 6 24 60 


Comment
Article Tags: