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
function isBalancePossible ( T , a ) { let baseForm = [ ] ; let s = 0 ; while ( T > 0 ) { baseForm . push ( T % a ) ; T = Math . floor ( T / a ) ; s ++ ; } baseForm . push ( 0 ) ; for ( let i = 0 ; i < s ; 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 . splice ( i + 1 , 0 , baseForm [ i + 1 ] + 1 ) ; } } return true ; } let T = 11 ; let a = 4 ; let balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { document . write ( " " ) ; } else { document . write ( " " ) ; }