Maximize the median of the given array after adding K elements to the same array | PHP implementation of the approach Function to return the maximized median ; Sort the array ; If size is even ; If size is odd ; Driver code
< ? php function getMaxMedian ( $ arr , $ n , $ k ) { $ size = $ n + $ k ; sort ( $ arr , $ n ) ; if ( $ size % 2 == 0 ) { $ median = ( float ) ( $ arr [ ( $ size / 2 ) - 1 ] + $ arr [ $ size / 2 ] ) / 2 ; return $ median ; } $ median = $ arr [ $ size / 2 ] ; return $ median ; } $ arr = array ( 3 , 2 , 3 , 4 , 2 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; echo ( getMaxMedian ( $ arr , $ n , $ k ) ) ;