<p>Given an array of <code>n</code> integers <code>nums</code>, a <strong>132 pattern</strong> is a subsequence of three integers <code>nums[i]</code>, <code>nums[j]</code> and <code>nums[k]</code> such that <code>i &lt; j &lt; k</code> and <code>nums[i] &lt; nums[k] &lt; nums[j]</code>.</p>

<p>Return <code>true</code><em> if there is a <strong>132 pattern</strong> in </em><code>nums</code><em>, otherwise, return </em><code>false</code><em>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no 132 pattern in the sequence.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,4,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a 132 pattern in the sequence: [1, 4, 2].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,3,2,0]
<strong>Output:</strong> true
<strong>Explanation:</strong> There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    bool find132pattern(vector<int>& nums) {

        vector<pair<int,int>> l(nums.size()) , r(nums.size());
        int mn = nums[0] , mx = nums[0];
        l[0] = {mn,mx};
        for(int i = 1 ;i<nums.size();i++)
        {

            // if(mn<mx && nums[i]<mx && nums[i]>mn)return 1;
            mn = min(mn,nums[i]);
            mx = max(mx,nums[i]);

            l[i] = {mn,mx};
        } 

        // mn = INT_MAX , mx = INT_MIN;

        stack<int> s;
        for(int i = nums.size()-1;i>=1;i--)
        {
            if(l[i-1].first<nums[i])
            {
                while(!s.empty() && s.top()<=l[i-1].first)s.pop();

                if(!s.empty() && s.top()<nums[i])return 1;
            }

            s.push(nums[i]);

            // r[i] = {mn,mx};
        }

        return 0;


        // for(int i = 1;i<nums.size()-1;i++)
        // {
        //     if(nums[i]>l[i-1].first && nums[i]>r[i+1].first && l[i-1].first < r[i+1].first)return 1;
        //     if(nums[i]>l[i-1].second && nums[i]>r[i+1].first && l[i-1].second < r[i+1].first)return 1;
        //     if(nums[i]>l[i-1].first && nums[i]>r[i+1].second && l[i-1].first < r[i+1].second)return 1;
        //     if(nums[i]>l[i-1].second && nums[i]>r[i+1].second && l[i-1].second < r[i+1].second)return 1;
        // }

        // return 0;
        
    }
};
76440