Advertisement
Guest User

Untitled

a guest
May 5th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. from bisect import bisect_left
  2.  
  3.  
  4. def countTriangleTripleIndex(a):
  5. a.sort()
  6. n, count = len(a), 0
  7. for i in range(n-2):
  8. j = i+1
  9. k = bisect_left(a, a[i]+a[j])-1
  10. while k < n:
  11. if k > j and a[i] + a[j] <= a[k]:
  12. count += k-j-1
  13. j += 1
  14. else:
  15. k += 1
  16. count += (k-j) * (k-j-1)/2
  17. return count
  18.  
  19. a = [2, 3, 4, 5]
  20. print countTriangleTripleIndex(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement