Finding largest triplet product in a stream in Python

In this article, we will see what is the largest triple product and how to perform this using Python.

Largest Triple Product:

The largest triplet product is the product of the 3 largest elements that belong to a subset of a stream. The given stream of integers can be represented in the form of a list.

Example :

If the input list is: [ 1, 2, 3, 4, 5 ]

The output will be: -1, -1, 6, 24, 60

Explanation :

If the iterative variable is less than 2 the output will be -1. When the iterative variable is 2 only three elements are there [1, 2, 3] so the output will be 6. If the iterative variable is 3, the elements are [ 1, 2, 3, 4] so the three largest elements will be [2, 3, 4] and the output will be 24 and so on…

Algorithm :

  1.  Read the number of elements.
  2.  Read the elements of the list.
  3.  Form subsets according to the value of the iterating variable.
  4.  Sort the subset(descending order).
  5.  Check the number of elements in a subset if less than 2 print -1 or print the product of the first three elements.
  6.  Check the value of the iterative variable with the number of elements in the list, until both are equal goto step 4.

Code: Find the largest triplet product in a stream in Python

n=int(input('Number of elements in the list:'))

for i in range(n):
    c=int(input('enter the element'))
for i in range(len(a)):
    if i>=2:
        for j in range(i+1):
        for j in range(3):






Functions to Understand :

Below are some functions that are mentioned in the code above and understanding these functions is essential.

  1. list(): This method creates an empty list.
  2. input(): This method is used to get values from the user.
  3. list.append(): This method adds an element to the list.
  4. list.sort(reverse=True): This method is used to arrange the elements of a list in descending order.
  5. list.clear(): This method is used to clear all elements that are present in a list.
  6. len(list): This method returns the length of the list.

Code Explanation:

a and b are two empty lists one to store the main list and other to store subsets of the main list respectively.

n is the number of elements in the list.

Using the append method the elements for the main list are added.

If the number of elements in the subset is greater than 2 then, these elements will be added to the list b, else -1 will be printed.

After appending into the list b, the elements are then sorted.

The 3 largest elements of the sorted list are multiplied and printed.

After printing the product the list b is cleared.

And the same process is repeated until the number of elements in the lists a and b is the same.

Also read: How to Use lstrip Method in Python

Leave a Reply

Your email address will not be published. Required fields are marked *