Minimize deletions in Array by deleting all occurrences of any number such that array size is reduced to at least half
Last Updated :
16 Feb, 2023
Given an array arr[] of positive integers, the task is to select an element from the array and delete all its occurrences, such that the number of elements selected are minimum and the array size becomes atleast half of its original size.
Note: Size of the given array is always even.
Example:
Input: arr[] = {2, 2, 2, 2, 4, 4, 6, 7, 7, 3, 3, 3}
Output: 2
Explanation: First we select 2 and delete all its occurrences after that arr[] becomes – {4, 4, 6, 7, 7, 3, 3, 3} with size = 8. As the size is still greater than half, we select 3 and delete all its occurrences, after that arr[] becomes – {4, 4, 6, 7, 7} with size = 5.
Input: arr[] = {3, 3, 3, 3, 3}
Output: 1
Explanation : select 3 and remove all its occurrences.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 4
Explanation : Since there is no duplicate elements, hence any 4 elements can be removed to make the array length at least half.
Approach: The task can be easily achieved by removing the elements with maximum frequency, as soon as the array size becomes at least half of the actual size, we return the number of unique elements deleted till now.
Follow the steps to solve the problem:
- Use Hash-map to store frequency of the elements in array present.
- Store the frequencies in a list.
- Sort the list and traverse it from the back.
- Select the largest frequency element and decrement it from the array size and increment the count of unique elements deleted.
- If new array size becomes at-least half of the original array size, return the number of unique elements till now.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reduceArrSize( int arr[], int n)
{
unordered_map< int , int > hm;
for ( int i = 0; i < n; i++) {
hm[arr[i]]++;
}
vector< int > freq;
for ( auto it = hm.begin(); it != hm.end(); it++)
{
freq.push_back(it->second);
}
sort(freq.begin(), freq.end());
int size = n;
int idx = freq.size() - 1;
int count = 0;
while (size > n/ 2) {
size -= freq[idx--];
count++;
}
return count;
}
int main()
{
int arr[] = { 2, 2, 2, 2, 4, 4,
6, 7, 7, 3, 3, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
int count = reduceArrSize(arr, n);
cout<<(count);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int reduceArrSize( int [] arr)
{
HashMap<Integer, Integer> hm = new HashMap<>();
for ( int i = 0 ; i < arr.length; i++) {
hm.put(arr[i], hm.getOrDefault(arr[i], 0 ) + 1 );
}
ArrayList<Integer> freq
= new ArrayList<Integer>(hm.values());
Collections.sort(freq);
int size = arr.length;
int idx = freq.size() - 1 ;
int count = 0 ;
while (size > arr.length / 2 ) {
size -= freq[idx--];
count++;
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 2 , 2 , 2 , 2 , 4 , 4 ,
6 , 7 , 7 , 3 , 3 , 3 };
int count = reduceArrSize(arr);
System.out.println(count);
}
}
|
Python3
def reduceArrSize(arr,n):
hm = {}
for i in range (n):
if arr[i] in hm:
hm[arr[i]] + = 1
else :
hm[arr[i]] = 1
freq = []
for key,value in hm.items():
freq.append(value)
freq.sort()
size = n
idx = len (freq) - 1
count = 0
while (size > n / / 2 ):
size - = freq[idx]
idx - = 1
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 2 , 2 , 2 , 2 , 4 , 4 , 6 , 7 , 7 , 3 , 3 , 3 ]
n = len (arr)
count = reduceArrSize(arr, n)
print (count)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int reduceArrSize( int [] arr)
{
Dictionary< int , int > hm = new Dictionary< int , int >();
for ( int i = 0; i < arr.Length; i++) {
if (hm.ContainsKey(arr[i])){
hm[arr[i]] = hm[arr[i]]+1;
}
else {
hm.Add(arr[i], 1);
}
}
List< int > freq
= new List< int >(hm.Values);
freq.Sort();
int size = arr.Length;
int idx = freq.Count - 1;
int count = 0;
while (size > arr.Length / 2) {
size -= freq[idx--];
count++;
}
return count;
}
public static void Main(String[] args)
{
int [] arr = { 2, 2, 2, 2, 4, 4,
6, 7, 7, 3, 3, 3 };
int count = reduceArrSize(arr);
Console.WriteLine(count);
}
}
|
Javascript
<script>
function reduceArrSize(arr)
{
var hm = new Map();
for ( var i = 0; i < arr.length; i++) {
if (hm.has(arr[i])){
hm.set(arr[i], hm.get(arr[i])+1);
}
else {
hm.set(arr[i], 1);
}
}
var freq =[ ...hm.values()];
freq.sort();
var size = arr.length;
var idx = freq.length - 1;
var count = 0;
while (size > arr.length / 2) {
size -= freq[idx--];
count++;
}
return count;
}
var arr = [2, 2, 2, 2, 4, 4,
6, 7, 7, 3, 3, 3];
var count = reduceArrSize(arr);
document.write(count);
</script>
|
Time Complexity: O(N*logN), sorting the frequency list
Auxiliary Space: O(N), hashmap to store the frequencies
Another efficient approach(Using a Priority Queue):
We can solve the problem by using a priority queue instead of sorting the frequency of elements. So we initialize a priority queue and store the frequency of every unique element which we get from an unordered map. Then we run a while loop and start deleting the elements from the priority queue which has the highest frequency to the lowest frequencies in decreasing order and hence we get the count of elements to be deleted. The time complexity of this approach is O(n*logn) but it will take very less time if elements are more frequent.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reduceArrSize( int arr[], int n)
{
unordered_map< int , int > hm;
for ( int i = 0; i < n; i++) {
hm[arr[i]]++;
}
priority_queue< int > pq;
for ( auto it = hm.begin(); it != hm.end(); it++)
{
pq.push(it->second);
}
int size = n;
int count = 0;
while (size > n/ 2) {
size -= pq.top();
pq.pop();
count++;
}
return count;
}
int main()
{
int arr[] = { 2, 2, 2, 2, 4, 4, 6, 7, 7, 3, 3, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
int count = reduceArrSize(arr, n);
cout<<(count);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int reduceArrSize( int [] arr) {
Map<Integer, Integer> hm = new HashMap<>();
for ( int i = 0 ; i < arr.length; i++) {
hm.put(arr[i], hm.getOrDefault(arr[i], 0 ) + 1 );
}
PriorityQueue<Integer> pq = new PriorityQueue<>( new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
pq.add(entry.getValue());
}
int size = arr.length;
int count = 0 ;
while (size > arr.length / 2 ) {
size -= pq.poll();
count++;
}
return count;
}
public static void main(String[] args) {
int [] arr = { 2 , 2 , 2 , 2 , 4 , 4 , 6 , 7 , 7 , 3 , 3 , 3 };
int count = reduceArrSize(arr);
System.out.println(count);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int ReduceArrSize( int [] arr)
{
Dictionary< int , int > hm
= new Dictionary< int , int >();
for ( int i = 0; i < arr.Length; i++) {
if (hm.ContainsKey(arr[i])) {
hm[arr[i]]++;
}
else {
hm.Add(arr[i], 1);
}
}
SortedSet< int > pq = new SortedSet< int >(
Comparer< int >.Create((o1, o2) => o2 - o1));
foreach (KeyValuePair< int , int > entry in hm)
{
pq.Add(entry.Value);
}
int size = arr.Length;
int count = 0;
while (size > arr.Length / 2) {
size -= pq.Min;
pq.Remove(pq.Min);
count++;
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 2, 2, 2, 2, 4, 4, 6, 7, 7, 3, 3, 3 };
int count = ReduceArrSize(arr);
Console.WriteLine(count);
}
}
|
Javascript
function reduceArrSize(arr, n) {
let hm = new Map();
for (let i = 0; i < n; i++) {
if (hm.has(arr[i])) {
hm.set(arr[i], hm.get(arr[i]) + 1);
} else {
hm.set(arr[i], 1);
}
}
let pq = [];
for (let [key, value] of hm) {
pq.push(value);
}
pq.sort((a, b) => b - a);
let size = n;
let count = 0;
while (size > n / 2) {
size -= pq.shift();
count++;
}
return count;
}
let arr = [2, 2, 2, 2, 4, 4, 6, 7, 7, 3, 3, 3];
let n = arr.length;
let count = reduceArrSize(arr, n);
console.log(count);
|
Python3
import heapq
import collections
def reduceArrSize(arr):
hm = collections.Counter(arr)
pq = []
for frequency in hm.values():
heapq.heappush(pq, frequency)
size = len (arr)
count = 0
while size > len (arr) / 2 :
size - = heapq.heappop(pq)
count + = 1
return count / / 2
if __name__ = = "__main__" :
arr = [ 2 , 2 , 2 , 2 , 4 , 4 , 6 , 7 , 7 , 3 , 3 , 3 ]
count = reduceArrSize(arr)
print (count)
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Similar Reads
Minimize steps to reduce Array by deleting an element and all multiples in a step
Given an array arr[] of size N, the task is to find the minimum number of operations to reduce the array by deleting an element and all its multiples from the array in one operation. Examples: Input: N = 5, arr[] = {5, 2, 3, 8, 7}Output: 4Explanation: Removing 2, will make it remove 8 (as it is a mu
9 min read
Make all array elements equal by reducing array elements to half minimum number of times
Given an array arr[] consisting of N integers, the task is to minimize the number of operations required to make all array elements equal by converting Ai to Ai / 2. in each operation Examples: Input: arr[] = {3, 1, 1, 3}Output: 2Explanation: Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}. Red
6 min read
Reduce Array and Maximize sum by deleting one occurrence of A[i] and all occurrences of A[i]+1 and A[i]-1
Given an array A[] having N positive integers, the task is to perform the following operations and maximize the sum obtained while reducing the array: Select an array element (say A[i]) and delete one occurrence of that element and add A[i] to the sum.Delete all the occurrences of A[i]-1 and A[i]+1.
8 min read
Maximum number of times Array can be reduced in half when its all elements are even
Given an array arr, the task is to perform operations on the array when all elements in the array are even. In one operation, replace each integer X in the array by X/2. Find maximum possible number of operations that you can perform. Examples: Input: arr[] = {8, 12, 40}Output: 2Explanation: Initial
6 min read
Minimum decrements to make an Array at most 0 such that all array elements are cyclically decremented after a number is reduced to 0
Given a circular array arr[] of N integers and an array cost[], the task is to calculate the minimum number of operations required to make all elements of the array equal to 0, where at each operation decrement the value of an index i by 1. If the value of an index becomes 0, decrement the value of
6 min read
Minimize operations to reduce Array sum by half by reducing any elements by half
Given an array Arr[], the task is to find out the minimum number of operations to make the sum of array elements lesser or equal to half of its initial value. In one such operation, it is allowed to half the value of any array element. Examples: Input: Arr[] = [4, 6, 3, 9, 10, 2]Output: 5Explanation
5 min read
Minimize count of array elements to be removed such that at least K elements are equal to their index values
Given an array arr[](1- based indexing) consisting of N integers and a positive integer K, the task is to find the minimum number of array elements that must be removed such that at least K array elements are equal to their index values. If it is not possible to do so, then print -1. Examples: Input
13 min read
Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
Given an array arr[] of N elements. At any step, we can delete a number of a different parity from the just previous step, i.e., if, at the previous step, an odd number was deleted then delete an even number in the current step or vice versa. It is allowed to start by deleting any number. Deletion i
7 min read
Minimize cost of insertions and deletions required to make all array elements equal
Given a sorted array arr[] of size N (1 ? N ? 105) and two integers A and B, the task is to calculate the minimum cost required to make all array elements equal by increments or decrements. The cost of each increment and decrement are A and B respectively. Examples: Input: arr[] = { 2, 5, 6, 9, 10,
11 min read
Minimize deletion or insertions of Array elements such that arr[i] have frequency as its value
Given an array arr[] of length N, the task is to find the minimum number of operations to change the array such that each arr[i] occurs arr[i] times where in each operation either one element can be deleted from the array or one element can be inserted in the array. Examples: Input: N = 4, arr[ ] =
7 min read