Similar Heights solution codechef-

Similar Heights solution codechef

Chef is teaching his class of NN students at Hogwarts. He groups students with the same height together for an activity. Some of the students end up in a groups with only themselves and are saddened by this.

With the help of his magic wand, Chef can increase the height of any student to any value he likes. Now Chef wonders, what is the minimum number of students whose height needs to be increased so that there are no sad students?

Input Format

  • The first line of input will contain a single integer TT, denoting the number of test cases.
  • Each test case consists of two lines of input.
    • The first line of each test case contains one integer NN — the number of students.
    • The second line consists of NN space-separated integers H1,H2,,HNH1,H2,…,HN denoting the heights of the students.

Output Format

For each test case, output on a single line the minimum number of students whose heights must to be increased.


  • 1T51041≤T≤5⋅104
  • 2N1052≤N≤105
  • 1Hi1091≤Hi≤109
  • The sum of NN over all test cases won’t exceed 31053⋅105.

Sample Input 1 

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

Sample Output 1 



Test case 11: The students form 22 groups each having 22 students so no change of heights is required.

Test case 22: Initially the student with height 11 cannot be paired with anyone else. Chef can increase his height to 22 and now there is only 11 group with all the 44 students in it.

Test case 33: All students have same height and hence no change of heights is required.

Test case 44: One possible way is to increase the height of the first student to 55 and the heights of the second and third students to 44, hence forming one group of 22 students and one of 33 students. In total, the heights of 33 students need to be changed.

Solution – CLICK HERE

Leave a Comment