You are given two sorted arrays A, B and another integer D. Find the number of pairs (i, j) such that |A[i] - B[j]| <= D.
Follow up: You are given three sorted arrays A, B, C, and another integer D. Find the number of tuples (i, j, k) such that |A[i] - B[j]| <= D, |A[i] - C[k]| <= D, and |B[j] - C[k]| <= D.
这道题考察的是有序数组上的双指针或二分统计。第一问要求统计数组 A 和 B 中满足绝对差不超过 D 的元素对数量,可以利用数组已排序这一条件,用滑动窗口 / 双指针在线性时间内完成,避免暴力枚举。进阶部分把问题扩展到三个有序数组,要求统计三元组是否两两都满足阈值限制;这类题通常需要继续利用排序性质,把每个数组中的可行区间转化为窗口计数,或对中间数组逐点扩展统计左右边界,从而在可接受的复杂度内求解。