Making elements of two arrays same with minimum increment / decrement | C # program to find minimum increment / decrement operations to make array elements same . ; sorting both arrays in ascending order ; variable to store the final result ; After sorting both arrays Now each array is in non - decreasing order . Thus , we will now compare each element of the array and do the increment or decrement operation depending upon the value of array b [ ] . ; Driver code
using System ; public class GFG { static int MinOperation ( int [ ] a , int [ ] b , int n ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] > b [ i ] ) result = result + Math . Abs ( a [ i ] - b [ i ] ) ; else if ( a [ i ] < b [ i ] ) result = result + Math . Abs ( a [ i ] - b [ i ] ) ; } return result ; } public static void Main ( ) { int [ ] a = { 3 , 1 , 1 } ; int [ ] b = { 1 , 2 , 2 } ; int n = a . Length ; Console . WriteLine ( MinOperation ( a , b , n ) ) ; } }