Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Chef has an array A of length N.
In one operation, Chef can choose any two distinct indices i,j (1≤i,j≤N,i=j) and either change Ai to Aj or change Aj to Ai.
Find the minimum number of operations required to make all the elements of the array equal.
For each test case, output the minimum number of operations required to make all the elements equal.
Input:
4
3
1 2 3
4
5 5 5 5
4
2 2 1 1
3
1 1 2
Output:
2
0
2
1
Test Case 1: You can make all the elements equal in 2 operations. In the first operation, you can choose indices 1,2 and convert A1 to A2. So the array becomes [2,2,3]. Now you can choose indices 1,3 and convert A3 to A1, so the final array becomes [2,2,2].
Test Case 2: Since all the elements are already equal there is no need to perform any operation.
Test Case 3: You can make all the elements equal in 2 operations. In the first operation, you can choose indices 1,3 and convert A1 to A3. So the array becomes [1,2,1,1]. Now you can choose indices 1,2 and convert A2 to A1, so the final array becomes [1,1,1,1].
Test Case 4: You can make all the elements equal in 1 operation. You can pick indices 2,3 and convert A3 to A2 after which the array becomes [1,1,1].
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n =sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int count=0;
for(int i=0;i<n;i++){
int a=0;
for(int j=0;j<n;j++){
if(arr[i]==arr[j])
a++;
}
count=Math.max(a,count);
}
System.out.println(n-count);
}
}
}
#include <iostream>
using namespace std;
int main() {
// your code goes here
int T;
std::cin >> T;
while(T--){
int N;
cin>>N;
int A[N];
for( int i=0; i<N; i++){
cin>>A[i];
}
int min=0,count=0;
for(int i=0; i<N; i++){
int count=0;
for(int j=i; j<N; j++){
if(A[i]==A[j]){
count++;
}
if(min<count){
min=count;
}
}
}
std::cout << (N-min) << std::endl;
}
return 0;
}
# cook your dish here
for _ in range(int(input())):
n=int(input());
arr = list(map(int,input().split()));
count=0;
for i in arr:
if arr.count(i)>count:
count = arr.count(i)
print(n-count)
In our experience, we suggest you solve this Make all equal using Pairs CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Make all equal using Pairs CodeChef Solution
I hope this Make all equal using Pairs CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>