Make Array Elements Equal Problem Statement
Given an integer array, your objective is to change all elements to the same value, minimizing the cost. The cost of changing an element from x
to y
is defined as |x - y|
. Determine the minimum possible cost to achieve this goal.
Example:
Input:
ARR = [3, 4, 5]
Output:
2
Explanation:
If we convert all elements to the value 4, the cost for changing 3 to 4 is |3 - 4|
which is 1, and changing 5 to 4 is |5 - 4|
which is 1. Total cost is 1 + 1 = 2, which is minimal.
Input:
The first line contains an integer ‘T’, the number of test cases. Each test case includes:
An integer ‘N’, the number of elements in the array.
A line with ‘N’ space-separated integers representing the array elements.
Output:
For each test case, output the minimum cost to make all elements of the array equal. Print each test case output on a new line.
Constraints:
1 <= T <= 5
1 <= N <= 10^5
1 <= ARR[i] <= 10^8
- Time limit: 1 second
Be the first one to answer
Add answer anonymously...
Top Publicis Sapient Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Publicis Sapient Software Developer
>
Publicis Sapient Software Developer Interview Questions
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