-
Notifications
You must be signed in to change notification settings - Fork 71
/
k-th-element-of-two-sorted-arrays.cpp
73 lines (63 loc) · 1.42 KB
/
k-th-element-of-two-sorted-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
71
72
73
#include <bits/stdc++.h>
using namespace std;
void fill_array(int *arr, int list_size)
{
int number;
for(int i =0; i< list_size; i++)
{
cin >> number;
arr[i] = number;
}
}
int solve_test()
{
// Read sizes of the two arrays and position of element to find
int size1, size2, kth_element;
cin >> size1 >> size2 >> kth_element;
// Declare the two arrays
int arr1[size1];
int arr2[size2];
// Fill out the two lists of numbers
fill_array(arr1, size1);
fill_array(arr2, size2);
// Sort arrays
sort(arr1, arr1+size1);
sort(arr2, arr2+size2);
// Merge two sorted arrays
int arr3[size2+size1];
int i, j , k;
i = k = j = 0;
while(i < size1 && j < size2)
{
if(arr1[i] < arr2[j])
{
arr3[k] = arr1[i];
i++;
k++;
}
else
{
arr3[k] = arr2[j];
j++;
k++;
}
}
// Check if there are arr1 elements to add (because we reached the end of arr2)
while(i < size1)
arr3[k++] = arr1[i++];
// Check if there are arr2 elements to add (because we reached the end of arr1)
while(j < size2)
arr3[k++] = arr2[j++];
// Return k-th element
return arr3[--kth_element];
}
int main()
{
int t;
cin >> t;
for(int i=0; i<t; i++)
{
cout << solve_test()<<endl;
}
return 0;
}