[Solution] Make all equal using Pairs solution codechef

Make all equal using Pairs solution codechef – Chef has an array AA of length NN.

In one operation, Chef can choose any two distinct indices i,ji,j (1i,jN,ij)(1≤i,j≤N,i≠j) and either change AiAi to AjAj or change AjAj to AiAi.

Table of Contents

[Solution] Make all equal using Pairs solution codechef

Make all equal using Pairs solution codechef – Find the minimum number of operations required to make all the elements of the array equal.

Input Format

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

Output Format

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

Make all equal using Pairs solution codechef

  • 1T1001≤T≤100
  • 2N10002≤N≤1000
  • 1Ai10001≤Ai≤1000

Sample Input 1 

4
3
1 2 3
4
5 5 5 5
4
2 2 1 1
3
1 1 2

[Solution] Make all equal using Pairs solution codechef

 

2
0
2
1

Explanation

Test Case 11: You can make all the elements equal in 22 operations. In the first operation, you can choose indices 1,21,2 and convert A1A1 to A2A2. So the array becomes [2,2,3][2,2,3]. Now you can choose indices 1,31,3 and convert A3A3 to A1A1, so the final array becomes [2,2,2][2,2,2].

Test Case 22: Since all the elements are already equal there is no need to perform any operation.

Test Case 33: You can make all the elements equal in 22 operations. In the first operation, you can choose indices 1,31,3 and convert A1A1 to A3A3. So the array becomes [1,2,1,1][1,2,1,1]. Now you can choose indices 1,21,2 and convert A2A2 to A1A1, so the final array becomes [1,1,1,1][1,1,1,1].

Test Case 44: You can make all the elements equal in 11 operation. You can pick indices 2,32,3 and convert A3A3 to A2A2 after which the array becomes [1,1,1][1,1,1].

For Solution

Click Here

Leave a Comment