Find the K-th minimum element from an array concatenated M times
Last Updated :
20 Aug, 2022
Given an array arr[] and two integers K and M. The problem is to find the K-th Minimum element after concatenating the array to itself M times.
Examples:
Input : arr[] = {3, 1, 2}, K = 4, M = 3
Output : 4'th Minimum element is : 2
Explanation: Concatenate array 3 times (ie., M = 3)
arr[] = [3, 1, 2, 3, 1, 2, 3, 1, 2]
arr[] = [1, 1, 1, 2, 2, 2, 3, 3, 3]
Now 4'th Minimum element is 2
Input : arr[] = {1, 13, 9, 17, 1, 12}, K = 19, M = 7
Output : 19'th Minimum element is : 9
Simple Approach :
- Append the given array into a vector or any other array say V for M times.
- Sort the vector or array V in ascending order.
- Return the value at index ( K-1 ) of vector V i.e., return V[ K – 1 ].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int KthMinValAfterMconcatenate( int A[], int N,
int M, int K)
{
vector< int > V;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
V.push_back(A[j]);
}
}
sort(V.begin(), V.end());
return (V[K - 1]);
}
int main()
{
int A[] = { 3, 1, 2 };
int M = 3, K = 4;
int N = sizeof (A) / sizeof (A[0]);
cout << KthMinValAfterMconcatenate(A, N, M, K);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
class GFG
{
static int KthMinValAfterMconcatenate( int [] A, int N,
int M, int K)
{
ArrayList V = new ArrayList();
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
V.add(A[j]);
}
}
Collections.sort(V);
return (( int ) V.get(K - 1 ));
}
public static void main(String[] args)
{
int [] A = { 3 , 1 , 2 };
int M = 3 , K = 4 ;
int N = A.length;
System.out.println(KthMinValAfterMconcatenate(A, N, M, K));
}
}
|
Python3
def KthMinValAfterMconcatenate(A, N, M, K):
V = []
for i in range ( 0 , M):
for j in range ( 0 , N):
V.append(A[j])
V.sort()
return V[K - 1 ]
if __name__ = = "__main__" :
A = [ 3 , 1 , 2 ]
M, K = 3 , 4
N = len (A)
print (KthMinValAfterMconcatenate(A, N, M, K))
|
C#
using System;
using System.Collections;
class GFG
{
static int KthMinValAfterMconcatenate( int []A, int N,
int M, int K)
{
ArrayList V= new ArrayList();
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
V.Add(A[j]);
}
}
V.Sort();
return (( int )V[K - 1]);
}
static void Main()
{
int []A = { 3, 1, 2 };
int M = 3, K = 4;
int N = A.Length;
Console.WriteLine(KthMinValAfterMconcatenate(A, N, M, K));
}
}
|
PHP
<?php
function KthMinValAfterMconcatenate( $A , $N ,
$M , $K )
{
$V = array ();
for ( $i = 0; $i < $M ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
{
array_push ( $V , $A [ $j ]);
}
}
sort( $V );
return ( $V [ $K - 1]);
}
$A = array ( 3, 1, 2 );
$M = 3;
$K = 4;
$N = count ( $A );
echo KthMinValAfterMconcatenate( $A , $N , $M , $K );
?>
|
Javascript
<script>
function KthMinValAfterMconcatenate(A, N, M, K)
{
var V = [];
for ( var i = 0; i < M; i++) {
for ( var j = 0; j < N; j++) {
V.push(A[j]);
}
}
V.sort();
return (V[K - 1]);
}
var A = [3, 1, 2];
var M = 3, K = 4;
var N = A.length;
document.write( KthMinValAfterMconcatenate(A, N, M, K));
</script>
|
Time Complexity: O((M * N) * log(M * N))
Auxiliary Space: O(M * N)
Efficient Approach: The above approach will work fine if the value of M is small but for
a large value of M will give a memory error or a timeout error.
The idea is to observe that after sorting the given array when we concatenate it M times and sort it again, each element will now appear M times in the new concatenated array. So,
- Sort given array in ascending order.
- Return the value present at the index ( (K-1) / M ) of the given array ie., return arr[((K-1) / M)].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int KthMinValAfterMconcatenate( int A[], int N,
int M, int K)
{
sort(A, A + N);
return (A[((K - 1) / M)]);
}
int main()
{
int A[] = { 3, 1, 2 };
int M = 3, K = 4;
int N = sizeof (A) / sizeof (A[0]);
cout << KthMinValAfterMconcatenate(A, N, M, K);
return 0;
}
|
Java
import java.util.*;
class GFG1
{
static int KthMinValAfterMconcatenate( int [] A, int N,
int M, int K)
{
Arrays.sort(A);
return (A[((K - 1 ) / M)]);
}
public static void main(String[] args)
{
int [] A = { 3 , 1 , 2 };
int M = 3 , K = 4 ;
int N = A.length;
System.out.println(KthMinValAfterMconcatenate(A, N, M, K));
}
}
|
Python3
def KthMinValAfterMconcatenate(A, N, M, K):
V = []
for i in range ( 0 , M):
for j in range ( 0 , N):
V.append(A[j])
V.sort()
return V[K - 1 ]
if __name__ = = "__main__" :
A = [ 3 , 1 , 2 ]
M, K = 3 , 4
N = len (A)
print (KthMinValAfterMconcatenate(A, N, M, K))
|
C#
using System;
class GFG
{
static int KthMinValAfterMconcatenate( int []A, int N,
int M, int K)
{
Array.Sort(A);
return (A[((K - 1) / M)]);
}
static void Main()
{
int []A = { 3, 1, 2 };
int M = 3, K = 4;
int N = A.Length;
Console.WriteLine(KthMinValAfterMconcatenate(A, N, M, K));
}
}
|
PHP
<?php
function KthMinValAfterMconcatenate( $A , $N , $M , $K )
{
sort( $A );
return ( $A [(( $K - 1) / $M )]);
}
$A = array (3, 1, 2);
$M = 3; $K = 4;
$N = sizeof( $A );
echo (KthMinValAfterMconcatenate( $A , $N , $M , $K ));
?>
|
Javascript
<script>
function KthMinValAfterMconcatenate(A, N, M, K)
{
A.sort((a,b)=>a-b)
return (A[((K - 1) / M)]);
}
var A = [3, 1, 2 ];
var M = 3, K = 4;
var N = A.length;
document.write( KthMinValAfterMconcatenate(A, N, M, K));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...