Because the problem statement says that 1\leq A_i\leq 10^9 and not0\leq A_i < n.
The blog states that the array contains elements from 0 to k-1. Would you not agree then that k should be 10^9 + 1 here?
Give me any test case where it fails
I already did that above.
1
3
1 4 7
The answer is 1, not 0 (which is what your code outputs).
Acc to your logic, if the answer is less than N-2, all elements must be equal…
so cant the number be like this
2 6 2 6 2 6
here there is no need to delete any element
please guide if i am wrong!
#include<bits/stdc++.h> #define ll long long #define pb push_back #define mod 1000000007 #define inf (1LL<<60) #define all(x) (x).begin(), (x).end() #define prDouble(x) cout << fixed << setprecision(10) << x #define triplet pair<ll,pair<ll,ll>> #define goog(tno) cout << “Case #” << tno <<": " #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) #define w(t) int t; cin>>t ; while(t–)
using namespace std;
void gshikhar84(){
fast_io;
}
int main() {
gshikhar84();
w(t){
ll int n;
cin>>n;
ll int arr[n];
for(ll int i = 0 ; i<n ; i++){
cin>>arr[i];
}
sort(arr , arr + n);
ll int count = 1;
ll int mx = INT_MIN;
for(ll int i = 0 ; i<n-1 ; i++){
if(arr[i] == arr[i+1]){
count++;
}
else{
mx = max(mx , count);
count = 1;
}
}
ll int ans;
if(mx == 1 && n > 1){
ans = n - 2;
}
else if(n == 1){
ans = 0;
}
else{
ans = n - mx;
}
cout<<ans<<endl;
}
return 0;
}
Can u plzz help me in finding my mistake. What I did is I simply sort the array now count the frequency of element with maximum frequency but if (n==1) print 0 , if count of maximum frequency = 1 and n > 1 print(n -2)…else i have printed n - mx where mx = frequency of max element.
You have too much pointless casework. Please go through the editorial and the code linked in it and use that to debug.
Also, please stop pasting your entire code in the comments. Just link your submission.
but what if the array was 1,2,3,4,5 so here max_freq = 1 so min(5-1, 5-2) = min(4,3) = 3
so, elements would be 1,2,3 but [1,2 & 2,1] = 1; [1,3 & 3,1] = 2 & [2,3 & 3,2] = 1 here not all are equal???