给定一个数组arr []和两个整数K和M。问题是将数组与自身连接M次后,找到第K个极小值元素。
例子:
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
简单方法:
- 将给定的数组附加到向量或称为V的任何其他数组中M次。
- 按升序对向量或数组V排序。
- 返回向量V的索引(K-1)处的值,即返回V [K – 1]。
下面是上述方法的实现:
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 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[0]);
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
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[0]);
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
输出:
2
有效方法:如果M的值较小,则上述方法可以正常工作,但对于
较大的M值将导致内存错误或超时错误。
这样做的目的是要观察到,在对给定数组进行M次连接并再次对其进行排序之后,每个元素现在在新的级联数组中将出现M次。所以,
- 给定数组升序排序。
- 返回给定数组的索引((K-1)/ M)上存在的值,即返回arr [((K-1)/ M)]。
下面是上述方法的实现:
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[0]);
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
输出:
2