# Find the Increasing subsequence of length three with maximum product

• Difficulty Level : Hard
• Last Updated : 21 Jun, 2022

Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order.  Examples:

```
Input:
arr[] = {6, 7, 8, 1, 2, 3, 9, 10}
Output:
8 9 10

Input:
arr[] = {1, 5, 10, 8, 9}
Output: 5 8 9```

Approach: Since we want to find the maximum product, we need to find following two things for every element in the given sequence:

• LSL: The largest smaller element on left of given element
• LGR: The largest greater element on right of given element.

Once we find LSL and LGR for an element, we can find the product of element with its LSL and LGR (if they both exist). We calculate this product for every element and return maximum of all products.

A simple method is to use nested loops. The outer loop traverses every element in sequence. Inside the outer loop, run two inner loops (one after other) to find LSL and LGR of current element. Time complexity of this method is O(n2). We can do this in O(nLogn) time.

For simplicity, let us first create two arrays LSL[] and LGR[] of size n each where n is number of elements in input array arr[]. The main task is to fill two arrays LSL[] and LGR[]. Once we have these two arrays filled, all we need to find maximum product LSL[i]*arr[i]*LGR[i] where 0 < i < n-1 (Note that LSL[i] doesn’t exist for i = 0 and LGR[i] doesn’t exist for i = n-1). We can fill LSL[] in O(nLogn) time. The idea is to use a Balanced Binary Search Tree like AVL. We start with empty AVL tree, insert the leftmost element in it. Then we traverse the input array starting from the second element to second last element. For every element currently being traversed, we find the floor of it in AVL tree.

If a floor exists, we store the floor in LSL[], otherwise we store NIL. After storing the floor, we insert the current element in the AVL tree. We can fill LGR[] in O(n) time. The idea is similar to this post. We traverse from right side and keep track of the largest element. If the largest element is greater than current element, we store it in LGR[], otherwise, we store NIL. Finally, we run a O(n) loop and return maximum of LSL[i]*arr[i]*LGR[i]. Note that we can avoid space required for LSL, we can find and use LSL values in final loop.

Implementation:

## Python3

 `from` `bisect ``import` `bisect_left``class` `Solution:``    ``def` `BinarySearch(``self``,a, x):``        ``i ``=` `bisect_left(a, x)``        ``if` `i:``            ``return` `(i``-``1``)``        ``else``:``            ``return` `-``1``    ``def` `countArray(``self``, arr, n) :``        ``# Calculate LGR for each element``        ``LGR``=``[i ``for` `i ``in` `arr]``        ``LGR[``-``1``]``=``-``1``        ``max_from_right ``=` `arr[n``-``1``]``        ``for` `i ``in` `range``(n``-``2``,``-``1``,``-``1``):``            ``temp``=``LGR[i]``            ``LGR[i]``=``max_from_right``            ``if` `max_from_right< temp:``                ``max_from_right``=``temp``        ``# Calculate LSL for each element``        ``LSL ``=` `[``0``] ``*` `(n)``        ``LSL[``0``] ``=` `-``1``        ``lst ``=` `[]``        ``lst.append(arr[``0``])` `        ``for` `i ``in` `range``(``1``, n):``            ``idx ``=` `self``.BinarySearch(lst, arr[i])``            ``if``(idx !``=` `-``1``):``                ``LSL[i] ``=` `lst[idx]``            ``lst.insert(idx``+``1` `, arr[i])``        ``maxProduct``=``float``(``'-inf'``)``        ``ans``=``[``-``1``]``        ``for` `i ``in` `range``(``0``,n):``            ``currP ``=` `LSL[i]``*``arr[i]``*``LGR[i]``            ``if` `currP>maxProduct ``and` `LSL[i]

Output

`8 9 10`

Time Complexity:-  O(nLogn)
Auxiliary Space:- O(n)

My Personal Notes arrow_drop_up