Back to course home
0% completed
Solution: Count Number of Pairs With Absolute Difference K
Problem Statement
Given an array of integers nums
and a positive integer k
, return the number of pairs (i, j)
where i < j
and the absolute difference
of nums[i]
and nums[j]
should be equal to k
. In other words, |nums[i] - nums[j]| == k
.
Examples
-
Example 1:
- Input: nums =
[1, 3, 5, 7]
, K = 2 - Expected Output: 3
- Justification: The pairs with an absolute difference of 2 are (1, 3), (3, 5), and (5, 7).
- Input: nums =
-
Example 2:
- Input: nums =
[4, 4, 4, 4]
, K = 1 - Expected Output: 0
- Input: nums =
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible