Given two sorted arrays of size m and n respectively, you are tasked with finding the element that would be at the k’th position of the final sorted array.
Examples:
Input : Array 1 - 2 3 6 7 9
Array 2 - 1 4 8 10
k = 5
Output : 6
Explanation: The final sorted array would be -
1, 2, 3, 4, 6, 7, 8, 9, 10
The 5th element of this array is 6.
Input : Array 1 - 100 112 256 349 770
Array 2 - 72 86 113 119 265 445 892
k = 7
Output : 256
Explanation: Final sorted array is -
72, 86, 100, 112, 113, 119, 256, 265, 349, 445, 770, 892
7th element of this array is 256.
Basic Approach
Since we are given two sorted arrays, we can use the merging technique to get the final merged array. From this, we simply go to the k’th index.
C++
#include <iostream>
using namespace std;
int kth(int arr1[], int arr2[], int n, int m, int k)
{
// array to store the merged sorted array
int sorted1[m + n];
int i = 0, j = 0, d = 0;
while (i < n && j < m) {
// If the element of arr1 is smaller, insert the
// element to the sorted array
if (arr1[i] < arr2[j])
sorted1[d++] = arr1[i++];
else
// If the element of arr2 is smaller, insert the
// element to the sorted array
sorted1[d++] = arr2[j++];
}
// Push the remaining elements of arr1
while (i < n)
sorted1[d++] = arr1[i++];
// Push the remaining elements of arr2
while (j < m)
sorted1[d++] = arr2[j++];
// Return the element at kth position in the merged
// sorted array
return sorted1[k - 1];
}
// Driver Code
int main()
{
int arr1[5] = { 2, 3, 6, 7, 9 };
int arr2[4] = { 1, 4, 8, 10 };
int k = 5;
cout << kth(arr1, arr2, 5, 4, k);
return 0;
}
Java
class Main {
static int kth(int arr1[], int arr2[], int n, int m,
int k)
{
// array to store the merged sorted array
int[] sorted1 = new int[m + n];
int i = 0, j = 0, d = 0;
while (i < n && j < m) {
// If the element of arr1 is smaller, insert the
// element to the sorted array
if (arr1[i] < arr2[j])
sorted1[d++] = arr1[i++];
else
// If the element of arr2 is smaller, insert
// the element to the sorted array
sorted1[d++] = arr2[j++];
}
// Push the remaining elements of arr1
while (i < n)
sorted1[d++] = arr1[i++];
// Push the remaining elements of arr2
while (j < m)
sorted1[d++] = arr2[j++];
// Return the element at kth position in the merged
// sorted array
return sorted1[k - 1];
}
// Driver Code
public static void main(String[] args)
{
int arr1[] = { 2, 3, 6, 7, 9 };
int arr2[] = { 1, 4, 8, 10 };
int k = 5;
System.out.print(kth(arr1, arr2, 5, 4, k));
}
}
/* This code is contributed by Harsh Agarwal */
C#
// C# Program to find kth element
// from two sorted arrays
class GFG {
static int kth(int[] arr1, int[] arr2, int n, int m,
int k)
{
// array to store the merged sorted array
int[] sorted1 = new int[m + n];
int i = 0, j = 0, d = 0;
while (i < n && j < m) {
// If the element of arr1 is smaller, insert the
// element to the sorted array
if (arr1[i] < arr2[j])
sorted1[d++] = arr1[i++];
else
// If the element of arr2 is smaller, insert
// the element to the sorted array
sorted1[d++] = arr2[j++];
}
// Push the remaining elements of arr1
while (i < n)
sorted1[d++] = arr1[i++];
// Push the remaining elements of arr2
while (j < m)
sorted1[d++] = arr2[j++];
// Return the element at kth position in the merged
// sorted array
return sorted1[k - 1];
}
// Driver Code
static void Main()
{
int[] arr1 = { 2, 3, 6, 7, 9 };
int[] arr2 = { 1, 4, 8, 10 };
int k = 5;
System.Console.WriteLine(kth(arr1, arr2, 5, 4, k));
}
}
// This code is contributed by mits
Javascript
// JavaScript Program to find kth element
// from two sorted arrays
function kth(arr1 , arr2 , n , m , k) {
// array to store the merged sorted array
var sorted1 = Array(m + n).fill(0);
var i = 0, j = 0, d = 0;
while (i < n && j < m) {
// If the element of arr1 is smaller, insert the element to the sorted array
if (arr1[i] < arr2[j])
sorted1[d++] = arr1[i++];
else
// If the element of arr2 is smaller, insert the element to the sorted array
sorted1[d++] = arr2[j++];
}
// Push the remaining elements of arr1
while (i < n)
sorted1[d++] = arr1[i++];
// Push the remaining elements of arr2
while (j < m)
sorted1[d++] = arr2[j++];
// Return the element at kth position in the merged sorted array
return sorted1[k - 1];
}
var arr1 = [ 2, 3, 6, 7, 9 ];
var arr2 = [ 1, 4, 8, 10 ];
var k = 5;
console.log(kth(arr1, arr2, 5, 4, k));
PHP
<?php
function kth($arr1, $arr2,
$m, $n, $k)
{
// array to store the merged sorted array
$sorted1[$m + $n] = 0;
$i = 0;
$j = 0;
$d = 0;
while ($i < $m && $j < $n)
{
// If the element of arr1 is smaller, insert the element to the sorted array
if ($arr1[$i] < $arr2[$j])
$sorted1[$d++] = $arr1[$i++];
else
// If the element of arr2 is smaller, insert the element to the sorted array
$sorted1[$d++] = $arr2[$j++];
}
// Push the remaining elements of arr1
while ($i < $m)
$sorted1[$d++] = $arr1[$i++];
// Push the remaining elements of arr2
while ($j < $n)
$sorted1[$d++] = $arr2[$j++];
// Return the element at kth position in the merged sorted array
return $sorted1[$k - 1];
}
// Driver Code
$arr1 = array(2, 3, 6, 7, 9);
$arr2 = array(1, 4, 8, 10);
$k = 5;
echo kth($arr1, $arr2, 5, 4, $k);
?>
Python3
def kth(arr1, arr2, n, m, k):
# array to store the merged sorted array
sorted1 = [0] * (m + n)
i = 0
j = 0
d = 0
while (i < n and j < m):
# If the element of arr1 is smaller, insert the element to the sorted array
if (arr1[i] < arr2[j]):
sorted1[d] = arr1[i]
i += 1
else:
# If the element of arr2 is smaller, insert the element to the sorted array
sorted1[d] = arr2[j]
j += 1
d += 1
# Push the remaining elements of arr1
while (i < n):
sorted1[d] = arr1[i]
d += 1
i += 1
# Push the remaining elements of arr2
while (j < m):
sorted1[d] = arr2[j]
d += 1
j += 1
# Return the element at kth position in the merged sorted array
return sorted1[k - 1]
# Driver code
arr1 = [2, 3, 6, 7, 9]
arr2 = [1, 4, 8, 10]
k = 5
print(kth(arr1, arr2, 5, 4, k))