fork download
  1. import math
  2. def main(arr,l,r):
  3. hm={}
  4. i=0
  5. ans=math.inf
  6. for j in range(len(arr)):
  7.  
  8. ele_J=arr[j]
  9.  
  10. if l<=ele_J and ele_J<=r:
  11. hm[ele_J]=hm.get(ele_J,0)+1
  12.  
  13. while len(hm)==r-l+1:
  14. ans=min(ans,j-i+1)
  15. ele_i=arr[i]
  16. if ele_i in hm:
  17. hm[ele_i]-=1
  18. if hm[ele_i]==0:
  19. del hm[ele_i]
  20. i+=1
  21.  
  22. return -1 if ans==math.inf else ans
  23.  
  24. print(main([2,1,4,3,2,1,1,4],2,4))
Success #stdin #stdout 0.08s 7900KB
stdin
Standard input is empty
stdout
3