<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) {
        size_t nums_sz = nums.size();
        if (nums_sz - 1 < indexDiff)
        {
            indexDiff = nums_sz - 1;
        }
        
        vector<int> nums_sorted;
        copy(nums.begin(), nums.begin() + indexDiff + 1, back_inserter(nums_sorted));

        size_t nums_sorted_sz = nums_sorted.size();

        sort(nums_sorted.begin(), nums_sorted.end());

        for (int i = 1; i < nums_sorted_sz; i++)
        {
            if (abs(nums_sorted[i-1] - nums_sorted[i]) <= valueDiff)
            {
                return true;
            }
        }

        int to_add_elem;
        int to_del_elem;
        int to_add_elem_id;
        for (int i = indexDiff + 1; i < nums_sz; i++)
        {
            to_add_elem = nums[i];
            to_del_elem = nums[i - indexDiff - 1];

            to_add_elem_id = distance(nums_sorted.begin(), lower_bound(nums_sorted.begin(), nums_sorted.end(), to_del_elem));
            nums_sorted[to_add_elem_id] = to_add_elem;

            if (to_add_elem > to_del_elem)
            {
                for(int j = to_add_elem_id; j < nums_sorted_sz - 1; j++)
                {
                    if (nums_sorted[j + 1] >= nums_sorted[j])
                    {
                        to_add_elem_id = j;
                        break;
                    }
                    swap(nums_sorted[j + 1], nums_sorted[j]);
                }
            }
            else if (to_add_elem < to_del_elem)
            {
                for(int j = to_add_elem_id; j > 0; j--)
                {
                    if (nums_sorted[j - 1] <= nums_sorted[j])
                    {
                        to_add_elem_id = j;
                        break;
                    }
                    swap(nums_sorted[j - 1], nums_sorted[j]);
                }
            }
            else // if to_add_elem = to_del_elem
            {
                continue;
            }

            if (to_add_elem_id == 0)
            {
                if (abs(nums_sorted[0] - nums_sorted[1]) <= valueDiff)
                {
                    return true;
                }
            }
            else if (to_add_elem_id == nums_sorted_sz - 1)
            {
                if (abs(nums_sorted[to_add_elem_id - 1] - nums_sorted[to_add_elem_id]) <= valueDiff)
                {
                    return true;
                }
            }
            else
            {
                if (abs(nums_sorted[to_add_elem_id - 1] - nums_sorted[to_add_elem_id]) <= valueDiff ||
                    abs(nums_sorted[to_add_elem_id + 1] - nums_sorted[to_add_elem_id]) <= valueDiff)
                {
                    return true;
                }
            }
        }

        return false;
    }
};
96423