-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkth_ele_twoSortedArray.cpp
More file actions
64 lines (55 loc) · 1004 Bytes
/
Copy pathkth_ele_twoSortedArray.cpp
File metadata and controls
64 lines (55 loc) · 1004 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int kthEle(int *arr, int *brr, int n, int m, int z)
{
int nm = n+m;
vector<int> temp(nm);
int i=0, j=0, k=0;
while(i < n && j < m)
{
if(arr[i] < brr[j])
{
temp[k]=arr[i];
i++;
}
else
{
temp[k]=brr[j];
j++;
}
k++;
}
// leftovers in arr
while(i < n)
{
temp[k]=arr[i];
i++;
k++;
}
//leftovers in brr
while(j < m)
{
temp[k]=brr[j];
j++;
k++;
}
for(i=0; i<temp.size(); i++)
{
if(i+1 == z)
return temp[i];
}
}
int main()
{
cout<<"\n";
int arr[] = {2,3,6,7,9};
int brr[] = {1,4,8,10};
int n = sizeof(arr)/sizeof(arr[0]);
int m = sizeof(brr)/sizeof(brr[0]);
int z = 5;
int res;
res = kthEle(arr, brr, n, m, z);
cout<<res<<"\n";
return 0;
}