diff --git a/DSA/Drishti_590015423/Day_47/Question2.cpp b/DSA/Drishti_590015423/Day_47/Question2.cpp new file mode 100644 index 000000000..5c310f1a7 --- /dev/null +++ b/DSA/Drishti_590015423/Day_47/Question2.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int kthElement(vector& A, vector& B, int k) { + int n = A.size(); + int m = B.size(); + + // Always binary search on the smaller array + if (n > m) + return kthElement(B, A, k); + + int low = max(0, k - m); + int high = min(k, n); + + while (low <= high) { + int cutA = (low + high) / 2; + int cutB = k - cutA; + + int leftA = (cutA == 0) ? INT_MIN : A[cutA - 1]; + int leftB = (cutB == 0) ? INT_MIN : B[cutB - 1]; + + int rightA = (cutA == n) ? INT_MAX : A[cutA]; + int rightB = (cutB == m) ? INT_MAX : B[cutB]; + + if (leftA <= rightB && leftB <= rightA) { + return max(leftA, leftB); + } + else if (leftA > rightB) { + high = cutA - 1; + } + else { + low = cutA + 1; + } + } + + return -1; + } +};