Count Number of Pairs With Absolute Difference K - Problem

You're given an integer array nums and an integer k. Your task is to find how many pairs of indices (i, j) exist where i < j and the absolute difference between nums[i] and nums[j] equals exactly k.

The absolute difference |nums[i] - nums[j]| means:

  • If nums[i] >= nums[j], then |nums[i] - nums[j]| = nums[i] - nums[j]
  • If nums[i] < nums[j], then |nums[i] - nums[j]| = nums[j] - nums[i]

Example: Given nums = [1, 2, 2, 1] and k = 1, the pairs (0,1), (0,2), (1,3), and (2,3) all have absolute difference of 1, so the answer is 4.

Input & Output

example_1.py β€” Basic Case
$ Input: nums = [1,2,2,1], k = 1
β€Ί Output: 4
πŸ’‘ Note: The pairs with absolute difference 1 are: (0,1): |1-2|=1, (0,2): |1-2|=1, (1,3): |2-1|=1, (2,3): |2-1|=1. Total: 4 pairs.
example_2.py β€” No Valid Pairs
$ Input: nums = [1,3], k = 3
β€Ί Output: 0
πŸ’‘ Note: There's only one pair (0,1) and |1-3|=2, which is not equal to k=3. So the answer is 0.
example_3.py β€” All Same Elements
$ Input: nums = [3,2,1,5,1,4], k = 2
β€Ί Output: 4
πŸ’‘ Note: The pairs with absolute difference 2 are: (0,1): |3-1|=2, (0,4): |3-1|=2, (2,5): |1-3|=2, (3,4): |5-3|=2. Wait, let me recalculate: (0,2): |3-1|=2, (0,4): |3-1|=2, (1,5): |2-4|=2, (2,5): |1-3|=2. Actually: (2,5): |1-3|=2, (0,2): |3-1|=2, (0,4): |3-1|=2, (1,5): |2-4|=2. Total: 4 pairs.

Visualization

Tap to expand
πŸ•ΊπŸ’ƒ Dance Partner Matching (Height Difference = k)πŸ‘€Height: 160πŸ‘€Height: 165πŸ‘€Height: 170πŸ‘€Height: 160βœ“ Match!βœ“ Match!Algorithm: One-Pass Hash TableStep-by-step process (k=5):1. Person 160: Check for 155/165 β†’ None found, add 160 to list2. Person 165: Check for 160/170 β†’ Found 160! Count=1, add 1653. Person 170: Check for 165/175 β†’ Found 165! Count=2, add 1704. Person 160: Check for 155/165 β†’ Found 165! Count=3, add 160Total Compatible Pairs: 3
Understanding the Visualization
1
Meet Each Person
As each person arrives, check if any previous arrivals could be their dance partner
2
Check Height Difference
Look for people who are exactly k inches taller or shorter
3
Count Matches
Add the number of compatible partners to our running total
4
Record Arrival
Add the current person to our list for future matching
Key Takeaway
🎯 Key Insight: By checking for compatible partners as each person arrives (rather than comparing everyone with everyone), we achieve optimal O(n) performance while naturally maintaining the ordering constraint.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through array, each hash table operation is O(1) average case

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Hash table can store up to n unique elements in worst case

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ nums.length ≀ 200
  • 1 ≀ nums[i] ≀ 100
  • 0 ≀ k ≀ 99
Asked in
Amazon 25 Google 18 Microsoft 12 Meta 8
42.4K Views
Medium Frequency
~12 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen