Find kth element in two sorted arrays
Here given code implementation process.
// C Program
// Find kth element in two sorted arrays
#include <stdio.h>
// Find kth sorted element which is exists in combination of given two arrays
void findElement(int arr1[], int arr2[], int n1, int n2, int k)
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
printf(" %d-th element not exist \n", k);
}
else
{
// Loop controlling variable
int a = 0;
int b = 0;
// Define some useful resultant variables
int counter = 0;
int result = 0;
int flag = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter++;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
// Display result
printf(" %d-th element is : %d\n", k, result);
}
}
int main(int argc, char const *argv[])
{
// Given sorted arrays of integer elements
int arr1[] = {
3 , 6 , 8 , 9 , 10 , 14 , 21
};
int arr2[] = {
-1 , 4 , 5 , 12
};
// Get the size
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
/*
// When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
findElement(arr1, arr2, n1, n2, 1);
return 0;
}
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
/*
Java Program for
Find kth element in two sorted arrays
*/
class Search
{
// Find kth sorted element which is exists in combination of given two arrays
public void findElement(int[] arr1, int[] arr2, int n1, int n2, int k)
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
System.out.print( k + "-th element not exist \n");
}
else
{
// Loop controlling variable
int a = 0;
int b = 0;
// Define some useful resultant variables
int counter = 0;
int result = 0;
int flag = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter++;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
// Display result
System.out.print(" " + k + "-th element is : " + result + "\n");
}
}
public static void main(String[] args)
{
Search task = new Search();
// Given sorted arrays of integer elements
int[] arr1 = {
3 , 6 , 8 , 9 , 10 , 14 , 21
};
int[] arr2 = {
-1 , 4 , 5 , 12
};
// Get the size
int n1 = arr1.length;
int n2 = arr2.length;
/*
// When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
}
}
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Find kth element in two sorted arrays
*/
class Search
{
public:
// Find kth sorted element which is exists in combination of given two arrays
void findElement(int arr1[], int arr2[], int n1, int n2, int k)
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
cout << k << "-th element not exist \n";
}
else
{
// Loop controlling variable
int a = 0;
int b = 0;
// Define some useful resultant variables
int counter = 0;
int result = 0;
int flag = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter++;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
// Display result
cout << " " << k << "-th element is : " << result << "\n";
}
}
};
int main()
{
Search task = Search();
// Given sorted arrays of integer elements
int arr1[] = {
3 , 6 , 8 , 9 , 10 , 14 , 21
};
int arr2[] = {
-1 , 4 , 5 , 12
};
// Get the size
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
return 0;
}
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
// Include namespace system
using System;
/*
C# Program for
Find kth element in two sorted arrays
*/
public class Search
{
// Find kth sorted element which is exists in combination of given two arrays
public void findElement(int[] arr1, int[] arr2, int n1, int n2, int k)
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
Console.Write(k + "-th element not exist \n");
}
else
{
// Loop controlling variable
int a = 0;
int b = 0;
// Define some useful resultant variables
int counter = 0;
int result = 0;
int flag = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter++;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
// Display result
Console.Write(" " + k + "-th element is : " + result + "\n");
}
}
public static void Main(String[] args)
{
Search task = new Search();
// Given sorted arrays of integer elements
int[] arr1 = {
3 , 6 , 8 , 9 , 10 , 14 , 21
};
int[] arr2 = {
-1 , 4 , 5 , 12
};
// Get the size
int n1 = arr1.Length;
int n2 = arr2.Length;
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
}
}
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
<?php
/*
Php Program for
Find kth element in two sorted arrays
*/
class Search
{
// Find kth sorted element which is exists in combination of given two arrays
public function findElement( $arr1, $arr2, $n1, $n2, $k)
{
if ($k <= 0 || $k > $n1 + $n2)
{
// When given element are not valid (outside of range)
echo $k ."-th element not exist \n";
}
else
{
// Loop controlling variable
$a = 0;
$b = 0;
// Define some useful resultant variables
$counter = 0;
$result = 0;
$flag = 0;
// Find kth element
while ($flag == 0 && ($a < $n1 || $b < $n2))
{
// element counter
$counter++;
if ($a < $n1 && $b < $n2)
{
// When exists both array elements
if ($arr1[$a] < $arr2[$b])
{
if ($counter == $k)
{
// When we get result
$result = $arr1[$a];
$flag = 1;
}
$a++;
}
else
{
if ($counter == $k)
{
// When we get result
$result = $arr2[$b];
$flag = 1;
}
$b++;
}
}
else if ($a < $n1)
{
if ($counter == $k)
{
// When we get result
$result = $arr1[$a];
$flag = 1;
}
$a++;
}
else
{
if ($counter == $k)
{
// When we get result
$result = $arr2[$b];
$flag = 1;
}
$b++;
}
}
// Display result
echo " ". $k ."-th element is : ". $result ."\n";
}
}
}
function main()
{
$task = new Search();
// Given sorted arrays of integer elements
$arr1 = array(3, 6, 8, 9, 10, 14, 21);
$arr2 = array(-1, 4, 5, 12);
// Get the size
$n1 = count($arr1);
$n2 = count($arr2);
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
$task->findElement($arr1, $arr2, $n1, $n2, 4);
// Find 7th element
$task->findElement($arr1, $arr2, $n1, $n2, 7);
// Find 10th element
$task->findElement($arr1, $arr2, $n1, $n2, 10);
// Find 1st element
$task->findElement($arr1, $arr2, $n1, $n2, 1);
}
main();
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
/*
Node Js Program for
Find kth element in two sorted arrays
*/
class Search
{
// Find kth sorted element which is exists in combination of given two arrays
findElement(arr1, arr2, n1, n2, k)
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
process.stdout.write(k + "-th element not exist \n");
}
else
{
// Loop controlling variable
var a = 0;
var b = 0;
// Define some useful resultant variables
var counter = 0;
var result = 0;
var flag = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter++;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a++;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b++;
}
}
// Display result
process.stdout.write(" " + k + "-th element is : " + result + "\n");
}
}
}
function main()
{
var task = new Search();
// Given sorted arrays of integer elements
var arr1 = [3, 6, 8, 9, 10, 14, 21];
var arr2 = [-1, 4, 5, 12];
// Get the size
var n1 = arr1.length;
var n2 = arr2.length;
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
}
main();
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
# Python 3 Program for
# Find kth element in two sorted arrays
class Search :
# Find kth sorted element which is exists in combination of given two arrays
def findElement(self, arr1, arr2, n1, n2, k) :
if (k <= 0 or k > n1 + n2) :
# When given element are not valid (outside of range)
print(k ,"-th element not exist ")
else :
# Loop controlling variable
a = 0
b = 0
# Define some useful resultant variables
counter = 0
result = 0
flag = 0
# Find kth element
while (flag == 0 and(a < n1 or b < n2)) :
# element counter
counter += 1
if (a < n1 and b < n2) :
# When exists both array elements
if (arr1[a] < arr2[b]) :
if (counter == k) :
# When we get result
result = arr1[a]
flag = 1
a += 1
else :
if (counter == k) :
# When we get result
result = arr2[b]
flag = 1
b += 1
elif(a < n1) :
if (counter == k) :
# When we get result
result = arr1[a]
flag = 1
a += 1
else :
if (counter == k) :
# When we get result
result = arr2[b]
flag = 1
b += 1
# Display result
print(" ", k ,"-th element is : ", result )
def main() :
task = Search()
# Given sorted arrays of integer elements
arr1 = [3, 6, 8, 9, 10, 14, 21]
arr2 = [-1, 4, 5, 12]
# Get the size
n1 = len(arr1)
n2 = len(arr2)
#
# When combining the elements of sorted array
# [-1,3,4,5,6,8,9,10,12,14,21]
# Find 4th element
task.findElement(arr1, arr2, n1, n2, 4)
# Find 7th element
task.findElement(arr1, arr2, n1, n2, 7)
# Find 10th element
task.findElement(arr1, arr2, n1, n2, 10)
# Find 1st element
task.findElement(arr1, arr2, n1, n2, 1)
if __name__ == "__main__": main()
Output
4 -th element is : 5
7 -th element is : 9
10 -th element is : 14
1 -th element is : -1
# Ruby Program for
# Find kth element in two sorted arrays
class Search
# Find kth sorted element which is exists in combination of given two arrays
def findElement(arr1, arr2, n1, n2, k)
if (k <= 0 || k > n1 + n2)
# When given element are not valid (outside of range)
print(k ,"-th element not exist \n")
else
# Loop controlling variable
a = 0
b = 0
# Define some useful resultant variables
counter = 0
result = 0
flag = 0
# Find kth element
while (flag == 0 && (a < n1 || b < n2))
# element counter
counter += 1
if (a < n1 && b < n2)
# When exists both array elements
if (arr1[a] < arr2[b])
if (counter == k)
# When we get result
result = arr1[a]
flag = 1
end
a += 1
else
if (counter == k)
# When we get result
result = arr2[b]
flag = 1
end
b += 1
end
elsif(a < n1)
if (counter == k)
# When we get result
result = arr1[a]
flag = 1
end
a += 1
else
if (counter == k)
# When we get result
result = arr2[b]
flag = 1
end
b += 1
end
end
# Display result
print(" ", k ,"-th element is : ", result ,"\n")
end
end
end
def main()
task = Search.new()
# Given sorted arrays of integer elements
arr1 = [3, 6, 8, 9, 10, 14, 21]
arr2 = [-1, 4, 5, 12]
# Get the size
n1 = arr1.length
n2 = arr2.length
#
# When combining the elements of sorted array
# [-1,3,4,5,6,8,9,10,12,14,21]
# Find 4th element
task.findElement(arr1, arr2, n1, n2, 4)
# Find 7th element
task.findElement(arr1, arr2, n1, n2, 7)
# Find 10th element
task.findElement(arr1, arr2, n1, n2, 10)
# Find 1st element
task.findElement(arr1, arr2, n1, n2, 1)
end
main()
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
/*
Scala Program for
Find kth element in two sorted arrays
*/
class Search
{
// Find kth sorted element which is exists in combination of given two arrays
def findElement(arr1: Array[Int], arr2: Array[Int], n1: Int, n2: Int, k: Int): Unit = {
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
print(" "+k + "-th element not exist \n");
}
else
{
// Loop controlling variable
var a: Int = 0;
var b: Int = 0;
// Define some useful resultant variables
var counter: Int = 0;
var result: Int = 0;
var flag: Int = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter += 1;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1(a) < arr2(b))
{
if (counter == k)
{
// When we get result
result = arr1(a);
flag = 1;
}
a += 1;
}
else
{
if (counter == k)
{
// When we get result
result = arr2(b);
flag = 1;
}
b += 1;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1(a);
flag = 1;
}
a += 1;
}
else
{
if (counter == k)
{
// When we get result
result = arr2(b);
flag = 1;
}
b += 1;
}
}
// Display result
print(" " + k + "-th element is : " + result + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Search = new Search();
// Given sorted arrays of integer elements
var arr1: Array[Int] = Array(3, 6, 8, 9, 10, 14, 21);
var arr2: Array[Int] = Array(-1, 4, 5, 12);
// Get the size
var n1: Int = arr1.length;
var n2: Int = arr2.length;
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
}
}
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
/*
Swift 4 Program for
Find kth element in two sorted arrays
*/
class Search
{
// Find kth sorted element which is exists in combination of given two arrays
func findElement(_ arr1: [Int], _ arr2: [Int], _ n1: Int, _ n2: Int, _ k: Int)
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
print(k ,"-th element not exist ");
}
else
{
// Loop controlling variable
var a: Int = 0;
var b: Int = 0;
// Define some useful resultant variables
var counter: Int = 0;
var result: Int = 0;
var flag: Int = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter += 1;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a += 1;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b += 1;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a += 1;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b += 1;
}
}
// Display result
print(" ", k ,"-th element is : ", result );
}
}
}
func main()
{
let task: Search = Search();
// Given sorted arrays of integer elements
let arr1: [Int] = [3, 6, 8, 9, 10, 14, 21];
let arr2: [Int] = [-1, 4, 5, 12];
// Get the size
let n1: Int = arr1.count;
let n2: Int = arr2.count;
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
}
main();
Output
4 -th element is : 5
7 -th element is : 9
10 -th element is : 14
1 -th element is : -1
/*
Kotlin Program for
Find kth element in two sorted arrays
*/
class Search
{
// Find kth sorted element which is exists in combination of given two arrays
fun findElement(arr1: Array < Int > , arr2: Array<Int> , n1: Int, n2: Int, k: Int): Unit
{
if (k <= 0 || k > n1 + n2)
{
// When given element are not valid (outside of range)
print(" "+ k + "-th element not exist \n");
}
else
{
// Loop controlling variable
var a: Int = 0;
var b: Int = 0;
// Define some useful resultant variables
var counter: Int = 0;
var result: Int = 0;
var flag: Int = 0;
// Find kth element
while (flag == 0 && (a < n1 || b < n2))
{
// element counter
counter += 1;
if (a < n1 && b < n2)
{
// When exists both array elements
if (arr1[a] < arr2[b])
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a += 1;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b += 1;
}
}
else if (a < n1)
{
if (counter == k)
{
// When we get result
result = arr1[a];
flag = 1;
}
a += 1;
}
else
{
if (counter == k)
{
// When we get result
result = arr2[b];
flag = 1;
}
b += 1;
}
}
// Display result
print(" " + k + "-th element is : " + result + "\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Search = Search();
// Given sorted arrays of integer elements
var arr1: Array <Int> = arrayOf(3, 6, 8, 9, 10, 14, 21);
var arr2: Array <Int> = arrayOf(-1, 4, 5, 12);
// Get the size
var n1: Int = arr1.count();
var n2: Int = arr2.count();
/*
When combining the elements of sorted array
[-1,3,4,5,6,8,9,10,12,14,21]
*/
// Find 4th element
task.findElement(arr1, arr2, n1, n2, 4);
// Find 7th element
task.findElement(arr1, arr2, n1, n2, 7);
// Find 10th element
task.findElement(arr1, arr2, n1, n2, 10);
// Find 1st element
task.findElement(arr1, arr2, n1, n2, 1);
}
Output
4-th element is : 5
7-th element is : 9
10-th element is : 14
1-th element is : -1
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment