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
Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →