Balance pans using given weights that are powers of a number | method returns true if balancing of scale is possible ; baseForm vector will store T 's  representation on base a in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver Code
< ? php function isBalancePossible ( $ T , $ a ) { $ baseForm = array ( ) ; while ( $ T ) { array_push ( $ baseForm , $ T % $ a ) ; $ T = ( int ) ( $ T / $ a ) ; } array_push ( $ baseForm , 0 ) ; for ( $ i = 0 ; $ i < count ( $ baseForm ) ; $ i ++ ) { if ( $ baseForm [ $ i ] != 0 && $ baseForm [ $ i ] != 1 && $ baseForm [ $ i ] != ( $ a - 1 ) && $ baseForm [ $ i ] != $ a ) return false ; if ( $ baseForm [ $ i ] == $ a || $ baseForm [ $ i ] == ( $ a - 1 ) ) $ baseForm [ $ i + 1 ] += 1 ; } return true ; } $ T = 11 ; $ a = 4 ; $ balancePossible = isBalancePossible ( $ T , $ a ) ; if ( $ balancePossible ) echo " Balance ▁ is ▁ possible STRNEWLINE " ; else echo " Balance ▁ is ▁ not ▁ possible STRNEWLINE " ; ? >