# 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.

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 n space-separated integers of the array arr .

Constraints

2   <=   n   <=   10^5
0   <=   k   <=   10^9
0   <=   arr[ i ]  <=  2^31 -1
each integer arr[ i ]  will be unique

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```

### Solution :

```                            ```Solution in C :

In   C++  :

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */

int pairs(vector <int> a,int k) {
int ans = 0;
set<long long> s;
for(int i = 0; i < a.size(); i++) s.insert(a[i]);
for(int i = 0; i < a.size(); i++){
long long b = a[i] - k;
if(s.count(b)) ans ++;
}
return ans;
}

/* Tail starts here */
int main() {
int res;

int _a_size,_k;
cin >> _a_size>>_k;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
vector<int> _a;
int _a_item;
for(int _a_i=0; _a_i<_a_size; _a_i++) {
cin >> _a_item;
_a.push_back(_a_item);
}

res = pairs(_a,_k);
cout << res;

return 0;
}

In  Java  :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
static int pairs(int[] a,int k) {
Arrays.sort(a);
int N = a.length;
int count = 0;
for (int i = 0; i < N - 1; i++)
{
int j = i + 1;
while((j < N) && (a[j++] - a[i]) < k);
j--;
while((j < N) && (a[j++] - a[i]) == k)
count++;
}

return count;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res;

String n = in.nextLine();
String[] n_split = n.split(" ");

int _a_size = Integer.parseInt(n_split[0]);
int _k = Integer.parseInt(n_split[1]);

int[] _a = new int[_a_size];
int _a_item;
String next = in.nextLine();
String[] next_split = next.split(" ");

for(int _a_i = 0; _a_i < _a_size; _a_i++) {
_a_item = Integer.parseInt(next_split[_a_i]);
_a[_a_i] = _a_item;
}

res = pairs(_a,_k);
System.out.println(res);
}
}

In   C :

#include<stdio.h>
int get_num()
{
int num=0;
char c=getchar_unlocked();
while(!(c>='0' && c<='9'))
c=getchar_unlocked();
while(c>='0' && c<='9')
{
num=(num<<3)+(num<<1)+c-'0';
c=getchar_unlocked();
}
return num;
}
void quicksort(int x[],int first,int last)
{
int pivot,j,temp,i;

if(first<last)
{
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}
int main()
{
int n=get_num();
int k=get_num();
int a[100000]={0};
int i=0;
while(i<n)
a[i++]=get_num();
quicksort(a,0,n-1);
int temp=0,count=0,flag=0;
for(i=0;i<n-1;i++)
{
int j=i+1;
temp=0;
for(;j<n;j++)
{
if(a[j]-a[i]==k)
count++;
else if(a[j]-a[i]>k)
break;
}
}
printf("%d\n",count);
return 0;
}

In  Python3 :

def main():
N, K = (int(x) for x in sys.stdin.readline().split())
A = [int(x) for x in sys.stdin.readline().split()]
setA = set(A)
count = 0
for x in A:
if (x-K) in setA:
count = count +1
print (count)

if __name__ == '__main__':
import sys
main()```
```

## View More Similar Problems

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

## Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle