Minimum sum of values subtracted from array elements to make all array elements equal | Java program for the above approach ; Function to find the sum of values removed to make all array elements equal ; Stores the minimum of the array ; Stores required sum ; Traverse the array ; Add the value subtracted from the current element ; Return the total sum ; Driver Code ; Function Call
import java . util . Arrays ; class GFG { static int minValue ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int minimum = arr [ 0 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] - minimum ) ; } return sum ; } static public void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . length ; System . out . println ( minValue ( arr , N ) ) ; } }