Balance pans using given weights that are powers of a number | Java code to check whether scale can be balanced or not ; 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
import java . util . * ; class GFG { static boolean isBalancePossible ( int T , int a ) { Vector < Integer > baseForm = new Vector < > ( ) ; int s = 0 ; while ( T > 0 ) { baseForm . add ( T % a ) ; T /= a ; s ++ ; } baseForm . add ( 0 ) ; for ( int i = 0 ; i < s ; i ++ ) { if ( baseForm . get ( i ) != 0 && baseForm . get ( i ) != 1 && baseForm . get ( i ) != ( a - 1 ) && baseForm . get ( i ) != a ) { return false ; } if ( baseForm . get ( i ) == a || baseForm . get ( i ) == ( a - 1 ) ) { baseForm . add ( i + 1 , baseForm . get ( i + 1 ) + 1 ) ; } } return true ; } public static void main ( String [ ] args ) { int T = 11 ; int a = 4 ; boolean balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { System . out . println ( " Balance ▁ is ▁ possible " ) ; } else { System . out . println ( " Balance ▁ is ▁ not ▁ possible " ) ; } } }