Minimize Array by removing all distinct element pairs
Given an array A[] of length N, the task is to find the minimum size of the array after selecting and removing distinct element pairs any number of times.
Examples:
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.
Solution: Follow the steps below to solve this problem:
- Create a map, say mp to store the frequency of each element in the array. Let the maximum frequency of any element be m.
- If the length of the array is even:
- Notice that if m is less than or equal to (N/2), then every element can form a pair with another. Thus, output 0.
- Otherwise, the minimum size of the array i.e, the number of elements left without a pair are given by:
- If the length of the array is odd:
- Notice that if m is greater than or equal to (N/2), then 1 element will always be left without any pair. Thus, output 1.
- Otherwise, the minimum size of the array will again be given by (2*m–N).
Below is the implementation of the above code:
C++
#include <bits/stdc++.h>
using namespace std;
int minSize( int N, int A[])
{
map< int , int > mpp;
int m = 0;
for ( int i = 0; i < N; ++i) {
mpp[A[i]]++;
m = max(mpp[A[i]], m);
}
if (N % 2 == 0) {
if (m <= N / 2) {
return 0;
}
else {
return (2 * m) - N;
}
}
else {
if (m <= N / 2) {
return 1;
}
else {
return (2 * m) - N;
}
}
}
int main()
{
int N = 6;
int A[N] = { 1, 7, 7, 4, 4, 4 };
cout << minSize(N, A);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minSize( int N, int [] A)
{
HashMap<Integer, Integer> mpp
= new HashMap<Integer, Integer>();
int m = 0 ;
for ( int i = 0 ; i < N; ++i) {
if (mpp.containsKey(A[i])) {
mpp.put(A[i], mpp.get(A[i]) + 1 );
}
else {
mpp.put(A[i], 1 );
}
m = Math.max(mpp.get(A[i]), m);
}
if (N % 2 == 0 ) {
if (m <= N / 2 ) {
return 0 ;
}
else {
return ( 2 * m) - N;
}
}
else {
if (m <= N / 2 ) {
return 1 ;
}
else {
return ( 2 * m) - N;
}
}
}
public static void main(String[] args)
{
int N = 6 ;
int [] A = { 1 , 7 , 7 , 4 , 4 , 4 };
System.out.println(minSize(N, A));
}
}
|
Python3
def minSize(N, A):
mpp = {}
m = 0
for i in range (N):
if A[i] not in mpp:
mpp[A[i]] = 0
mpp[A[i]] + = 1
m = max (mpp[A[i]], m)
if (N % 2 = = 0 ):
if (m < = N / 2 ):
return 0
else :
return ( 2 * m) - N
else :
if (m < = N / 2 ):
return 1
else :
return ( 2 * m) - N
N = 6
A = [ 1 , 7 , 7 , 4 , 4 , 4 ]
print (minSize(N, A))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int minSize( int N, int []A)
{
Dictionary< int , int > mpp =
new Dictionary< int , int >();
int m = 0;
for ( int i = 0; i < N; ++i) {
if (mpp.ContainsKey(A[i]))
{
mpp[A[i]] = mpp[A[i]] + 1;
}
else
{
mpp.Add(A[i], 1);
}
m = Math.Max(mpp[A[i]], m);
}
if (N % 2 == 0) {
if (m <= N / 2) {
return 0;
}
else {
return (2 * m) - N;
}
}
else {
if (m <= N / 2) {
return 1;
}
else {
return (2 * m) - N;
}
}
}
public static void Main()
{
int N = 6;
int []A = { 1, 7, 7, 4, 4, 4 };
Console.Write(minSize(N, A));
}
}
|
Javascript
<script>
function minSize(N, A) {
let mpp = new Map();
let m = 0;
for (let i = 0; i < N; ++i) {
if (!mpp.has(A[i]))
mpp.set(A[i], 1);
else
mpp.set(A[i], mpp.get(A[i]) + 1)
m = Math.max(mpp.get(A[i]), m);
}
if (N % 2 == 0) {
if (m <= Math.floor(N / 2)) {
return 0;
}
else {
return (2 * m) - N;
}
}
else {
if (m <= Math.floor(N / 2)) {
return 1;
}
else {
return (2 * m) - N;
}
}
}
let N = 6;
let A = [1, 7, 7, 4, 4, 4]
document.write(minSize(N, A));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
16 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...