fork download
  1. # all the pairs where the sum <=s
  2. def fn(arr,s):
  3. i=0
  4. j=len(arr)-1
  5. cnt=0
  6. while i<j:
  7. pair_sum=arr[i]+arr[j]
  8. if pair_sum>s:
  9. j-=1
  10. else:
  11. cnt+=(j-i)
  12. i+=1
  13.  
  14. return cnt
  15.  
  16.  
  17. def main(arr,l,r):
  18. ans=fn(arr,r)-fn(arr,l)
  19. return ans
  20.  
  21. print(main([0,1,2,3,4,5,6],3,5))
Success #stdin #stdout 0.08s 7916KB
stdin
Standard input is empty
stdout
5