Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets
Last Updated :
28 Mar, 2022
Given an array arr[] consisting of N integers, the task is to repeatedly select triplets and remove the maximum and minimum elements from the triplets in each operation, such that the remaining array is of longest possible length and consists only of distinct elements.
Examples:
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
Approach: Follow the steps below to solve the problem:
- Traverse the array arr[] and calculate the frequencies of each array element.
- For each distinct element, check if its frequency is even or odd and perform the following operations accordingly:
- If frequency is odd (Except 1):
- Remove 2 elements in each operation, by selecting same elements in triplets. After a series of operations, only 1 occurrence of the element will remain.
- If frequency is even (Except 2):
- Remove 2 elements in each operation, by selecting the same values in triplets. After a series of operations, only two occurrences of the element will remain
- Now, initialize a variable, say cnt, to store the count of all even frequent array elements.
- If cnt is even, then make all even elements unique, without removing any unique element from the array by selecting triplets only from even frequent elements.
- Otherwise, remove 1 unique element to make the array pairwise distinct.
C++
#include <bits/stdc++.h>
using namespace std;
int maxUniqueElements( int A[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[A[i]]++;
}
int cnt = 0;
for ( auto x : mp) {
if (x.second % 2 == 0) {
cnt++;
}
}
int ans = mp.size();
if (cnt % 2 == 1) {
ans--;
}
return ans;
}
int main()
{
int N = 5;
int A[] = { 1, 2, 1, 3, 7 };
cout << maxUniqueElements(A, N);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int maxUniqueElements( int [] Arr, int N)
{
HashMap<Integer, Integer> mp = new HashMap<>();
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 );
}
}
int cnt = 0 ;
for (Map.Entry<Integer, Integer> entry :
mp.entrySet()) {
if ((entry.getValue()) % 2 == 0 ) {
cnt++;
}
}
int ans = mp.size();
if (cnt % 2 == 1 ) {
ans--;
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
int A[] = { 1 , 2 , 1 , 3 , 7 };
System.out.println(maxUniqueElements(A, N));
}
}
|
Python3
def maxUniqueElements(A, N):
mp = {}
for i in range (N):
if A[i] in mp:
mp[A[i]] + = 1
else :
mp[A[i]] = 1
cnt = 0
for key,value in mp.items():
if (value % 2 = = 0 ):
cnt + = 1
ans = len (mp)
if (cnt % 2 = = 1 ):
ans - = 1
return ans
if __name__ = = '__main__' :
N = 5
A = [ 1 , 2 , 1 , 3 , 7 ]
print (maxUniqueElements(A, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int maxUniqueElements( int [] Arr, int N)
{
Dictionary< int , int > mp
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(Arr[i])) {
mp[Arr[i]] += 1;
}
else {
mp[Arr[i]] = 1;
}
}
int cnt = 0;
foreach (KeyValuePair< int , int > entry in mp)
{
if ((entry.Value) % 2 == 0) {
cnt++;
}
}
int ans = mp.Count;
if (cnt % 2 == 1) {
ans--;
}
return ans;
}
public static void Main( string [] args)
{
int N = 5;
int [] A = { 1, 2, 1, 3, 7 };
Console.Write(maxUniqueElements(A, N));
}
}
|
Javascript
<script>
function maxUniqueElements(A, N)
{
let mp = new Map();
for (let i = 0; i < N; i++)
{
if (mp.has(A[i]))
{
mp.set(mp.get(A[i]), mp.get(A[i]) + 1);
}
else
{
mp.set(A[i], 1);
}
}
let cnt = 0;
for (let [key, value] of mp)
{
if (value % 2 == 0)
{
cnt++;
}
}
let ans = mp.size;
if (cnt % 2 == 1)
{
ans--;
}
return ans;
}
let N = 5;
let A = [ 1, 2, 1, 3, 7 ];
document.write(maxUniqueElements(A, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...