2426. Number of Pairs Satisfying Inequality Hard
1/**
2 * [2426] Number of Pairs Satisfying Inequality
3 *
4 * You are given two 0-indexed integer arrays nums1 and nums2, each of size n, and an integer diff. Find the number of pairs (i, j) such that:
5 *
6 * 0 <= i < j <= n - 1 and
7 * nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff.
8 *
9 * Return the number of pairs that satisfy the conditions.
10 *
11 * Example 1:
12 *
13 * Input: nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
14 * Output: 3
15 * Explanation:
16 * There are 3 pairs that satisfy the conditions:
17 * 1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions.
18 * 2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions.
19 * 3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions.
20 * Therefore, we return 3.
21 *
22 * Example 2:
23 *
24 * Input: nums1 = [3,-1], nums2 = [-2,2], diff = -1
25 * Output: 0
26 * Explanation:
27 * Since there does not exist any pair that satisfies the conditions, we return 0.
28 *
29 *
30 * Constraints:
31 *
32 * n == nums1.length == nums2.length
33 * 2 <= n <= 10^5
34 * -10^4 <= nums1[i], nums2[i] <= 10^4
35 * -10^4 <= diff <= 10^4
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/number-of-pairs-satisfying-inequality/
41// discuss: https://leetcode.com/problems/number-of-pairs-satisfying-inequality/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn number_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, diff: i32) -> i64 {
47
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_2426() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.