Related Articles
Find the K-th minimum element from an array concatenated M times
• Difficulty Level : Medium
• Last Updated : 18 Apr, 2019

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Approach :

1. Append the given array into a vector or any other array say V for M times.
2. Sort the vector or array V in ascending order.
3. 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++

 `// C++ programme to find the K'th minimum  ` `// element from an array concatenated M times ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the K-th minimum element  ` `// from an array concatenated M times ` `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 the elements in ascending order ` `    ``sort(V.begin(), V.end()); ` ` `  `    ``// return K'th Min element ` `    ``// present at K-1 index ` `    ``return` `(V[K - 1]); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `A[] = { 3, 1, 2 }; ` ` `  `    ``int` `M = 3, K = 4; ` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``cout << KthMinValAfterMconcatenate(A, N, M, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `import` `java.util.ArrayList; ` `import` `java.util.Collections; ` ` `  `// Java programme to find the K'th minimum  ` `// element from an array concatenated M times ` `class` `GFG  ` `{ ` ` `  `    ``// Function to find the K-th minimum element  ` `    ``// from an array concatenated M times ` `    ``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]); ` `            ``} ` `        ``} ` ` `  `        ``// sort the elements in ascending order ` `        ``Collections.sort(V); ` ` `  `        ``// return K'th Min element ` `        ``// present at K-1 index ` `        ``return` `((``int``) V.get(K - ``1``)); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 program to find the K'th minimum   ` `# element from an array concatenated M times  ` `   `  `# Function to find the K-th minimum element   ` `# from an array concatenated M times  ` `def` `KthMinValAfterMconcatenate(A, N, M, K):  ` `  `  `    ``V ``=` `[]  ` `   `  `    ``for` `i ``in` `range``(``0``, M):   ` `        ``for` `j ``in` `range``(``0``, N):   ` `            ``V.append(A[j])  ` `   `  `    ``# sort the elements in ascending order  ` `    ``V.sort()  ` `   `  `    ``# return K'th Min element  ` `    ``# present at K-1 index  ` `    ``return` `V[K ``-` `1``]  ` `   `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"``: ` `  `  `    ``A ``=` `[``3``, ``1``, ``2``]   ` `   `  `    ``M, K ``=` `3``, ``4`  `    ``N ``=` `len``(A)  ` `   `  `    ``print``(KthMinValAfterMconcatenate(A, N, M, K))  ` `   `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# programme to find the K'th minimum  ` `// element from an array concatenated M times ` `using` `System; ` `using` `System.Collections; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to find the K-th minimum element  ` `// from an array concatenated M times ` `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]); ` `        ``} ` `    ``} ` ` `  `    ``// sort the elements in ascending order ` `    ``V.Sort(); ` ` `  `    ``// return K'th Min element ` `    ``// present at K-1 index ` `    ``return` `((``int``)V[K - 1]); ` `} ` ` `  `// Driver Code ` `static` `void` `Main() ` `{ ` `    ``int` `[]A = { 3, 1, 2 }; ` ` `  `    ``int` `M = 3, K = 4; ` `    ``int` `N = A.Length; ` ` `  `    ``Console.WriteLine(KthMinValAfterMconcatenate(A, N, M, K)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```2
```

Efficent Approach: The above approach will work fine if the value of M is small, but for
a large value of M it will give 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,

1. Sort given array in ascending order.
2. 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++

 `// C++ programme to find the K'th minimum element  ` `// from an array concatenated M times ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the K-th minimum element  ` `// from an array concatenated M times ` `int` `KthMinValAfterMconcatenate(``int` `A[], ``int` `N,  ` `                                  ``int` `M, ``int` `K) ` `{ ` `    ``// Sort the elements in  ` `    ``// ascending order ` `    ``sort(A, A + N); ` ` `  `    ``// Return the K'th Min element ` `    ``// present at ( (K-1) / M ) index ` `    ``return` `(A[((K - 1) / M)]); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `A[] = { 3, 1, 2 }; ` ` `  `    ``int` `M = 3, K = 4; ` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``cout << KthMinValAfterMconcatenate(A, N, M, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java programme to find the K'th minimum element  ` `// from an array concatenated M times ` `import` `java.util.*; ` ` `  `class` `GFG1  ` `{ ` ` `  `    ``// Function to find the K-th minimum element  ` `    ``// from an array concatenated M times ` `    ``static` `int` `KthMinValAfterMconcatenate(``int``[] A, ``int` `N, ` `                                            ``int` `M, ``int` `K)  ` `    ``{ ` `        ``// Sort the elements in  ` `        ``// ascending order ` `        ``Arrays.sort(A); ` ` `  `        ``// Return the K'th Min element ` `        ``// present at ( (K-1) / M ) index ` `        ``return` `(A[((K - ``1``) / M)]); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## Python3

 `# Python3 program to find the K'th minimum  ` `# element from an array concatenated M times  ` ` `  `# Function to find the K-th minimum element  ` `# from an array concatenated M times  ` `def` `KthMinValAfterMconcatenate(A, N, M, K):  ` ` `  `    ``V ``=` `[]  ` ` `  `    ``for` `i ``in` `range``(``0``, M):  ` `        ``for` `j ``in` `range``(``0``, N):  ` `            ``V.append(A[j])  ` ` `  `    ``# sort the elements in ascending order  ` `    ``V.sort()  ` ` `  `    ``# return K'th Min element  ` `    ``# present at K-1 index  ` `    ``return` `V[K ``-` `1``]  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``A ``=` `[``3``, ``1``, ``2``]  ` ` `  `    ``M, K ``=` `3``, ``4` `    ``N ``=` `len``(A)  ` ` `  `    ``print``(KthMinValAfterMconcatenate(A, N, M, K))  ` ` `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# programme to find the K'th minimum element  ` `// from an array concatenated M times ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to find the K-th minimum element  ` `// from an array concatenated M times ` `static` `int` `KthMinValAfterMconcatenate(``int` `[]A, ``int` `N,  ` `                                ``int` `M, ``int` `K) ` `{ ` `    ``// Sort the elements in  ` `    ``// ascending order ` `    ``Array.Sort(A); ` ` `  `    ``// Return the K'th Min element ` `    ``// present at ( (K-1) / M ) index ` `    ``return` `(A[((K - 1) / M)]); ` `} ` ` `  `// Driver Code ` `static` `void` `Main() ` `{ ` `    ``int` `[]A = { 3, 1, 2 }; ` ` `  `    ``int` `M = 3, K = 4; ` `    ``int` `N = A.Length; ` ` `  `    ``Console.WriteLine(KthMinValAfterMconcatenate(A, N, M, K)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :