<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of size <code>n</code>, and an integer <code>diff</code>. Find the number of <strong>pairs</strong> <code>(i, j)</code> such that:</p>

<ul>
	<li><code>0 &lt;= i &lt; j &lt;= n - 1</code> <strong>and</strong></li>
	<li><code>nums1[i] - nums1[j] &lt;= nums2[i] - nums2[j] + diff</code>.</li>
</ul>

<p>Return<em> the <strong>number of pairs</strong> that satisfy the conditions.</em></p>

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

<pre>
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong>
There are 3 pairs that satisfy the conditions:
1. i = 0, j = 1: 3 - 2 &lt;= 2 - 2 + 1. Since i &lt; j and 1 &lt;= 1, this pair satisfies the conditions.
2. i = 0, j = 2: 3 - 5 &lt;= 2 - 1 + 1. Since i &lt; j and -2 &lt;= 2, this pair satisfies the conditions.
3. i = 1, j = 2: 2 - 5 &lt;= 2 - 1 + 1. Since i &lt; j and -3 &lt;= 2, this pair satisfies the conditions.
Therefore, we return 3.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [3,-1], nums2 = [-2,2], diff = -1
<strong>Output:</strong> 0
<strong>Explanation:</strong>
Since there does not exist any pair that satisfies the conditions, we return 0.
</pre>

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

<ul>
	<li><code>n == nums1.length == nums2.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= diff &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
        pass


class Solution:
    def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
        seen = []
        cnt = 0
        for n1, n2 in zip(nums1, nums2):
            for elem in seen:
                if n1 - n2 + diff >= elem:
                    cnt += 1
            seen.append(n1 - n2)
        return cnt


from sortedcontainers import SortedList
class Solution:
    def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
        seen = SortedList()
        cnt = 0
        for n1, n2 in zip(nums1, nums2):
            idx = seen.bisect_right(n1-n2+diff)
            cnt += idx
            seen.add(n1 - n2)
        return cnt









from typing import List

class Solution:
    def __init__(self):
        self.count = 0
    
    def check_count(self, nums: List[int], start: int, mid: int, end: int, d: int):
        l = start
        r = mid + 1
        
        while l <= mid and r <= end:
            if nums[l] <= nums[r] + d:
                self.count += (end - r + 1)
                l += 1
            else:
                r += 1
        
        # Sort the array from start to end
        nums[start:end + 1] = sorted(nums[start:end + 1])
    
    def merge_sort(self, nums: List[int], start: int, end: int, d: int):
        if start >= end:
            return
        
        mid = (start + end) // 2
        self.merge_sort(nums, start, mid, d)
        self.merge_sort(nums, mid + 1, end, d)
        self.check_count(nums, start, mid, end, d)
    
    def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
        n = len(nums1)
        c = [nums1[i] - nums2[i] for i in range(n)]
        
        self.merge_sort(c, 0, n - 1, diff)
        return self.count









from typing import List

class Solution:
    def __init__(self):
        self.count = 0
    
    def check_count(self, nums: List[int], start: int, mid: int, end: int, d: int):
        l = start
        r = mid + 1
        
        while l <= mid and r <= end:
            if nums[l] <= nums[r] + d:
                self.count += (end - r + 1)
                l += 1
            else:
                r += 1
    
    def merge(self, nums: List[int], start: int, mid: int, end: int):
        # Standard merge operation without affecting check_count
        merged = []
        l, r = start, mid + 1
        
        while l <= mid and r <= end:
            if nums[l] <= nums[r]:
                merged.append(nums[l])
                l += 1
            else:
                merged.append(nums[r])
                r += 1
        
        while l <= mid:
            merged.append(nums[l])
            l += 1
        while r <= end:
            merged.append(nums[r])
            r += 1
        
        # Copy the merged elements back into the original array nums
        for i in range(len(merged)):
            nums[start + i] = merged[i]
    
    def merge_sort_and_count(self, nums: List[int], start: int, end: int, d: int):
        if start >= end:
            return
        
        mid = (start + end) // 2
        self.merge_sort_and_count(nums, start, mid, d)
        self.merge_sort_and_count(nums, mid + 1, end, d)
        self.check_count(nums, start, mid, end, d)  # Count without sorting
        self.merge(nums, start, mid, end)  # Sort the array after counting
    
    def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
        n = len(nums1)
        C = [nums1[i] - nums2[i] for i in range(n)]
        
        self.merge_sort_and_count(C, 0, n - 1, diff)
        return self.count


34500