Skip to content

Commit 5b06a37

Browse files
authored
Merge pull request kothariji#761 from Vamshikrishna-D/patch-2
added equal difference in c++
2 parents 039c679 + 7bb9053 commit 5b06a37

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

C-or-CPP Projects/EqualDifference.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int main() {
6+
// your code goes here
7+
int t;
8+
cin>>t;
9+
while(t--)
10+
{
11+
int n;
12+
cin>>n;
13+
vector<int> a(n);
14+
unordered_map<int,int> m;
15+
for(int i=0;i<n; i++)
16+
{
17+
cin>>a[i];
18+
m[a[i]]++;
19+
}
20+
if(n<=2)
21+
{
22+
cout<<0<<endl;
23+
}
24+
else
25+
{
26+
int maxi = INT_MIN;
27+
for(auto it = m.begin(); it!= m.end(); it++)
28+
{
29+
if(it->second> maxi)
30+
{
31+
maxi= max(maxi,it->second);
32+
}
33+
}
34+
int ans= min(n-2, n-maxi);
35+
cout<<ans<<endl;
36+
}
37+
}
38+
return 0;
39+
}

0 commit comments

Comments
 (0)