HACKRNDM - Hacking the random number generator

You recently wrote a random number generator code for a web application and now you notice that some cracker has cracked it. It now gives numbers at a difference of some given value k more predominantly. You being a hacker decide to write a code that will take in n numbers as input and a value k and find the total number of pairs of numbers whose absolute difference is equal to k, in order to assist you in your random number generator testing.

NOTE: All values fit in the range of a signed integer, n, k>=1

Input

1st line contains n & k.
2nd line contains n numbers of the set. All the n numbers are assured to be distinct.

(Edited: n <= 10^5)

Output

One integer saying the no of pairs of numbers that have a diff k.

Example

Input:
5 2
1 5 3 4 2 Output: 3

Added by:vijay
Date:2011-10-15
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own Problem

hide comments
2015-05-08 19:56:25 :.Mohib.:
Merge sort + binary search ==> AC:)
2015-03-24 18:27:51 Daljeet Singh
O(nlogn) works
2015-03-04 21:07:44 cracked
numbers in the second line are separated by '\n' instead of ' '
2015-02-17 13:47:57 kailash
O(n) --> TLE O(nlogn)--> AC
@Rahul Jain i used long long int

Last edit: 2015-02-17 13:48:34
2015-02-02 17:48:57 Rahul Jain
what is the range of the elements. I am using -32768 to 32767, but that gives me wrong answer
2015-01-14 17:32:34 MKM
Pretty easy!
2015-01-09 08:33:01 eightnoteight
why! why! why! why am i getting wrong answer for the same algo in python but getting accepted in C++
i have also submitted a solution in python which doesn't print a new line to the output, but still i'm getting wrong answer

Last edit: 2015-01-09 08:34:34
2014-12-25 17:32:31 swordfish12
misleading problem name :( ... very simple problem
2014-11-25 06:29:17 mayank
Simple :)
2014-10-07 07:29:51 deCodeIt
Segmentation fault at running (2).. =="

Now Solved :) ... a single pointer was giving seg fault.. hahahaaaha .. got 4 SIGSEV error for that

Last edit: 2014-10-07 13:29:44
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.