2006. Count Number of Pairs With Absolute Difference K Easy
1/**
2 * [2006] Count Number of Pairs With Absolute Difference K
3 *
4 * Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
5 * The value of |x| is defined as:
6 *
7 * x if x >= 0.
8 * -x if x < 0.
9 *
10 *
11 * Example 1:
12 *
13 * Input: nums = [1,2,2,1], k = 1
14 * Output: 4
15 * Explanation: The pairs with an absolute difference of 1 are:
16 * - [<u>1</u>,<u>2</u>,2,1]
17 * - [<u>1</u>,2,<u>2</u>,1]
18 * - [1,<u>2</u>,2,<u>1</u>]
19 * - [1,2,<u>2</u>,<u>1</u>]
20 *
21 * Example 2:
22 *
23 * Input: nums = [1,3], k = 3
24 * Output: 0
25 * Explanation: There are no pairs with an absolute difference of 3.
26 *
27 * Example 3:
28 *
29 * Input: nums = [3,2,1,5,4], k = 2
30 * Output: 3
31 * Explanation: The pairs with an absolute difference of 2 are:
32 * - [<u>3</u>,2,<u>1</u>,5,4]
33 * - [<u>3</u>,2,1,<u>5</u>,4]
34 * - [3,<u>2</u>,1,5,<u>4</u>]
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= nums.length <= 200
40 * 1 <= nums[i] <= 100
41 * 1 <= k <= 99
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/
47// discuss: https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn count_k_difference(nums: Vec<i32>, k: i32) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2006() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.