-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK-th element of two Arrays.cpp
70 lines (61 loc) · 1.51 KB
/
K-th element of two Arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int kthElement(vector<int>& a, vector<int>& b, int k) {
// code here
int m = a.size();
int n = b.size();
int left = k;
if(m>n) return kthElement(b,a,k);
int low = max(0,k-n), high = min(k,m);
while(low<=high)
{
int b1 = (low+high)>>1;
int b2 = left - b1;
int l1 = INT_MIN, l2=INT_MIN;
int r1 = INT_MAX, r2=INT_MAX;
if(b1<m) r1 = a[b1];
if(b2<n) r2 = b[b2];
if(b1 - 1 >=0) l1 = a[b1 - 1];
if(b2 - 1 >=0) l2 = b[b2 - 1];
if(l1 <=r2 && l2<=r1){
return max(l1,l2);
}
else if(l1>r2) high = b1-1;
else low = b1+1;
}
return 0;
}
};
//{ Driver Code Starts.
// Driver code
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
int n, m, k;
cin >> k;
cin.ignore();
string input;
int num;
vector<int> a, b;
getline(cin, input);
stringstream s2(input);
while (s2 >> num) {
a.push_back(num);
}
getline(cin, input);
stringstream s3(input);
while (s3 >> num) {
b.push_back(num);
}
Solution ob;
cout << ob.kthElement(a, b, k) << endl << "~\n";
}
return 0;
}
// } Driver Code Ends