Check if a number N can be expressed in base B
Last Updated :
16 Apr, 2021
Given a number N and any base B. The task is to check if N can be expressed in the form a1*b0 + a2*b1 + a3*b2 + ….+ a101*b100 where each coefficients a1, a2, a3…a101 are either 0, 1 or -1.
Examples:
Input: B = 3, N = 7
Output: Yes
Explanation:
The number 7 can be expressed as 1 * 30 + (-1) * 31 + 1 * 32 = 1 – 3 + 9.
Input: B = 100, N = 50
Output: No
Explanation:
There is no possible way to express the number.
Approach: Below are the steps:
- If the base B representation of N consists of only 0s and 1s then the answer exists.
- If the above condition isn’t satisfied, then iterate from lower digit to higher ones and if the digit is not equal to 0 or 1, then try to subtract the appropriate power of B from it and increment higher digit.
- If it becomes equal to -1, then we can subtract this power digit, if it becomes equal to 0, then simply ignore, in other cases, representing the number in the required form is not possible.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int n, int w)
{
vector< int > a(105);
int p = 0;
while (n > 0) {
a[p++] = n % w;
n /= w;
}
bool flag = true ;
for ( int i = 0; i <= 100; i++) {
if (a[i] == 0 || a[i] == 1)
continue ;
else if (a[i] == w
|| a[i] == w - 1)
a[i + 1]++;
else
flag = false ;
}
return flag;
}
int main()
{
int B = 3, N = 7;
if (check(N, B))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean check( int n, int w)
{
int [] a = new int [ 105 ];
int p = 0 ;
while (n > 0 )
{
a[p++] = n % w;
n /= w;
}
boolean flag = true ;
for ( int i = 0 ; i <= 100 ; i++)
{
if (a[i] == 0 || a[i] == 1 )
continue ;
else if (a[i] == w || a[i] == w - 1 )
a[i + 1 ]++;
else
flag = false ;
}
return flag;
}
public static void main(String[] args)
{
int B = 3 , N = 7 ;
if (check(N, B))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def check(n, w):
a = [ 0 for i in range ( 105 )];
p = 0
while (n > 0 ):
a[p] = n % w
p + = 1
n / / = w
flag = True
for i in range ( 101 ):
if (a[i] = = 0 or a[i] = = 1 ):
continue
elif (a[i] = = w or a[i] = = w - 1 ):
a[i + 1 ] + = 1
else :
flag = False
return flag
if __name__ = = "__main__" :
B = 3
N = 7
if (check(N, B)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool check( int n, int w)
{
int [] a = new int [105];
int p = 0;
while (n > 0)
{
a[p++] = n % w;
n /= w;
}
bool flag = true ;
for ( int i = 0; i <= 100; i++)
{
if (a[i] == 0 || a[i] == 1)
continue ;
else if (a[i] == w || a[i] == w - 1)
a[i + 1]++;
else
flag = false ;
}
return flag;
}
public static void Main(String[] args)
{
int B = 3, N = 7;
if (check(N, B))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function check(n , w) {
var a = Array(105).fill(0);
var p = 0;
while (n > 0) {
a[p++] = n % w;
n /= w;
n = parseInt(n);
}
var flag = true ;
for (i = 0; i <= 100; i++) {
if (a[i] == 0 || a[i] == 1)
continue ;
else if (a[i] == w || a[i] == w - 1)
a[i + 1]++;
else
flag = false ;
}
return flag;
}
var B = 3, N = 7;
if (check(N, B))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(logBN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...