Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

K-Difference

Sharing is caring!

Problem Statement
In this challenge, you will be given an array of integers, each unique within the array, and an integer representing a target difference. Determine the number of pairs of elements in the array that have a difference equal to the target difference.

For example, consider the array [1, 3, 5] and a target difference 2. There are two pairs: [1,3] and [3,5], that have the target difference.

Function Description
Complete the function kDifference in the editor below. The function must return an integer count of the number of pairs within a having a difference of k.

kDifference has the following parameter(s):

  • a[a[0],…a[n-1]]: array of integers to test
  • k: the target difference

Sample Input 0

Sample Output 0

Explanation 0
Count the number of pairs in a = [1, 5, 3, 4, 2] whose difference is k = 2. The following three pairs meet the criterion: (1, 3), (5, 3), and (4, 2).

Solution