Minimum decrement operations to make Array elements equal by only decreasing K each time | Java implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code
class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int solve ( int arr [ ] , int n , int k ) { int minx = INT_MAX ; int i ; for ( i = 0 ; i < n ; i ++ ) { minx = Math . min ( minx , arr [ i ] ) ; } int decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return - 1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } public static void main ( String [ ] args ) { int n , k ; n = 3 ; k = 3 ; int arr [ ] = { 12 , 9 , 15 } ; System . out . println ( solve ( arr , n , k ) ) ; } }