2364. Count Number of Bad Pairs Medium
1/**
2 * [2364] Count Number of Bad Pairs
3 *
4 * You are given a 0-indexed integer array nums. A pair of indices (i, j) is a bad pair if i < j and j - i != nums[j] - nums[i].
5 * Return the total number of bad pairs in nums.
6 *
7 * Example 1:
8 *
9 * Input: nums = [4,1,3,3]
10 * Output: 5
11 * Explanation: The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
12 * The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
13 * The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
14 * The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
15 * The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
16 * There are a total of 5 bad pairs, so we return 5.
17 *
18 * Example 2:
19 *
20 * Input: nums = [1,2,3,4,5]
21 * Output: 0
22 * Explanation: There are no bad pairs.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= nums.length <= 10^5
28 * 1 <= nums[i] <= 10^9
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/count-number-of-bad-pairs/
34// discuss: https://leetcode.com/problems/count-number-of-bad-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn count_bad_pairs(nums: Vec<i32>) -> i64 {
40
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_2364() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.