Skip to content

Instantly share code, notes, and snippets.

@mylons
Created March 16, 2012 16:13
Show Gist options
  • Save mylons/2050786 to your computer and use it in GitHub Desktop.
Save mylons/2050786 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import itertools
import collections
def Kdiff(nums, k):
return sum(1 for x, y in itertools.combinations(nums, 2) if (x-y) == k)
def ErrorAndExit():
print "0"
sys.exit()
def main():
n = k = 0
try:
n, k = raw_input().rstrip().split()
n = int(n)
k = int(k)
if n > (10 ** 5) or n <= 0:#doesn't pass requirement on n
ErrorAndExit()
elif k >= (1 * (10**9)) or k <0:#doesn't pass requirement on k
ErrorAndExit()
except: #no k or just malformed input. print 0 and exit
ErrorAndExit()
nums = collections.defaultdict(lambda: 1)
def clg():
for num in raw_input().rstrip().split():
nums[ num ] += 1
return nums
print Kdiff( nums.iterkeys(), k )
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment