Minimum decrement operations to make Array elements equal by only decreasing K each time | C # implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code
using System ; class GFG { static int INT_MAX = int . MaxValue ; 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 ( ) { int n , k ; n = 3 ; k = 3 ; int [ ] arr = { 12 , 9 , 15 } ; Console . WriteLine ( solve ( arr , n , k ) ) ; } }