通过删除所有不同的元素对来最小化数组
给定一个长度为N的数组A[] ,任务是在多次选择和删除不同的元素对后找到数组的最小大小。
例子:
Input: A[] = {1, 7, 7, 4, 4, 4}, N = 6
Output: 0
Explanation: 3 pairs containing distinct elements can be formed from the array. Thus, no element left in the array.
Input: A[] = {25, 25}, N = 2
Output: 2
Explanation: No pairs containing distinct elements can be formed from the array. Thus, 2 elements left in the array.
解决方案:按照以下步骤解决此问题:
- 创建一个映射,例如mp来存储数组中每个元素的频率。让任何元素的最大频率为m 。
- 如果数组的长度是偶数:
- 请注意,如果m小于或等于 ( N /2),则每个元素都可以与另一个元素形成一对。因此,输出 0。
- 否则,数组的最小大小,即没有一对的元素的数量由下式给出:
- 如果数组的长度是奇数:
- 请注意,如果m大于或等于 ( N /2),则始终会留下 1 个元素而没有任何对。因此,输出 1。
- 否则,数组的最小大小将再次由 (2* m – N ) 给出。
下面是上述代码的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum size of
// of the array after performing a set of
// operations
int minSize(int N, int A[])
{
// Map to store the frequency of each
// element of the array
map mpp;
// Stores the maximum frequency of an
// element
int m = 0;
// Loop to find the maximum frequency of
// an element
for (int i = 0; i < N; ++i) {
// Increment the frequency
mpp[A[i]]++;
// Stores the maximum frequency
m = max(mpp[A[i]], m);
}
// If N is even
if (N % 2 == 0) {
// If m is less than or equal to N/2
if (m <= N / 2) {
return 0;
}
else {
return (2 * m) - N;
}
}
else {
// If m is less than or equal to N/2
if (m <= N / 2) {
return 1;
}
else {
return (2 * m) - N;
}
}
}
// Driver Code
int main()
{
// Given input
int N = 6;
int A[N] = { 1, 7, 7, 4, 4, 4 };
// Function Call
cout << minSize(N, A);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the minimum size of
// of the array after performing a set of
// operations
static int minSize(int N, int[] A)
{
// Map to store the frequency of each
// element of the array
HashMap mpp
= new HashMap();
// Stores the maximum frequency of an
// element
int m = 0;
// Loop to find the maximum frequency of
// an element
for (int i = 0; i < N; ++i) {
// Increment the frequency
if (mpp.containsKey(A[i])) {
mpp.put(A[i], mpp.get(A[i]) + 1);
}
else {
mpp.put(A[i], 1);
}
// Stores the maximum frequency
m = Math.max(mpp.get(A[i]), m);
}
// If N is even
if (N % 2 == 0) {
// If m is less than or equal to N/2
if (m <= N / 2) {
return 0;
}
else {
return (2 * m) - N;
}
}
else {
// If m is less than or equal to N/2
if (m <= N / 2) {
return 1;
}
else {
return (2 * m) - N;
}
}
}
// Driver Code
public static void main(String[] args)
{
// Given input
int N = 6;
int[] A = { 1, 7, 7, 4, 4, 4 };
// Function Call
System.out.println(minSize(N, A));
}
}
// Thi code is contributed by ukasp.
Python3
# Python program for the above approach
# Function to find the minimum size of
# of the array after performing a set of
# operations
def minSize(N, A):
# Map to store the frequency of each
# element of the array
mpp = {}
# Stores the maximum frequency of an
# element
m = 0
# Loop to find the maximum frequency of
# an element
for i in range(N):
if A[i] not in mpp:
mpp[A[i]]=0
# Increment the frequency
mpp[A[i]]+=1
# Stores the maximum frequency
m = max(mpp[A[i]], m)
# If N is even
if (N % 2 == 0):
# If m is less than or equal to N/2
if (m <= N / 2):
return 0
else:
return (2 * m) - N
else:
# If m is less than or equal to N/2
if (m <= N / 2):
return 1
else:
return (2 * m) - N
# Driver Code
# Given input
N = 6
A = [1, 7, 7, 4, 4, 4]
# Function Call
print(minSize(N, A))
# This code is contributed by Shubham Singh
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to find the minimum size of
// of the array after performing a set of
// operations
static int minSize(int N, int []A)
{
// Map to store the frequency of each
// element of the array
Dictionary mpp =
new Dictionary();
// Stores the maximum frequency of an
// element
int m = 0;
// Loop to find the maximum frequency of
// an element
for (int i = 0; i < N; ++i) {
// Increment the frequency
if (mpp.ContainsKey(A[i]))
{
mpp[A[i]] = mpp[A[i]] + 1;
}
else
{
mpp.Add(A[i], 1);
}
// Stores the maximum frequency
m = Math.Max(mpp[A[i]], m);
}
// If N is even
if (N % 2 == 0) {
// If m is less than or equal to N/2
if (m <= N / 2) {
return 0;
}
else {
return (2 * m) - N;
}
}
else {
// If m is less than or equal to N/2
if (m <= N / 2) {
return 1;
}
else {
return (2 * m) - N;
}
}
}
// Driver Code
public static void Main()
{
// Given input
int N = 6;
int []A = { 1, 7, 7, 4, 4, 4 };
// Function Call
Console.Write(minSize(N, A));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出:
0
时间复杂度: O(N)
辅助空间: O(N)