Replace two consecutive equal values with one greater | Function to replace consecutive equal elements ; Index in result ; to print new array ; Driver Code
< ? php function replace_elements ( $ arr , $ n ) { $ pos = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ pos ++ ] = $ arr [ $ i ] ; while ( $ pos > 1 && $ arr [ $ pos - 2 ] == $ arr [ $ pos - 1 ] ) { $ pos -- ; $ arr [ $ pos - 1 ] ++ ; } } for ( $ i = 0 ; $ i < $ pos ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 6 , 4 , 3 , 4 , 3 , 3 , 5 ) ; $ n = count ( $ arr ) ; replace_elements ( $ arr , $ n ) ; ? >