Description
Solutions
Submission
Counting Pairs 🍓

Given an integer k and a list of integers, count the number of distinct valid pairs of integers (a, b) in the list for which 1 + k = b. Two pairs of integers (a, b) and (c, d) are considered dinstinct if at least one element of (a, b) does not also belong to (c, d). Note that the elements in a pair might be the same element in the array. An instance of this is below where k = 0.

Function Description

Complete the function countPairs in the editor.

countPairs has the following parameter(s):

  1. int numbers[n]: array of integers
  2. int k: target difference

Returns

int: number of valid (a, b) pairs in the numbers array that have a difference of k

Example 1:

Input:  numbers = [1, 1, 1, 2], k = 1
Output: 1
Explanation:
This arr has 3 different valid pairs: (1, 1), (1, 2), and (2, 2,). For k = 1, there is only 1 valid pair which satisfies a + k = b: the pair (a, b) = (1, 2)

Example 2:

Input:  numbers = [1, 2], k = 0
Output: 2
Explanation:
This arr has 3 different valid pairs: (1, 1), (1, 2), and (2, 2,). For k = 0, there is only 2 valid pair which satisfies a + k = b: 1 + 0 = 1 and 2 + 0 = 2.
Constraints:
    n/a
Thumbnail 0
Testcase

Result
Case 1

input:

output: