重复删除三元组的最大和最小元素后可能的最长剩余不同元素数组
给定一个由N个整数组成的数组arr[] ,任务是重复选择三元组并在每个操作中从三元组中删除最大和最小元素,以使剩余的数组具有最长的可能长度并且仅包含不同的元素。
例子:
Input: N = 5, arr[] = {1, 2, 1, 3, 7}<
Output: 3
Explanation: Select the triplet (1, 2, 1) and remove 1 and 2. The remaining array is [1, 3, 7] in which all elements are pairwise distinct.
Input: N = 6, arr[] = {8, 8, 8, 9, 9, 9}
Output: 2
解决方法:按照以下步骤解决问题:
- 遍历数组arr[]并计算每个数组元素的频率。
- 对于每个不同的元素,检查其频率是偶数还是奇数,并相应地执行以下操作:
- 如果频率为奇数(1 除外):
- 通过选择三元组中的相同元素,在每个操作中删除 2 个元素。经过一系列操作后,该元素将仅出现 1 次。
- 如果频率是偶数(除了 2):
- 通过在三元组中选择相同的值,在每个操作中删除 2 个元素。经过一系列操作后,该元素将仅保留两次出现
- 现在,初始化一个变量,比如cnt ,以存储所有偶数频繁数组元素的计数。
- 如果cnt是偶数,则使所有偶数元素唯一,而不通过仅从偶数频繁元素中选择三元组来从数组中删除任何唯一元素。
- 否则,删除 1 个唯一元素以使数组成对不同。
- 如果频率为奇数(1 除外):
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to return length of longest
// remaining array of pairwise distinct
// array possible by removing triplets
int maxUniqueElements(int A[], int N)
{
// Stores the frequency of array elements
unordered_map mp;
// Traverse the array
for (int i = 0; i < N; i++) {
mp[A[i]]++;
}
// Iterate through the map
int cnt = 0;
for (auto x : mp) {
// If frequency of current
// element is even
if (x.second % 2 == 0) {
cnt++;
}
}
// Stores the required count
// of unique elements remaining
int ans = mp.size();
// If count is odd
if (cnt % 2 == 1) {
ans--;
}
return ans;
}
// Driver Code
int main()
{
int N = 5;
int A[] = { 1, 2, 1, 3, 7 };
cout << maxUniqueElements(A, N);
}
Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to return length of longest
// remaining array of pairwise distinct
// array possible by removing triplets
static int maxUniqueElements(int[] Arr, int N)
{
// Stores the frequency of array elements
HashMap mp = new HashMap<>();
// Traverse the array
for (int i = 0; i < N; i++) {
if (mp.containsKey(Arr[i])) {
mp.put(Arr[i], mp.get(Arr[i]) + 1);
}
else {
mp.put(Arr[i], 1);
}
}
// Iterate through the map
int cnt = 0;
for (Map.Entry entry :
mp.entrySet()) {
// If frequency of current
// element is even
if ((entry.getValue()) % 2 == 0) {
cnt++;
}
}
// Stores the required count
// of unique elements remaining
int ans = mp.size();
// If count is odd
if (cnt % 2 == 1) {
ans--;
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int A[] = { 1, 2, 1, 3, 7 };
System.out.println(maxUniqueElements(A, N));
}
}
// This code is contributed by maddler.
Python3
# Python3 Program to implement
# the above approach
# Function to return length of longest
# remaining array of pairwise distinct
# array possible by removing triplets
def maxUniqueElements(A, N):
# Stores the frequency of array elements
mp = {}
# Traverse the array
for i in range(N):
if A[i] in mp:
mp[A[i]] += 1
else:
mp[A[i]] = 1
# Iterate through the map
cnt = 0
for key,value in mp.items():
# If frequency of current
# element is even
if (value % 2 == 0):
cnt += 1
# Stores the required count
# of unique elements remaining
ans = len(mp)
# If count is odd
if (cnt % 2 == 1):
ans -= 1
return ans
# Driver Code
if __name__ == '__main__':
N = 5
A = [1, 2, 1, 3, 7]
print(maxUniqueElements(A, N))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to return length of longest
// remaining array of pairwise distinct
// array possible by removing triplets
static int maxUniqueElements(int[] Arr, int N)
{
// Stores the frequency of array elements
Dictionary mp
= new Dictionary();
// Traverse the array
for (int i = 0; i < N; i++) {
if (mp.ContainsKey(Arr[i])) {
mp[Arr[i]] += 1;
}
else {
mp[Arr[i]] = 1;
}
}
// Iterate through the map
int cnt = 0;
foreach(KeyValuePair entry in mp)
{
// If frequency of current
// element is even
if ((entry.Value) % 2 == 0) {
cnt++;
}
}
// Stores the required count
// of unique elements remaining
int ans = mp.Count;
// If count is odd
if (cnt % 2 == 1) {
ans--;
}
return ans;
}
// Driver Code
public static void Main(string[] args)
{
int N = 5;
int[] A = { 1, 2, 1, 3, 7 };
Console.Write(maxUniqueElements(A, N));
}
}
// This code is contributed by ukasp.
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(N)