Given an array of integers and an integer k, write a function that returns the count of unique pairs (x, y) such that the absolute difference between x and y is exactly k. Each pair should be counted only once, and the order of the numbers in the pair does not matter (i.e., (x, y) is considered the same as (y, x)).
For example, given the array: [1, 5, 3, 4, 2]
and k = 2
, the unique pairs with an absolute difference of 2 are:
Your function should return 3
.
Today, on February 11th, we remember historical innovators who transformed the world with creative problem-solving. Use your creativity to solve this problem efficiently!
Your function should have the following signature (or equivalent in your chosen language):
function pairsWithDifference(arr, k) // returns an integer
Happy coding!