Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://practice.geeksforgeeks.org/problems/k-th-element-of-two-sorted-array1317/1
- #User function Template for python3
- class Solution:
- def kthElement(self, A, B, n, m, k):
- if n>m:
- A,B=B,A
- n,m=m,n
- l,r=max(0,k-m),min(n-1,k)
- while True:
- i=(l+r)//2
- j=k-i-2
- #print(i,j)
- aleft=A[i] if i>=0 else float('-inf')
- bleft=B[j] if j>=0 else float('-inf')
- aright=A[i+1] if i+1<n else float('inf')
- bright=B[j+1] if j+1<m else float('inf')
- #print(i,j,aleft,bleft,aright,bright)
- if aleft<=bright and bleft<=aright:
- return max(aleft,bleft)
- elif aleft>bright:
- r=i-1
- else:
- l=i+1
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- def main():
- T = int(input())
- while(T > 0):
- sz = [int(x) for x in input().strip().split()]
- n, m, k = sz[0], sz[1], sz[2]
- a = [int(x) for x in input().strip().split()]
- b = [int(x) for x in input().strip().split()]
- ob = Solution()
- print(ob.kthElement( a, b, n, m, k))
- T -= 1
- if __name__ == "__main__":
- main()
- # } Driver Code Ends
Add Comment
Please, Sign In to add comment