fork download
  1. # your code goes here
  2. # Count All ((i,j) pairs such that b[i] - b[j] == k (count of such pairs.) [i<j]
  3.  
  4. def func(arr, K):
  5. freq = {}
  6. result = 0
  7.  
  8. for n in arr:
  9. result += freq.get(n+K, 0)
  10. freq[n] = freq.get(n, 0)+1
  11.  
  12. return result
  13.  
  14. def main():
  15. b = [1, 5, 3, 4, 2]
  16. k = 2
  17. print("Count of pairs:", func(b, k))
  18.  
  19. if __name__ == "__main__":
  20. main()
  21.  
Success #stdin #stdout 0.11s 14152KB
stdin
Standard input is empty
stdout
Count of pairs: 2