# Balance pans using given weights that are powers of a number in C++ program

STATEMENT − Balance pans using given weights that are powers of a number.

DESCRIPTION − In this problem we are given a pan based weighing machine. we are given a weight T and some other weights whose values are powers of a number a. We need to balance the pans using the given weights.

Now, based on this we have this equation,

T + (some power of a) = (some other power of a)

Now, we should remember that there is exactly one weight corresponding to a power value.

Example,

T = 12 : a = 4

Using the below values, we can balance the weights as,

12 + 4 = 16

Now, we solve this problem we need to represent T in power of a. For this we will change the base of T to a from base 10

CASE 1 − On changing the base, if the representation has only 1’s and 0’s in the value. Then the weights of 1’s can be used to additively create the value of T.

Let’s take an example,

T = 10 : a = 3,

Changing the base of 10 to 3, the value becomes 101.

So the will be made using 30 and 32 (1 + 9) = 10.

CASE 2 − On changing the base, if the representation had only values other than the 1's and 0’s, balancing needs some more operation to be performed. Here, the mandatory condition for solution is the base conversion should have digit value of (a - 1). In this case, we will transfer the power of the value to T’s dide. And increase the number in base representation by 1.

Let’s take an example,

T = 7 : a = 3

On changing the base of 7 to 3 we will get 021.

Transfering 31to T’s side and increasing the other side by 1. We get the number = 10 which is represented as 101 i.e. (9 + 1). Which can be balanced.

Based on the above cases we will create a program to solve this problem.

## Example

#include <bits/stdc++.h>
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 = 21;
int a = 4;
if (isBalancePossible(T, a))
cout << "Balance is possible" << endl;
else
cout << "Balance is not possible" << endl;
return 0;
}

## Output

Balance is possible