# Equal differences | codechef solution

## Problem :-

### Read problems statements in Mandarin Chinese, Russian, and Bengali as well.

You are given an array of $�$ integers. Find the minimum number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal.

### Input Format

• The first line of input contains a single integer $�$ denoting the number of test cases. The description of $�$ test cases follows.
• The first line of each test case contains an integer $�$.
• The second line of each test case contains $�$ space-separated integers ${�}_{1},{�}_{2},\dots ,{�}_{�}$.

### Output Format

For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition.

### Constraints

• $1\le �\le 1{0}^{4}$
• $1\le �\le 1{0}^{5}$
• $1\le {�}_{�}\le 1{0}^{9}$
• Sum of $�$ over all test cases does not exceed $5\cdot 1{0}^{5}$.

### Sample 1:

Input
Output
3
2
1 2
5
2 5 1 2 2
4
1 2 1 2
0
2
2

### Explanation:

Test case $1$: There is only one pair of integers and the absolute difference between them is $\mathrm{\mid }{�}_{1}-{�}_{2}\mathrm{\mid }=\mathrm{\mid }1-2\mathrm{\mid }=1$. So there is no need to delete any integer from the given array.

Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $\left[2,2,2\right]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.

Code(C++):-

#include <bits/stdc++.h>
using namespace std;
#define l long
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
l l int n;
cin>>n;
l l int s[n];
unordered_map<l l int,l l int>m;
for(l l int i=0;i<n;i++)
{
cin>>s[i];
m[s[i]]++;

}
long long int maax=0;
for(auto i:m)
{
if(maax<i.second)
maax=i.second;
}
if(n==1||n==2)
cout<<"0\n";
else if(maax!=1)
{
cout<<(n-maax)<<endl;

}
else cout<<(n-2)<<endl;

}
return 0;
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-