Given an array which contains integer values, we need to make all values of this array equal to some integer value with minimum cost where the cost of changing an array value x to y is abs(x-y).
Input : arr = [1, 100, 101] Output : 100 We can change all its values to 100 with minimum cost, |1 - 100| + |100 - 100| + |101 - 100| = 100 Input : arr = [4, 6] Output : 2 We can change all its values to 5 with minimum cost, |4 - 5| + |5 - 6| = 2
This problem can be solved by observing the cost while changing the target equal value, i.e. we will see the change in cost when target equal value is changed. It can be observed that, as we increase the target equal value the total cost decreases up to a limit and then starts increasing i.e. the cost graph with respect to target equal value is of U-shape and as cost graph is in U-shape, the ternary search can be applied to this search space and our goal is to get that bottom most point of the curve which will represent the smallest cost. We will make smallest and largest value of the array as the limit of our search space and then we will keep skipping 1/3 part of the search space until we reach to the bottom most point of our U-curve.
Please see below code for better understanding,
Think geometrically. Assume that array elements are co-ordinates on x axis. The problem reduces to finding another co-ordinate such that the sum of distances between this choice and other co-ordinates is minimized.
Observe that: If number of coordinates are odd then y = middle element. If even then y is any number in between middle 2 co-ordinates. Say Input = [a, b, c, d]. Output is any number between b and c including both. Hence the cost is sum which can be computed easily now that we have chosen y. sum|(y-ai)| for all i.
It is really easy to code it.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cost to make all array elements equal
- Make array elements equal with minimum cost
- Minimum Cost to make all array elements equal using given operations
- Minimum cost to equal all elements of array using two operation
- Minimum operation to make all elements equal in array
- Minimum operations to make all elements equal using the second array
- Minimum number of steps to make all elements of array equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum operations required to make all the array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum number of increment-other operations to make all array elements equal.
- Minimum elements to be inserted in Array to make adjacent differences equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Find the minimum number of operations required to make all array elements equal
- Minimum cost to make an Array a permutation of first N natural numbers
- Minimum cost to make array size 1 by removing larger of pairs
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimum number of moves to make all elements equal