<p>Given an integer array <code>nums</code>, partition it into two (contiguous) subarrays <code>left</code> and <code>right</code> so that:</p>

<ul>
	<li>Every element in <code>left</code> is less than or equal to every element in <code>right</code>.</li>
	<li><code>left</code> and <code>right</code> are non-empty.</li>
	<li><code>left</code> has the smallest possible size.</li>
</ul>

<p>Return <em>the length of </em><code>left</code><em> after such a partitioning</em>.</p>

<p>Test cases are generated such that partitioning exists.</p>

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

<pre>
<strong>Input:</strong> nums = [5,0,3,8,6]
<strong>Output:</strong> 3
<strong>Explanation:</strong> left = [5,0,3], right = [8,6]
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,0,6,12]
<strong>Output:</strong> 4
<strong>Explanation:</strong> left = [1,1,1,0], right = [6,12]
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li>There is at least one valid answer for the given input.</li>
</ul>

/*
1. lets assume we have only one element(i.e arr[0]) in left subarray.
2. max element of left arry (lmax) for now is arr[0] and partition is at 1.
3. Traverse all the elements from 1 to n-1.
    - if element is smaller than lmax, increase partition by 1;
    - else if element is greater than lmax, take all the element ahead as a right subarray part untill its
        reaches the end while doing that also store the max element of right subarray (rmax).
        
        - if we get a element which is smaller than lmax then, new partitoion is index of smaller element + 1
            and max element of new left subarray is max of the right subarray we calculated. 

*/
class Solution {
public:
    int partitionDisjoint(vector<int>& arr) {
        int lmax = arr[0]; 
        int partition = 1;
        for(int i=1;i<arr.size();){
            if(arr[i] < lmax){
                partition++;
                i++;
            }else{
                int j=i;
                int rmax = 0;
                while(j<arr.size()){
                    if(arr[j] < lmax){
                        lmax = rmax;
                        partition = j + 1;
                        break;
                    }else{
                        rmax = max(rmax,arr[j]);
                    }
                    j++;
                }
                i = j+1;
            }
        }
        return partition;
    }
};
97100