-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kth_Smallest_in_union of two_sorted_array.cpp
69 lines (67 loc) · 1.28 KB
/
Kth_Smallest_in_union of two_sorted_array.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
#include<stdio.h>
int* merge(int a[],int b[],int n, int* temp)
{
int i=0,j=0,k=0,l=0;
//static int temp[2*n];
while(i<n & j<n)
{
if(a[i]<b[j])
{
temp[k] = a[i];
i++;
}
else
{
temp[k] = b[j];
j++;
}
k++;
}
if (i==n)
{
while(j<n)
{
temp[k] = b[j];
j++;
k++;
}
}
if (j==n)
{
while(i<n)
{
temp[k] = a[i];
i++;
k++;
}
}
return temp;
}
int search_k_smallest(int a[],int b[], int n, int k)
{
int i=0;
int temp[2*n];
int* sorted_array = merge(a,b,n, temp);
printf("The combined Sorted Array is: \n");
for (i=0;i<2*n;i++)
printf("%d ",*(sorted_array+i));
return *(sorted_array+(k-1));
}
int main()
{
int n,k;
printf("Enter size of the array: ");
scanf("%d",&n);
int a[n],b[n];
printf("Enter the elements in sorted order for array 1: ");
for(int i=0;i<n;i++)
scanf("%d", &a[i]);
printf("Enter the elements in sorted order for array 2: ");
for(int i=0;i<n;i++)
scanf("%d", &b[i]);
printf("Enter the kth smallest element which you want to find (Value of K): ");
scanf("%d",&k);
int element = search_k_smallest(a,b,n,k);
printf("\nThe %dth Smallest Element: %d",k,element);
return 0;
}