Count minimum steps to get the given desired array | Returns count of minimum operations to convert a zero array to target array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert target to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of target don 't become 0. ; To store count of zeroes in current target array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver Code
< ? php function countMinOperations ( $ target , $ n ) { $ result = 0 ; while ( 1 ) { $ zero_count = 0 ; $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ target [ $ i ] & 1 ) break ; else if ( $ target [ $ i ] == 0 ) $ zero_count ++ ; } if ( $ zero_count == $ n ) return $ result ; if ( $ i == $ n ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ target [ $ j ] = $ target [ $ j ] / 2 ; $ result ++ ; } for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( $ target [ $ j ] & 1 ) { $ target [ $ j ] -- ; $ result ++ ; } } } } $ arr = array ( 16 , 16 , 16 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to ▁ STRNEWLINE " . " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " . countMinOperations ( $ arr , $ n ) ; ? >