My issue
map<int,int>hash;
int ans=n;
for(int i=0;i<n;i++){cin>>arr[i];hash[arr[i]]++;}
for(auto i:hash){
ans=min(ans,(n-i.second)*i.first);
} why this approach is failing by setting value as n I already considered all values to be 1 and then iterated over all the available values can anyone think of edge case here iterating over all values is getting accepted
My code
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n,m;
cin>>n;
int arr[n];
map<int,int>hash;
int ans=n;
for(int i=0;i<n;i++){cin>>arr[i];hash[arr[i]]++;}
for(auto i:hash){
ans=min(ans,(n-i.second)*i.first);
}
cout<<ans<<"\n";
}
return 0;
}
Problem Link: Redundant Array Practice Coding Problem