- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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 3^{0} and 3^{2} (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 3^{1}to 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

- Related Articles
- Form a Number Using Corner Digits of Powers
- Print all integers that are sum of powers of two given numbers in C++
- Program to check whether number is a sum of powers of three in Python
- C++ Representation of a Number in Powers of Other
- Complete the following table:Find the total number of persons whose weights are given in the above table."\n
- Program to find number of subsequences that satisfy the given sum condition using Python
- Find maximum number that can be formed using digits of a given number in C++
- Prepare trial balance using total and balance method for the given data
- Sorting an array that contains the value of some weights using JavaScript
- Assign weights to edges such that longest path in terms of weights is minimized in C++
- PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’
- Python program to count number of vowels using set in a given string
- Java program to find the factorial of a given number using recursion
- Java program to calculate the power of a Given number using recursion
- Java program to calculate the GCD of a given number using recursion