Tap here to switch tabs
Problem
Submissions

Problem: K-diff Pairs in an Array

med
30 min
Try to solve the K-diff Pairs in an Array problem.

Statement

Given an integer array nums and an integer k, return the number of unique pairs of values (a, b) such that a and b both appear in nums and |a - b| = k.

A pair is considered unique based on its values, not on the indices where those values occur in nums.

Note: When k = 0, a valid pair requires a value to appear at least twice in nums.

Constraints:

  • 1≤1 \leq nums.length ≤104\leq 10^4

  • −107≤-10^7 \leq nums[i] ≤107\leq 10^7

  • 0≤0 \leq k ≤107\leq 10^7

⋮
Tap here to switch tabs
Problem
Submissions

Problem: K-diff Pairs in an Array

med
30 min
Try to solve the K-diff Pairs in an Array problem.

Statement

Given an integer array nums and an integer k, return the number of unique pairs of values (a, b) such that a and b both appear in nums and |a - b| = k.

A pair is considered unique based on its values, not on the indices where those values occur in nums.

Note: When k = 0, a valid pair requires a value to appear at least twice in nums.

Constraints:

  • 1≤1 \leq nums.length ≤104\leq 10^4

  • −107≤-10^7 \leq nums[i] ≤107\leq 10^7

  • 0≤0 \leq k ≤107\leq 10^7