1
+ /*
2
+ -> Problem Statement
3
+ From given integer Array Choose a set of integers and remove all the occurrences of these integers in the array.
4
+ You need to find the minimum size of that set such that at least half of the integers of the array are removed.
5
+
6
+ -> Approach Explanation:
7
+ - First save each integer present in array and its no of occurrences in array using map
8
+ - As we need to find minimum size of the set we will keep removing elements having maximum occurrences to make its size less than or equal to half
9
+ - For finding elements having maximum occurrences we will use heap (priority_queue or maxheap) which will be take occurrences from map
10
+ and will return in descending order , so that we can remove at least half elements from array using minimum set of imtegers
11
+ */
12
+
13
+ #include < iostream>
14
+ #include < map>
15
+ #include < queue>
16
+ using namespace std ;
17
+
18
+ class Solution {
19
+ public:
20
+
21
+ int minSetSize (vector<int >& arr) {
22
+
23
+ map<int ,int >mp;
24
+ for (int i=0 ;i<arr.size ();i++)
25
+ {
26
+ mp[arr[i]]++; // mapping integers and frequencies of integers
27
+ }
28
+
29
+ priority_queue<int >pq;
30
+
31
+ for (auto i : mp)
32
+ {
33
+ pq.push (i.second ); // frequencies from map are pushed in priority_queue
34
+ }
35
+
36
+ int remove_count = 0 ; // required count of integers in set, for which all occurrences should be removed
37
+ int currsize = arr.size ();
38
+
39
+ while (currsize > arr.size ()/2 )
40
+ {
41
+ int top = pq.top (); // getting max occurrence in current priority queue
42
+ currsize -= top; // removing all occurrences
43
+ pq.pop (); // removing element having max occurrence in current priority queue
44
+ remove_count ++; // increasing count since we have got one more element for that set
45
+
46
+ }
47
+
48
+ return remove_count; // returning final count of integers present in set, for which all occurrences should be removed
49
+
50
+ }
51
+ };
52
+
53
+ int main ()
54
+ {
55
+ cout<<" \n Enter number of test cases: " ;
56
+ int t; cin>>t; // no. of test cases
57
+
58
+ /*
59
+ Constraints:
60
+ 1 <= arr.length <= 105
61
+ arr's length is even.
62
+ 1 <= arr[i] <= 105
63
+ */
64
+
65
+ while (t--)
66
+ {
67
+ cout<<" \n\n Enter number of elements in array: " ;
68
+ int n; cin>>n;
69
+
70
+ vector<int > arr (n);
71
+
72
+ cout<<" \n Enter integers in array ( Input ): " ;
73
+ for (int i=0 ; i<n; i++)
74
+ cin>>arr[i];
75
+
76
+ Solution *ob;
77
+ cout<<" \n Minimum elements in set required to remove atleast half of the elements are :" ;
78
+ cout << ob-> minSetSize (arr);
79
+
80
+ }
81
+ return 0 ;
82
+ }
83
+
84
+ /*
85
+ SAMPLE INPUT
86
+ 2 // number of test cases
87
+ 6 // size of the array for test case:1
88
+ 7 3 4 3 3 4 // integers array / vector for test case:1
89
+ 10 // size of the array for test case:2
90
+ 5 5 5 4 4 5 1 2 2 4 // integers array / vector for test case:2
91
+
92
+ SAMPLE OUTPUT (excluding interactive instructions)
93
+ 1 ( for test case-1 only by removing occurrences of 1 element(i.e 3) array will be of half in size )
94
+ 2 ( for test case-2 we need to remove occurrences of atleast 2 elements (i.e. {5,4} or {5,2}) to make array of size <= half )
95
+
96
+ COMPLEXITY ANALYSIS : N= no.of cards in desk
97
+ Time : O(N) to iterate array/vector and save frequencies or .
98
+ O(NLogN) for creating maxheap or inserting elements in priority_que
99
+ Hence, O(NlogN) overall.
100
+ Space: O(N) for inputs and aux space: O(N) to re-create a queue.
101
+ Hence, O(N) overall.
102
+ */
0 commit comments