Find minimum number of merge operations to make an array palindrome | Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver Code
< ? php function findMinOps ( $ arr , $ n ) { $ ans = 1 ; for ( $ i = 0 , $ j = $ n - 1 ; $ i <= $ j { if ( $ arr [ $ i ] == $ arr [ $ j ] ) { $ i ++ ; $ j -- ; } else if ( $ arr [ $ i ] > $ arr [ $ j ] ) { $ j -- ; $ arr [ $ j ] += $ arr [ $ j + 1 ] ; $ ans ++ ; } else { $ i ++ ; $ arr [ $ i ] += $ arr [ $ i - 1 ] ; $ ans ++ ; } } return $ ans ; } $ arr [ ] = array ( 1 , 4 , 5 , 9 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " , findMinOps ( $ arr , $ n ) ; ? >