# 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 :

- Read the number of elements.
- Read the elements of the list.
- Form subsets according to the value of the iterating variable.
- Sort the subset(descending order).
- Check the number of elements in a subset if less than 2 print -1 or print the product of the first three elements.
- 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

b=list() a=list() n=int(input('Number of elements in the list:')) for i in range(n): c=int(input('enter the element')) a.append(c) for i in range(len(a)): if i>=2: for j in range(i+1): b.append(a[j]) b.sort(reverse=True) d=1 for j in range(3): d=d*b[j] print(d) b.clear() else: print(-1)

**Input:**

5 1,2,3,4,5

**Output:**

-1 -1 6 24 60

**Functions to Understand :**

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

- list(): This method creates an empty list.
- input(): This method is used to get values from the user.
- list.append(): This method adds an element to the list.
- list.sort(reverse=True): This method is used to arrange the elements of a list in descending order.
- list.clear(): This method is used to clear all elements that are present in a list.
- 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