Balance pans using given weights that are powers of a number
Given a simple weighting scale with two pans, we are given a weight T and some other weights which are the powers of a specific number a, our goal is to balance these pans using the given weights. More formally we need to satisfy this equation,
T + (some power of a) = (some other powers of a)
Remember that we are given exactly one weight corresponding to one power.
Examples:
T = 11 and a = 4
Then scale can be balanced as,
11 + 4 + 1 = 16
We can see that in this problem our first goal is to represent T in terms of powers of a so when we write T with the base of a, if representation has only 1s and 0s then we know that weight corresponding to 1s can make the T. For example,
If T is 10 and a is 3 then representing 10 on
base of 3 we get 101 i.e. using 0th power of 3
and 2nd power of 3 (1 + 9) we can get 10
Now if all digits of base representation are not 1 or 0, then we can’t balance the scale, except the case when in base representation some digit is (a – 1) because in that case, we can transfer corresponding power to T’s side and increase the left number in base representation by 1. For example,
If T is 7 and a is 3 then representing 7 on base
3 we get 021.
Now while looping over representation from right
to left we get 2 (which is 3 - 1), in this case,
we can transfer corresponding power(3^1) to T’s
side and increase the left number by 1 i.e.,
T’s side 7 + 3 = 10 and representation 101 (9 + 1)
which has only 1s and 0s now, so scale can be balanced.
So now algorithm for solving this problem can be represented as follows, represent the T in base of a, if all digits of base representation are 1s or 0s, scale can be balanced otherwise loop from right side of the representation if any number is (a – 1) then increase left side number by 1, keep doing this and ignore 1, 0, (a – 1) and a cases in representation. If complete base representation is processed scale can be balanced otherwise not.
C++
#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 = 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;
}
|
Java
import java.util.*;
class GFG
{
static boolean isBalancePossible( int T, int a)
{
Vector<Integer> baseForm = new Vector<>();
int s = 0 ;
while (T > 0 )
{
baseForm.add(T % a);
T /= a;
s++;
}
baseForm.add( 0 );
for ( int i = 0 ; i < s; i++)
{
if (baseForm.get(i) != 0 &&
baseForm.get(i) != 1 &&
baseForm.get(i) != (a - 1 ) &&
baseForm.get(i) != a)
{
return false ;
}
if (baseForm.get(i) == a || baseForm.get(i) == (a - 1 ))
{
baseForm.add(i + 1 , baseForm.get(i + 1 ) + 1 );
}
}
return true ;
}
public static void main(String[] args)
{
int T = 11 ;
int a = 4 ;
boolean balancePossible = isBalancePossible(T, a);
if (balancePossible)
{
System.out.println( "Balance is possible" );
}
else
{
System.out.println( "Balance is not possible" );
}
}
}
|
Python3
def isBalancePossible(T,a):
baseForm = [];
s = 0 ;
while (T > 0 ):
baseForm.append(T % a);
T / / = a
s + = 1
baseForm.append( 0 );
for i in range (s):
if (baseForm[i] ! = 0 and baseForm[i] ! = 1 and baseForm[i] ! = (a - 1 ) and baseForm[i] ! = a) :
return False ;
if (baseForm[i] = = a or baseForm[i] = = (a - 1 )):
baseForm.insert(i + 1 , baseForm[i + 1 ] + 1 )
return True ;
T = 11 ;
a = 4 ;
balancePossible = isBalancePossible(T, a);
if (balancePossible):
print ( "Balance is possible" );
else :
print ( "Balance is not possible" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isBalancePossible( int T, int a)
{
List< int > baseForm = new List< int >();
int s = 0;
while (T > 0)
{
baseForm.Add(T % a);
T /= a;
s++;
}
baseForm.Add(0);
for ( int i = 0; i < s; 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.Insert(i + 1, baseForm[i+1] + 1);
}
}
return true ;
}
public static void Main()
{
int T = 11;
int a = 4;
bool balancePossible = isBalancePossible(T, a);
if (balancePossible)
{
Console.WriteLine( "Balance is possible" );
}
else
{
Console.WriteLine( "Balance is not possible" );
}
}
}
|
PHP
<?php
function isBalancePossible( $T , $a )
{
$baseForm = array ();
while ( $T )
{
array_push ( $baseForm , $T % $a );
$T = (int)( $T / $a );
}
array_push ( $baseForm , 0);
for ( $i = 0; $i < count ( $baseForm ); $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;
}
$T = 11;
$a = 4;
$balancePossible = isBalancePossible( $T , $a );
if ( $balancePossible )
echo "Balance is possible\n" ;
else
echo "Balance is not possible\n" ;
?>
|
Javascript
<script>
function isBalancePossible(T,a)
{
let baseForm = [];
let s = 0;
while (T > 0)
{
baseForm.push(T % a);
T = Math.floor(T/a);
s++;
}
baseForm.push(0);
for (let i = 0; i < s; 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.splice(i + 1,0, baseForm[i+1] + 1);
}
}
return true ;
}
let T = 11;
let a = 4;
let balancePossible = isBalancePossible(T, a);
if (balancePossible)
{
document.write( "Balance is possible" );
}
else
{
document.write( "Balance is not possible" );
}
</script>
|
Output:
Balance is possible
Last Updated :
22 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...