-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK-th Element of Two Sorted Arrays
58 lines (45 loc) · 1.55 KB
/
K-th Element of Two Sorted Arrays
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
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0)
{
StringTokenizer stt = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stt.nextToken());
int m = Integer.parseInt(stt.nextToken());
int k = Integer.parseInt(stt.nextToken());
int a[] = new int[(int)(n)];
int b[] = new int[(int)(m)];
String inputLine[] = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(inputLine[i]);
}
String inputLine1[] = br.readLine().trim().split(" ");
for (int i = 0; i < m; i++) {
b[i] = Integer.parseInt(inputLine1[i]);
}
Solution obj = new Solution();
System.out.println(obj.kthElement( a, b, n, m, k));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution {
public long kthElement( int arr1[], int arr2[], int n, int m, int k)
{
int arr[]=new int[n+m];
for(int i=0;i<n;i++)
arr[i]=arr1[i];
for(int i=0;i<m;i++)
arr[n+i]=arr2[i];
Arrays.sort(arr);
return arr[k-1];
}
}