Advertisement
Iam_Sandeep

Untitled

Aug 28th, 2022
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. from functools import lru_cache
  2. def find(a,k):
  3.     def ispossible(m):
  4.         @lru_cache()
  5.         def find(i,j):
  6.             n=len(a)
  7.             if i==n:return False
  8.             if j==k:return True
  9.             else:
  10.                 if m>=a[i]:
  11.                     incl = find(i+1,j+1)
  12.                     excl = find(i+1,j)
  13.                     return incl or excl
  14.                 else:
  15.                     excl =find(i+1,j)
  16.                     return excl
  17.         return find(0,0)
  18.        
  19.        
  20.     l,r=min(a),max(a)
  21.     ans = None
  22.     while l<=r:
  23.         m = (l+r)>>1
  24.         if ispossible(m):
  25.             ans = m
  26.             r=m-1
  27.         else:
  28.             l=m+1
  29.     return ans
  30. print(find([2,4,6,8,10],2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement