-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_10.cpp
More file actions
50 lines (47 loc) · 1021 Bytes
/
question_10.cpp
File metadata and controls
50 lines (47 loc) · 1021 Bytes
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
#include <iostream>
using namespace std;
class Solution {
public:
int kthElement(int arr1[], int arr2[], int n, int m, int k)
{
int i = 0;
int j = 0;
int value = -1;
while (i < n && j < m && k) {
if (arr1[i] < arr2[j]) {
if (k == 1) return arr1[i];
i++;
}
else {
if (k == 1) return arr2[j];
j++;
}
k--;
}
while (i < n && k) {
if (k == 1) return arr1[i];
i++;
k--;
}
while (j < m && k) {
if (k == 1) return arr2[j];
j++;
k--;
}
return -1;
}
};
int main()
{
int t = 1;
while (t--) {
int n = 5;
int m = 4;
int k = 5;
int arr1[] = {2, 3, 6, 7, 9};
int arr2[] = {1, 4, 8, 10};
Solution ob;
cout << ob.kthElement(arr1, arr2, n, m, k) << endl;
}
return 0;
}