Make all equal using Pairs CodeChef Solution

Problem – Make all equal using Pairs CodeChef Solution

Chef has an array A of length N.

In one operation, Chef can choose any two distinct indices i,j (1≤i,jN,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.

Input Format

  • First line will contain T, number of test cases. Then the test cases follow.
  • First line of each test case consists of an integer N – denoting the size of array A.
  • Second line of each test case consists of N space-separated integers A1​,A2​,…,AN​ – denoting the array A.

Output Format

For each test case, output the minimum number of operations required to make all the elements equal.

Constraints

  • 1≤T≤100
  • 2≤N≤1000
  • 1≤Ai​≤1000

Sample 1:

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

Explanation:

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].

Make all equal using Pairs CodeChef Solution in Java

/* 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);
		  
		}
	}
}

Make all equal using Pairs CodeChef Solution in C++17

#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;
}

Make all equal using Pairs CodeChef Solution in Pyth 3

# 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)        
Make all equal using Pairs CodeChef Solution Review:

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

Find on CodeChef

Conclusion:

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 >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *