Problem Statement
Given N integers, count the number of pairs of integers whose difference is K .
Input Format
The first line containsN and K .
The second line containsN numbers of the set. All the N numbers are unique.
Output Format
An integer that tells the number of pairs of integers whose difference isK .
Constraints:
N≤10 5
0<K<10 9
Each integer will be greater than0 and at least K smaller than 2 31 −1 .
Sample Input #00:
Input Format
The first line contains
The second line contains
Output Format
An integer that tells the number of pairs of integers whose difference is
Constraints:
Each integer will be greater than
Sample Input #00:
5 2
1 5 3 4 2
Sample Output #00: 3
Sample Input #01:10 1
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793
Sample Output #01:0
MY SOLUTION:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n,k,count=0;
int i;
n=s.nextInt();
k=s.nextInt();
long[] a=new long[n];
for(i=0;i<n;i++)
{
a[i]=s.nextLong();
}
Arrays.sort(a);
int length=n-1;
while(length!=0)
{
for(i=length-1;i>=0;i--)
{
if(Math.abs(a[length]-a[i])==k)
{
count++;
break;
}
}
length--;
}
System.out.println(count);
}
}