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


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)


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


5 2
1 5 3 4 2 Output: 3

hide comments
suyashky: 2020-01-29 23:24:45

The wrong Solution got AC!
Realized while solving OPCPIZZA.

shikhar_may7: 2019-10-19 22:20:19

wtf is with this question... cin, cout , sorted vectors result TLE; scanf, printf sorte array gives AC...!!!

emtiaz_001: 2019-09-22 15:20:20

very easy . good for beginner

aryan29: 2019-06-03 18:24:36

using maps will do the job AC in one go

adarsh313: 2019-03-09 16:11:08

little change in OPCPIZZA and AC

kushagra_2: 2019-01-10 14:20:38

that's why i love binary search AC in 0.04 sec:)

pavan_bansal: 2018-12-13 21:15:30

AC in one go!!!

salman3007: 2018-10-03 15:21:36

1. int arr[] is better than vector<int> arr.
2.scanf and printf are better than cin and cout

ameyanator: 2018-09-25 12:03:02

C++STL or binary search take your pick!

prince_jain: 2018-07-26 23:30:15

try to do binary search iteratively, recursive approach will give TLE

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