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

from collections import deque 

class Solution:
    def __init__(self):
        self.mydict = set()

    def containsNearbyAlmostDuplicate(self, nums: list[int], indexDiff: int, valueDiff: int) -> bool:
        #intiial part of the code converts nums into segments that we care about in O(n) time. 
        k = indexDiff
        segments = []
        temp = []
        for num in nums:
            temp.append(num)
            if len(temp) == k+1: 
                segments.append(temp)
                temp = [] 
        if temp!= []:
            segments.append(temp)
        
        #The next part will deal with the main part of the code 
        for i,seglist in enumerate(segments):
            queue1 = deque([i for i in seglist])
            if i < len(segments) - 1: 
                queue2 = deque([i for i in segments[i+1]])
            else: 
                queue2 = deque([])
            sortedlist = sorted(seglist)

            
            for i in range(1,len(sortedlist)):
                if abs(sortedlist[i] - sortedlist[i-1]) <= valueDiff:
                    return True 
            
            while queue2:
                self.mydict.add(queue1.popleft())
                checkVal = queue2.popleft()
                print(checkVal)
                ind = self.binSearch(sortedlist, checkVal)

                if ind> -1:
                    if sortedlist[ind] not in self.mydict:
                        if abs(checkVal-sortedlist[ind]) <= valueDiff:
                            return True
                    
                if ind<len(sortedlist) -1: 
                    if sortedlist[ind+1] not in self.mydict:
                        if abs(checkVal-sortedlist[ind+1]) <= valueDiff:
                            return True 
        return False 

    def binSearch(self, sortedlist: list[int], checkVal: int) -> int: #returns the value of the index after which checkVal should go
        base = 0
        ceiling = len(sortedlist) - 1 

        while True: 
            curr = base + ((ceiling - base)//2)
        
            if checkVal >= sortedlist[curr+1] and curr+1 == len(sortedlist) - 1:
                return curr + 1 
                
            if checkVal <= sortedlist[curr] and curr == 0:
                return curr - 1 
            
            if checkVal >= sortedlist[curr] and curr != len(sortedlist) - 1:
                if checkVal <= sortedlist[curr+1]:
                    return curr 
            
            if checkVal < sortedlist[curr]:
                ceiling = curr 
                
            if checkVal >= sortedlist[curr]: 
                base = curr 
32584