Pairs

Problem Statement 

Given an array of integers and a target value, determine the number of pairs of array elements that have a difference equal to the target value.

Example
k=1
arr = [1,2,3,4]
There are three values that differ by k=1: 2-1 = 1, 3-2= 1 and 4-3=1. Return 3.

Function Description
Complete the pairs function below.
pairs has the following parameter(s):
int k: an integer, the target difference
int arr[n]: an array of integers

Returns
int: the number of pairs that satisfy the criterion

Input Format
The first line contains two space-separated integers n and k, the size of arr and the target value.
The second line contains space-separated integers of the array arr.

Sample Input
STDIN Function
—– ——–
5 2 arr[] size n = 5, k =2
1 5 3 4 2 arr = [1, 5, 3, 4, 2]

Sample Output – 3

Explanation
There are 3 pairs of integers in the set with a difference of 2: [5,3], [4,2] and [3,1]. .

Step 1 : – Template Implementation

There are two types of two pointer templates.

1). Opposite direction two pointer template

2). Same direction two pointer template

Based on our analysis of the problem statement, we are using the same direction pointers template and as per the approach, we first sort the list using Collections.sort() to ensure that the smallest differences appear between consecutive elements.

 

Java
class Result {
  public static int pairs(int k, List<Integer> arr) { 
        int count = 0;
        
        Collections.sort(arr);
        int left = 0;
        int right = 1;
        while (right < arr.size()) {
            //code

            if (param1 == param2) {
                //code
                left++;
                right++;
            } else if (param1< param2) {
                right++;
            } else {
                left++;
            }
        }
        return count;
    }
}

Step 2 : – I/O parameters.

Input Parameters :- To calculate the value of param1, we find the difference between the elements at the right and left indices, i.e., arr.get(right) – arr.get(left).
This difference becomes our param1, and we replace param2 with the target value (k) to compare both and determine whether to move the left or right pointer.
Output parameter :- We need to return the count of all valid pairs from the list whose difference equals the given target value k. Each time we find such a pair, we increment the count.

 

Java
class Result {
  public static int pairs(int k, List<Integer> arr) { 
        int count = 0;
        
        Collections.sort(arr);
        int left = 0;
        int right = 1;
        while (right < arr.size()) {
            int diff = arr.get(right) - arr.get(left);

            if (diff == k) {
                count++;
                left++;
                right++;
            } else if (diff < k) {
                right++;
            } else {
                left++;
            }
        }
        return count;
    }
}

Final Code : – 

 

Java
class Result {
  public static int pairs(int k, List<Integer> arr) { 
        int count = 0;
        
        Collections.sort(arr);
        int left = 0;
        int right = 1;
        while (right < arr.size()) {
            int diff = arr.get(right) - arr.get(left);

            if (diff == k) {
                count++;
                left++;
                right++;
            } else if (diff < k) {
                right++;
            } else {
                left++;
            }
        }
        return count;
    }
}
Scroll to Top