Problem
You are given a sequence . You want all the elements of the sequence to be equal. In order to achieve that, you may perform zero or more moves. In each move, you must choose an index (), then choose or (it is not allowed to choose or ) and change the value of to — in other words, you should replace the value of one element of the sequence by one of its adjacent elements.
What is the minimum number of moves you need to make in order to make all the elements of the sequence equal?
Input
- The first line of the 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 a single integer .
- The second line contains space-separated integers .
Output
For each test case, print a single line containing one integer — the minimum required number of moves.
Constraints
- for each valid
Sample 1:
3 5 1 1 1 1 1 4 9 8 1 8 2 1 9
0 2 1
Explanation:
Example case 1: No moves are needed since all the elements are already equal.
Example case 3: We can perform one move on either or .
Program :
#include <stdio.h>
int main(void) {
// your code goes here
int t;
scanf("%d",&t);
while(t--)
{
int n,i,m;
scanf("%d",&n);
int arr[n+1];
int c[102]={0};
m=c[0];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
c[arr[i]]++;
m<c[arr[i]]?m=c[arr[i]]:m;
}
printf("%d\n",n-m);
}
return 0;
}
0 Comments