Find the modified array after performing k operations of given type | Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code
< ? php function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function removeMin ( & $ arr , $ n ) { $ minVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minVal = min ( $ minVal , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i ] - $ minVal ; } function removeFromMax ( & $ arr , $ n ) { $ maxVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ maxVal = max ( $ maxVal , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ maxVal - $ arr [ $ i ] ; } function modifyArray ( $ arr , $ n , $ k ) { if ( $ k % 2 == 0 ) removeMin ( $ arr , $ n ) ; else removeFromMax ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; } $ arr = array ( 4 , 8 , 12 , 16 ) ; $ n = count ( $ arr ) ; $ k = 2 ; modifyArray ( $ arr , $ n , $ k ) ; ? >