<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p>

<p>Find a pair of indices <code>(i, j)</code> such that:</p>

<ul>
	<li><code>i != j</code>,</li>
	<li><code>abs(i - j) &lt;= indexDiff</code>.</li>
	<li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li>
</ul>

<p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --&gt; 0 != 3
abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3
abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
<strong>Output:</strong> false
<strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
</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>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= indexDiff &lt;= nums.length</code></li>
	<li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li>
</ul>

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds;

class Solution {
public:
    using o_set = tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {
        int i=0,j=0;
        o_set st;
        while(i<nums.size()){
            if(j<nums.size() && j-i+1<=indexDiff+1){
                if(st.find(nums[j])!=st.end()){
                    return true;
                }
                st.insert(nums[j]);
                j++;
            } else if(j<nums.size()) {
                st.erase(st.find(nums[i]));
                i++;
            }

            if(j-i==indexDiff+1 || j==nums.size()){
                if(valueDiff==0){
                    if(j==nums.size()){
                        i++;
                    }
                    continue;
                }
                int searchMaxVal=nums[i]+valueDiff;
                int searchMinVal=nums[i]-valueDiff;

                auto maxValInd=st.order_of_key(searchMaxVal);
                auto valInd=st.order_of_key(nums[i]);
                auto minValInd=st.order_of_key(searchMinVal);
                
                auto maxVal=*st.find_by_order(maxValInd);
                auto val=*st.find_by_order(valInd);
                auto minVal=*st.find_by_order(minValInd);

                if(maxVal==searchMaxVal || searchMinVal==minVal){
                    return true;
                }

                if(maxValInd-valInd+1>2 || (valInd-minValInd+1>1 && minVal>searchMinVal)){
                    return true;
                }
                if(j==nums.size()){
                    i++;
                }
            }
        }
        return false;
    }
};


/*

iD= 2
vD= 3

1   5   9   1   5   9


9   
5 









*/
168418