Balance pans using given weights that are powers of a number | C ++ 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 to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isBalancePossible ( int T , int a ) { vector < int > baseForm ; while ( T ) { baseForm . push_back ( T % a ) ; T /= a ; } baseForm . push_back ( 0 ) ; for ( int i = 0 ; i < baseForm . size ( ) ; 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 ; } int main ( ) { int T = 11 ; int a = 4 ; bool balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) cout << " Balance ▁ is ▁ possible " << endl ; else cout << " Balance ▁ is ▁ not ▁ possible " << endl ; return 0 ; }