

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Questions & Answers
- Print all integers that are sum of powers of two given numbers in C++
- C++ Representation of a Number in Powers of Other
- Program to check whether number is a sum of powers of three in Python
- 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++
- Find maximum number that can be formed using digits of a given number in C++
- Program to find number of subsequences that satisfy the given sum condition using Python
- Prepare trial balance using total and balance method for the given data
- What are the Input Powers of a Synchronous Motor?
- What are the Output Powers of a Synchronous Motor?
- Minimum number of coins that make a given value
- That buzzword work life balance
- Program find number of subsets of colorful vertices that meets given conditions in Python
- Program to count number of unique paths that includes given edges in Python
- Count ways to express a number as sum of powers in C++