fork(1) download
  1. a = [1, 2, 5, 6, 100, 101, 1000, 1001, 100000, 100002, 100004]
  2. d = {}
  3.  
  4. for i in range(len(a)):
  5. for j in range(i + 1, len(a)):
  6. k = a[j] - a[i]
  7. if k not in d: d[k] = [1, i]
  8. else: d[k][0] += 1
  9.  
  10. # gives us dictionary, key is a difference between elements,
  11. # first element of list is count of pairs having this diff, second element is
  12. # starting point
  13. # {
  14. # 1: [2, 1],
  15. # 2: [1, 2],
  16. # 3: [3, 0],
  17. # 4: [3, 0],
  18. # 5: [1, 3],
  19. # 6: [1, 0],
  20. # 7: [2, 0],
  21. # 8: [1, 1],
  22. # 11: [1, 0]
  23. # }
  24.  
  25. x = max(d.iteritems(), key=lambda x: x[1][0])
  26. diff = x[0]
  27. j = x[1][1]
  28. result = [a[j]]
  29. for i in range(j, len(a)):
  30. if a[i] - a[j] == diff:
  31. result.append(a[i])
  32. j = i
  33.  
  34. print result
Success #stdin #stdout 0.01s 7728KB
stdin
Standard input is empty
stdout
[1, 2]