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

        n = len(nums1)
        
        # Create the difference array v
        v = [nums1[i] - nums2[i] for i in range(n)]
        
        # Helper function to merge and count valid pairs
        def merge_and_count(left, right):
            # Count pairs that satisfy v[i] <= v[j] + diff
            count = 0
            j = 0
            
            # For each element in right, find how many left elements are valid
            for r in right:
                while j < len(left) and left[j] <= r + diff:
                    j += 1
                count += j
            
            # Return the merge sorted array and the count of valid pairs
            return sorted(left + right), count

        # Merge sort function to sort and count
        def merge_sort_and_count(v):
            # Base case: if array has 1 or 0 elements, it's already sorted
            if len(v) <= 1:
                return v, 0
            
            # Split the array into two halves
            mid = len(v) // 2
            left, left_count = merge_sort_and_count(v[:mid])
            right, right_count = merge_sort_and_count(v[mid:])
            
            # Merge two halves and count the valid pairs
            merged, merge_count = merge_and_count(left, right)
            
            # Total count is the sum of counts from both halves and the merge step
            return merged, left_count + right_count + merge_count

        # Perform merge sort and count pairs
        _, result = merge_sort_and_count(v)
        
        return result
35400