- Related Questions & Answers
- Print all integers that are sum of powers of two given numbers in C++
- Find maximum number that can be formed using digits of a given number in C++
- Assign weights to edges such that longest path in terms of weights is minimized in C++
- Count ways to express a number as sum of powers in C++
- Minimum number using set bits of a given number in C++
- Find all combinations that add upto given number using C++
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- Print a number strictly less than a given number such that all its digits are distinct in C++
- Minimum number of coins that make a given value
- Print all combinations of points that can compose a given number in C++
- C++ Program to Sum the digits of a given number
- C++ program to perform unique factorization of a Given Number
- C++ program to concatenate a string given number of times?
- That buzzword work life balance
- Program to count number of queries that are true in a graph with weighted path in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

#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; }

Balance is possible

Advertisements