通过删除 M 个最小元素来修改数组,保持剩余元素的顺序
给定一个正整数M和一个由N个不同的正整数组成的数组,任务是从数组中删除前M个最小的元素,这样剩余元素的相对顺序不会改变。
例子:
Input: M = 5, arr[] = {2, 81, 75, 98, 72, 63, 53, 5, 40, 92}
Output: 81 75 98 72 92
Explanation:
The first M(= 5) smallest element are {2, 5, 40, 53, 63}. After removing these elements the modified array is {81, 75, 98, 72, 92}.
Input: M = 1, arr[] = {8, 3, 6, 10, 5}
Output: 8 6 10 5
基于排序的方法:给定的问题可以通过将每个数组元素与其索引配对,然后对数组进行排序来解决。请按照以下步骤解决问题:
- 初始化对A的向量并将A[i]初始化为{arr[i], i} 。
- 按对的第一个元素对向量A[]进行排序。
- 按该对的第二个元素对范围[M, N – 1]上的元素进行排序。
- 现在,使用变量i遍历给定范围[M, N – 1]并打印A[i].first的值作为结果数组元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the array after
// removing the smallest M elements
void removeSmallestM(int arr[], int N,
int M)
{
// Store pair of {element, index}
vector > A;
// Iterate over the range [0, N]
for (int i = 0; i < N; i++) {
A.emplace_back(arr[i], i);
}
// Sort with respect to the
// first value
sort(A.begin(), A.end());
// Sort from the index M to N - 1
// using comparator for sorting
// by the second value
sort(A.begin() + M, A.end(),
[&](pair a, pair b) {
return a.second < b.second;
});
// Traverse from M to N - 1
for (int i = M; i < N; i++) {
cout << A[i].first << " ";
}
}
// Driver Code
int main()
{
int M = 5;
int arr[] = { 2, 81, 75, 98, 72,
63, 53, 5, 40, 92 };
int N = sizeof(arr) / sizeof(arr[0]);
removeSmallestM(arr, N, M);
return 0;
}
Python3
# Python3 program for the above approach
# Function to print the array after
# removing the smallest M elements
def removeSmallestM(arr, N, M):
# Store pair of {element, index}
A = []
# Iterate over the range [0, N]
for i in range(N):
A.append([arr[i], i])
# Sort with respect to the
# first value
A = sorted(A)
B = []
for i in range(M, N):
B.append([A[i][1], A[i][0]])
B = sorted(B)
# Traverse from M to N - 1
for i in range(len(B)):
print(B[i][1], end = " ")
# Driver Code
if __name__ == '__main__':
M = 5
arr = [ 2, 81, 75, 98, 72,
63, 53, 5, 40, 92 ]
N = len(arr)
removeSmallestM(arr, N, M)
# This code is contributed by mohit kumar 29
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the array after
// removing the smallest M elements
void removeSmallestM(int arr[], int N,
int M)
{
// Stores the copy of arr
vector A(arr, arr + N);
// Sort the vector in increasing
// order
sort(A.begin(), A.end());
// Stores the smallest M elements
unordered_map mp;
for (int i = 0; i < M; i++) {
// Insert A[i] in the map
mp[A[i]] = 1;
}
for (int i = 0; i < N; i++) {
// If current value is present
// in the hashmap
if (mp.find(arr[i]) == mp.end()) {
// Print the value of
// current element
cout << arr[i] << " ";
}
}
}
// Driver Code
int main()
{
int M = 5;
int arr[] = { 2, 81, 75, 98, 72,
63, 53, 5, 40, 92 };
int N = sizeof(arr) / sizeof(arr[0]);
removeSmallestM(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static int[] reverse(int a[]) {
int i, n = a.length, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
// Function to print the array after
// removing the smallest M elements
static void removeSmallestM(int arr[], int N,
int M)
{
// Stores the copy of arr
int[] A = new int[N];
for(int i = 0;i mp = new LinkedHashMap();
for (int i = 0; i < M; i++) {
// Insert A[i] in the map
mp.put(A[i], 1);
}
for (int i = 0; i < N; i++)
{
// If current value is present
// in the hashmap
if (mp.containsKey(arr[i]))
{
// Print the value of
// current element
System.out.print(arr[i]+ " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int M = 5;
int arr[] = { 2, 81, 75, 98, 72,
63, 53, 5, 40, 92 };
int N = arr.length;
removeSmallestM(arr, N, M);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python Program for the above approach
# Function to print the array after
# removing the smallest M elements
def removeSmallestM(arr, N, M) :
# Stores the copy of arr
A = arr.copy()
# Sort the vector in increasing
# order
A.sort()
# Stores the smallest M elements
mp = {}
for i in range(M) :
# Insert A[i] in the map
mp[A[i]] = 1
for i in range(N) :
# If current value is present
# in the hashmap
if arr[i] not in mp :
# Print the value of
# current element
print(arr[i], end = " ")
# Driver Code
M = 5
arr = [2, 81, 75, 98, 72, 63, 53, 5, 40, 92]
N = len(arr)
removeSmallestM(arr, N, M)
# This code is contributed by gfgking
Javascript
输出:
81 75 98 72 92
时间复杂度: O(N*log N)
辅助空间: O(N)
基于HashMap的方法:给定的问题也可以使用 HashMap 来存储数组的最小M个元素来解决。请按照以下步骤解决问题:
- 初始化一个辅助向量,比如A ,并将所有数组元素arr[]存储在其中。
- 对向量A进行排序并初始化一个 HashMap,比如mp 。
- 使用变量i遍历[0, M – 1]范围,并在 HashMap 中插入A[i] 。
- 使用变量i遍历范围[0, N – 1] ,如果 HashMap 中不存在arr[i]的值,则打印arr[i]的值作为结果数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the array after
// removing the smallest M elements
void removeSmallestM(int arr[], int N,
int M)
{
// Stores the copy of arr
vector A(arr, arr + N);
// Sort the vector in increasing
// order
sort(A.begin(), A.end());
// Stores the smallest M elements
unordered_map mp;
for (int i = 0; i < M; i++) {
// Insert A[i] in the map
mp[A[i]] = 1;
}
for (int i = 0; i < N; i++) {
// If current value is present
// in the hashmap
if (mp.find(arr[i]) == mp.end()) {
// Print the value of
// current element
cout << arr[i] << " ";
}
}
}
// Driver Code
int main()
{
int M = 5;
int arr[] = { 2, 81, 75, 98, 72,
63, 53, 5, 40, 92 };
int N = sizeof(arr) / sizeof(arr[0]);
removeSmallestM(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static int[] reverse(int a[]) {
int i, n = a.length, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
// Function to print the array after
// removing the smallest M elements
static void removeSmallestM(int arr[], int N,
int M)
{
// Stores the copy of arr
int[] A = new int[N];
for(int i = 0;i mp = new LinkedHashMap();
for (int i = 0; i < M; i++) {
// Insert A[i] in the map
mp.put(A[i], 1);
}
for (int i = 0; i < N; i++)
{
// If current value is present
// in the hashmap
if (mp.containsKey(arr[i]))
{
// Print the value of
// current element
System.out.print(arr[i]+ " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int M = 5;
int arr[] = { 2, 81, 75, 98, 72,
63, 53, 5, 40, 92 };
int N = arr.length;
removeSmallestM(arr, N, M);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python Program for the above approach
# Function to print the array after
# removing the smallest M elements
def removeSmallestM(arr, N, M) :
# Stores the copy of arr
A = arr.copy()
# Sort the vector in increasing
# order
A.sort()
# Stores the smallest M elements
mp = {}
for i in range(M) :
# Insert A[i] in the map
mp[A[i]] = 1
for i in range(N) :
# If current value is present
# in the hashmap
if arr[i] not in mp :
# Print the value of
# current element
print(arr[i], end = " ")
# Driver Code
M = 5
arr = [2, 81, 75, 98, 72, 63, 53, 5, 40, 92]
N = len(arr)
removeSmallestM(arr, N, M)
# This code is contributed by gfgking
Javascript
输出:
81 75 98 72 92
时间复杂度: O(N*log N)
辅助空间: O(N)