<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>

class Solution {
public:

    bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {
        int n = nums.size();

        // Make a vector of pairs. Each pair is the value in nums and its index.
        vector<pair<int, int>> numberAndIndex(n);
        for(int ii=0; ii<n; ++ii)
        {
            numberAndIndex[ii] = pair<int, int>{nums[ii], ii};
        }
        
        int chunk = (2 * (indexDiff+1));
        vector<pair<int, int>> sortedArray(chunk);

        for(int ii=0; ii<n; ii+=(chunk/2))
        {
            if (ii + chunk < n)
            {
                copy(numberAndIndex.begin()+ii, numberAndIndex.begin()+ii+chunk, sortedArray.begin());
                sort(sortedArray.begin(), sortedArray.end());
                for(int jj=0; jj<(chunk/2); ++jj)
                {
                    int kk = jj+1;
                    while((kk < sortedArray.size()) && (abs(sortedArray[jj].second - sortedArray[kk].second) > indexDiff))
                    {
                        ++kk;
                    }
                    if((kk <sortedArray.size()) && ((sortedArray[kk].first - sortedArray[jj].first) <= valueDiff))
                    {
                        return true;
                    }
                }
            }
            else
            {
                sortedArray = vector<pair<int, int>>(n - ii);
                copy(numberAndIndex.begin()+ii, numberAndIndex.end(), sortedArray.begin());
                sort(sortedArray.begin(), sortedArray.end());
                for(int jj=0; jj<sortedArray.size(); ++jj)
                {
                    int kk = jj+1;
                    while((kk < sortedArray.size()) && (abs(sortedArray[jj].second - sortedArray[kk].second) > indexDiff))
                    {
                        ++kk;
                    }
                    if((kk <sortedArray.size()) && ((sortedArray[kk].first - sortedArray[jj].first) <= valueDiff))
                    {
                        return true;
                    }
                }
            }  
        }

        return false;
    }
};
122891