You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to a target value.
For example, given an array of [1, 2, 3, 4] and a target value of 1, we have three values meeting the condition: , , and .
Complete the pairs function below. It must return an integer representing the number of element pairs having the required difference.
pairs has the following parameter(s):
The first line contains two space-separated integers and , the size of and the target value.
The second line contains space-separated integers of the array .
An integer representing the number of pairs of integers whose difference is .
1 5 3 4 2
There are 3 pairs of integers in the set with a difference of 2: [5,3], [4,2] and [3,1] .