Minimum Operations
You are given an array 'ARR' of 'N' positive integers. You need to find the minimum number of operations needed to make all elements of the array equal. You can perform addition, multiplication, subtraction or division with any element on an array element.
Addition, Subtraction, Multiplication or Division on any element of the array will be considered as a single operation.
Example:
If the given array is [1,2,3] then the answer would be 2. One of the ways to make all the elements of the given array equal is by adding 1 to the array element with value 1 and subtracting 1 from the array element with value 3. So that final array would become [2,2,2].
Input Format:
The first line of input contains a single integer 'T', representing the number of test cases.
The first line of input contains an integer 'N' representing the length of the array.
The second line contains 'N' single space-separated integers representing elements of the array 'ARR'.
Output Format:
For each test case, return an integer in a single line i.e. the minimum number of operations required to make all the elements of the array equal.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^5
0 <= ARR[i] <= 10^5
Where 'ARR[i]' is the element of the array 'ARR' at index 'i'.
Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
For making all elements equal you can select a target value and then you can make all elements equal to that. Now, for converting a single element to a target value you can perform a single...read more
CodingNinjas
author
2y
Sort the array
For making all elements equal you can select a target value and then you can make all elements equal to that. Now, for converting a single element to a target value you can perform a sin...read more
CodingNinjas
author
2y
Optimal Approach
For making all elements equal you can select a target value and then you can make all elements equal to that. Now, for converting a single element to a target value you can perform a s...read more
Add answer anonymously...
Top TCS SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app