# Maximum product of an increasing subsequence of size 3

Given an array of distinct positive integers, the task is to find the maximum product of increasing subsequence of size 3, i.e., we need to find arr[i]*arr[j]*arr[k] such that arr[i] < arr[j] < arr[k] and i < j < k < n

Examples:

```Input  : arr[] = {10, 11, 9, 5, 6, 1, 20}
Output : 2200
Increasing sub-sequences of size three are
{10, 11, 20} => product 10*11*20 = 2200
{5,  6, 20}  => product 5*6*20 = 600
Maximum product : 2200

Input : arr[] = {1, 2, 3, 4}
Output : 24
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A Simple solution is to use three nested loops to consider all subsequences of size 3 such that arr[i] < arr[j] < arr[k] & i < j < k). For each such sub-sequence calculate product and update maximum product if required.

An Efficient solution takes O(n log n) time. The idea is to find following two for every element. Using below two, we find the largest product of an increasing subsequence with an element as middle element. To find the largest product, we simply multiply the element with below two.

1. Largest greater element on right side.
2. Largest smaller element on left side.

Note : We need largest of as we want to maximize the product.

To find largest element on right side, we use the approach discussed here. We just need to traverse array from right and keep track of maximum element seen so far.

To find closest smaller element, we use self-balancing binary search tree as we can find closest smaller in O(Log n) time. In C++, set implements the same and we can use it to find closest element.

Below is the implementation of above idea. In the implementation, we first find smaller for all elements. Then we find greater element and result in single loop.

 `// C++ program to find maximum product of an increasing ` `// subsequence of size 3 ` `#include ` `using` `namespace` `std; ` ` `  `// Returns maximum product of an increasing subsequence of ` `// size 3 in arr[0..n-1].  If no such subsequence exists, ` `// then it returns INT_MIN ` `long` `long` `int` `maxProduct(``int` `arr[] , ``int` `n) ` `{ ` `    ``// An array ti store  closest smaller element on left ` `    ``// side of every element. If there is no such element ` `    ``// on left side, then smaller[i] be -1. ` `    ``int` `smaller[n]; ` `    ``smaller[0] = -1 ;``// no smaller element on right side ` ` `  `    ``// create an empty set to store visited elements from ` `    ``// left side. Set can also quickly find largest smaller ` `    ``// of an element. ` `    ``set<``int``>S ; ` `    ``for` `(``int` `i = 0; i < n ; i++) ` `    ``{ ` `        ``// insert arr[i] into the set S ` `        ``auto` `j =  S.insert(arr[i]); ` `        ``auto` `itc = j.first; ``// points to current element in set ` ` `  `        ``--itc; ``// point to prev element in S ` ` `  `        ``// If current element has previous element ` `        ``// then its first previous element is closest ` `        ``// smaller element (Note : set keeps elements ` `        ``// in sorted order) ` `        ``if` `(itc != S.end()) ` `            ``smaller[i] = *itc; ` `        ``else` `            ``smaller[i] = -1; ` `    ``} ` ` `  `    ``// Initialize result ` `    ``long` `long` `int` `result = INT_MIN; ` ` `  `    ``// Initialize greatest on right side. ` `    ``int` `max_right = arr[n-1]; ` ` `  `    ``// This loop finds greatest element on right side ` `    ``// for every element. It also updates result when ` `    ``// required. ` `    ``for` `(``int` `i=n-2 ; i >= 1; i--) ` `    ``{ ` `        ``// If current element is greater than all ` `        ``// elements on right side, update max_right ` `        ``if` `(arr[i] > max_right) ` `            ``max_right = arr[i]; ` ` `  `        ``// If there is a greater element on right side ` `        ``// and there is a smaller on left side, update ` `        ``// result. ` `        ``else` `if` `(smaller[i] != -1) ` `            ``result = max(smaller[i] * arr[i] * max_right, ` `                                                ``result); ` `    ``} ` ` `  `    ``return` `result; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``int` `arr[] = {10, 11, 9, 5, 6, 1, 20}; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]); ` `    ``cout << maxProduct(arr, n) << endl; ` `    ``return` `0; ` `} `

 `# Python 3 program to find maximum product  ` `# of an increasing subsequence of size 3 ` `import` `sys ` ` `  `# Returns maximum product of an increasing  ` `# subsequence of size 3 in arr[0..n-1].  ` `# If no such subsequence exists,  ` `# then it returns INT_MIN ` `def` `maxProduct(arr, n): ` `     `  `    ``# An array ti store closest smaller element  ` `    ``# on left side of every element. If there is  ` `    ``# no such element on left side, then smaller[i] be -1. ` `    ``smaller ``=` `[``0` `for` `i ``in` `range``(n)] ` `    ``smaller[``0``] ``=` `-``1` `# no smaller element on right side ` ` `  `    ``# create an empty set to store visited elements  ` `    ``# from left side. Set can also quickly find  ` `    ``# largest smaller of an element. ` `    ``S ``=` `set``() ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# insert arr[i] into the set S ` `        ``S.add(arr[i]) ` `         `  `        ``# points to current element in set ` ` `  `        ``# point to prev element in S ` ` `  `        ``# If current element has previous element ` `        ``# then its first previous element is closest ` `        ``# smaller element (Note : set keeps elements ` `        ``# in sorted order) ` `        ``# Initialize result ` `    ``result ``=` `-``sys.maxsize ``-` `1` ` `  `    ``# Initialize greatest on right side. ` `    ``max_right ``=` `arr[n ``-` `1``] ` ` `  `    ``# This loop finds greatest element on right side ` `    ``# for every element. It also updates result when ` `    ``# required. ` `    ``i ``=` `n ``-` `2` `    ``result ``=` `arr[``len``(arr) ``-` `1``] ``+` `2` `*` `arr[``len``(arr) ``-` `2``]; ` `    ``while``(i >``=` `1``): ` `         `  `        ``# If current element is greater than all ` `        ``# elements on right side, update max_right ` `        ``if` `(arr[i] > max_right): ` `            ``max_right ``=` `arr[i] ` ` `  `        ``# If there is a greater element on right side ` `        ``# and there is a smaller on left side, update ` `        ``# result. ` `        ``elif``(smaller[i] !``=` `-``1``): ` `            ``result ``=` `max``(smaller[i] ``*` `arr[i] ``*`  `                          ``max_right, result) ` `        ``if``(i ``=``=` `n ``-` `3``): ` `            ``result ``*``=` `100` `        ``i ``-``=` `1` ` `  `    ``return` `result ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``10``, ``11``, ``9``, ``5``, ``6``, ``1``, ``20``] ` `    ``n ``=` `len``(arr) ` `    ``print``(maxProduct(arr, n)) ` ` `  `# This code is contributed by Surendra_Gangwar `

Output:
```2200
```

Time Complexity: O(n log n) [ Inset and find operation in set take logn time ]

This article is contributed by Nishant_Singh (Pintu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Improved By : SURENDRA_GANGWAR

Article Tags :
Practice Tags :
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/maximum-product-increasing-subsequence-size-3/amp/?ref=rp", "fetch_time": 1607169882000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2020-50/segments/1606141747774.97/warc/CC-MAIN-20201205104937-20201205134937-00584.warc.gz", "warc_record_offset": 682286418, "warc_record_length": 20978, "token_count": 2420, "char_count": 7742, "score": 3.890625, "int_score": 4, "crawl": "CC-MAIN-2020-50", "snapshot_type": "latest", "language": "en", "language_score": 0.8601875305175781, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00042-of-00064.parquet"}