-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Distinct Ids.cpp
62 lines (47 loc) · 1.18 KB
/
Minimum Distinct Ids.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Minimum Distinct Ids.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;
// Function to find distintc id's
int distinctIds(int arr[], int n, int mi)
{
unordered_map<int, int> m;
vector<pair<int, int> > v;
int count = 0;
// Store the occurrence of ids
for (int i = 0; i < n; i++)
m[arr[i]]++;
// Store into the vector second as first and vice-versa
for (auto it = m.begin(); it != m.end(); it++)
v.push_back(make_pair(it->second, it->first));
// Sort the vector
sort(v.begin(), v.end());
int size = v.size();
// Start removing elements from the beginning
for (int i = 0; i < size; i++) {
// Remove if current value is less than
// or equal to mi
if (v[i].first <= mi) {
mi -= v[i].first;
count++;
}
// Return the remaining size
else
return size - count;
}
return size - count;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 1, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int m = 2;
cout << distinctIds(arr, n, m);
return 0;
}