


You are given ‘ARR’ = [5, 2, 3]
In the first step, you can change 5 to 3, so the new array is [3, 2,3].
In the second step, you can change the 3 to 2, then the array is [2, 2,3].
In the third step, you can change the 3 to 2, then the array is [2, 2, 2]
Hence the answer is 3.
The first line of input contains the integer ‘T’ representing the number of test cases.
The first line of each test case contains one integer ‘N’, representing the size of the array.
The second line of each test case contains ‘N’ space-separated strings representing the element of the array.
For each test case, print a single integer representing the number of steps it will take to make all the array elements equal.
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^6
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the function.
In this approach, we will find the largest and equal the second largest element in the array. Each time we do this, we will increase the number of steps by 1. We will repeat the process until all the elements are in the array are not equal.
We will create a function isEqual(arr). This function returns true if all the elements in arr are equal. Otherwise, it will return false.
Algorithm:
In this approach, we will sort the array in decreasing order. Then we will count all the number of elements that are the largest in the array and add that number in the total steps to take. We will iterate to the last position of the second largest element in the array, and we will continue.
Example:
4 4 3 3 3 3 2 2 2 1 1, For this step, we add 2 to the total number of stops.
3 3 3 3 3 3 2 2 2 1 1, For this step, we add 6 to the total no of steps.
2 2 2 2 2 2 2 2 2 1 1, For this step, we add 9 to the total no. of steps.
We don’t need to change the elements in the array. So every time we find an element that is smaller than the previous, we add its position to the total number of steps.
Algorithm: