<p>You are given an array of <code>intervals</code>, where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> and each <code>start<sub>i</sub></code> is <strong>unique</strong>.</p>

<p>The <strong>right interval</strong> for an interval <code>i</code> is an interval <code>j</code> such that <code>start<sub>j</sub> &gt;= end<sub>i</sub></code> and <code>start<sub>j</sub></code> is <strong>minimized</strong>. Note that <code>i</code> may equal <code>j</code>.</p>

<p>Return <em>an array of <strong>right interval</strong> indices for each interval <code>i</code></em>. If no <strong>right interval</strong> exists for interval <code>i</code>, then put <code>-1</code> at index <code>i</code>.</p>

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

<pre>
<strong>Input:</strong> intervals = [[1,2]]
<strong>Output:</strong> [-1]
<strong>Explanation:</strong> There is only one interval in the collection, so it outputs -1.
</pre>

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

<pre>
<strong>Input:</strong> intervals = [[3,4],[2,3],[1,2]]
<strong>Output:</strong> [-1,0,1]
<strong>Explanation:</strong> There is no right interval for [3,4].
The right interval for [2,3] is [3,4] since start<sub>0</sub> = 3 is the smallest start that is &gt;= end<sub>1</sub> = 3.
The right interval for [1,2] is [2,3] since start<sub>1</sub> = 2 is the smallest start that is &gt;= end<sub>2</sub> = 2.
</pre>

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

<pre>
<strong>Input:</strong> intervals = [[1,4],[2,3],[3,4]]
<strong>Output:</strong> [-1,2,-1]
<strong>Explanation:</strong> There is no right interval for [1,4] and [3,4].
The right interval for [2,3] is [3,4] since start<sub>2</sub> = 3 is the smallest start that is &gt;= end<sub>1</sub> = 3.
</pre>

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

<ul>
	<li><code>1 &lt;= intervals.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>intervals[i].length == 2</code></li>
	<li><code>-10<sup>6</sup> &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>6</sup></code></li>
	<li>The start point of each interval is <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    pair<int,int> helper(vector<pair<int,int>> kl,int s,int e,int k){
        if(s>e){
            //return -1;
            pair<int,int> se;
            se.first=-1;
            se.second=-1;
            return se;
        }
        int mid=(s+e)/2;
        if(kl[mid].first==k){
            pair<int,int> ur=helper(kl,s,mid-1,k);
            if(ur.second>-1){
                return ur;
            }
            //return mid;
            pair<int,int> mi;
            mi.first=kl[mid].first;
            mi.second=kl[mid].second;
            return mi;
        }else if(kl[mid].first<k){
            pair<int,int> ur=helper(kl,mid+1,e,k);
            return ur;
        }
        pair<int,int> ur=helper(kl,s,mid-1,k);
        if(ur.second>-1){
            return ur;
        }
        //return ur;
        pair<int,int> mi;
        mi.first=kl[mid].first;
        mi.second=kl[mid].second;
        return mi;

    }
    vector<int> findRightInterval(vector<vector<int>>& arr) {
        vector<pair<int,int>> kl;
        for(int i=0;i<arr.size();i++){
            pair<int,int> lk;
            lk.first=arr[i][0];
            lk.second=i;
            kl.push_back(lk);
        }
        sort(kl.begin(),kl.end());
        vector<int> ans;
        vector<pair<int,int>> we;
        for(int i=0;i<arr.size();i++){
            pair<int,int> ki;
            ki.first=arr[i][1];
            ki.second=i;
            we.push_back(ki);
        }
        sort(we.begin(),we.end());
        //int j=0;
        vector<int> ap;
        for(int i=0;i<arr.size();i++){
            ap.push_back(0);

        }
        int j=0;
        for(int i=0;i<we.size();i++){
            pair<int,int> pu=we[i];
            for(j=0;j<kl.size();j++){
                if(kl[j].first>=pu.first){
                    break;
                }
            }
            //ap[pu.second]=
            if(j==kl.size()){
                ap[pu.second]=-1;
            }else{
                ap[pu.second]=kl[j].second;
            }




        }
        return ap;
        
        
    }
};
30556