Making elements of two arrays same with minimum increment / decrement | PHP 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
< ? php function MinOperation ( $ a , $ b , $ n ) { sort ( $ a ) ; sort ( $ b ) ; $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ a [ $ i ] > $ b [ $ i ] ) $ result = $ result + abs ( $ a [ $ i ] - $ b [ $ i ] ) ; else if ( $ a [ $ i ] < $ b [ $ i ] ) $ result = $ result + abs ( $ a [ $ i ] - $ b [ $ i ] ) ; } return $ result ; } $ a = array ( 3 , 1 , 1 ) ; $ b = array ( 1 , 2 , 2 ) ; $ n = sizeof ( $ a ) ; echo MinOperation ( $ a , $ b , $ n ) ; ? >