Check if a given number can be represented in given a no. of digits in any base
Given a number and no. of digits to represent the number, find if the given number can be represented in given no. of digits in any base from 2 to 32.
Examples :
Input: 8 4
Output: Yes
Possible in base 2 as 8 in base 2 is 1000
Input: 8 2
Output: Yes
Possible in base 3 as 8 in base 3 is 22
Input: 8 3
Output: No
Not possible in any base
We strongly recommend you minimize your browser and try this yourself first.
The idea is to check all bases one by one starting from base 2 to base 32. How do we check for a given base? Following are simple steps.
1) IF the number is smaller than the base and the digit is 1, then return true.
2) Else if the digit is more than 1 and the number is more than base, then remove the last digit from num by doing num/base, reduce the number of digits and recur.
3) Else return false
Below is the implementation of the above idea.
C++
#include <iostream>
using namespace std;
bool checkUtil( int num, int dig, int base)
{
if (dig==1 && num < base)
return true ;
if (dig > 1 && num >= base)
return checkUtil(num/base, --dig, base);
return false ;
}
bool check( int num, int dig)
{
for ( int base=2; base<=32; base++)
if (checkUtil(num, dig, base))
return true ;
return false ;
}
int main()
{
int num = 8;
int dig = 3;
(check(num, dig))? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static boolean checkUtil( int num, int dig, int base)
{
if (dig== 1 && num < base)
return true ;
if (dig > 1 && num >= base)
return checkUtil(num / base, --dig, base);
return false ;
}
static boolean check( int num, int dig)
{
for ( int base = 2 ; base <= 32 ; base++)
if (checkUtil(num, dig, base))
return true ;
return false ;
}
public static void main (String[] args)
{
int num = 8 ;
int dig = 3 ;
if (check(num, dig))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkUtil(num,dig,base):
if (dig = = 1 and num < base):
return True
if (dig > 1 and num > = base):
return checkUtil(num / base, - - dig, base)
return False
def check(num,dig):
for base in range ( 2 , 33 ):
if (checkUtil(num, dig, base)):
return True
return False
num = 8
dig = 3
if (check(num, dig) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkUtil( int num, int dig,
int i)
{
if (dig == 1 && num < i)
return true ;
if (dig > 1 && num >= i)
return checkUtil((num / i), --dig, i);
return false ;
}
static bool check( int num, int dig)
{
for ( int i = 2; i <= 32; i++)
if (checkUtil(num, dig, i))
return true ;
return false ;
}
public static void Main()
{
int num = 8;
int dig = 3;
if (check(num, dig))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkUtil( $num , $dig , $base )
{
if ( $dig == 1 && $num < $base )
return true;
if ( $dig > 1 && $num >= $base )
return checkUtil( $num / $base ,
-- $dig , $base );
return false;
}
function check( $num , $dig )
{
for ( $base = 2; $base <= 32; $base ++)
if (checkUtil( $num , $dig , $base ))
return true;
return false;
}
$num = 8;
$dig = 3;
if (check( $num , $dig ) == true)
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkUtil(num , dig , base)
{
if (dig == 1 && num < base)
return true ;
if (dig > 1 && num >= base)
return checkUtil(parseInt(num / base), --dig, base);
return false ;
}
function check(num , dig) {
for (base = 2; base <= 32; base++)
if (checkUtil(num, dig, base))
return true ;
return false ;
}
var num = 8;
var dig = 3;
if (check(num, dig))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
No
Time Complexity: O(32*32)
Auxiliary Space: O(1)
Optimized Approach:
we can optimize this program by adding a break statement in the check function to stop checking for bases once the number is found to be representable in a given number of digits in a particular base.
Here is a step-by-step approach to implement the above code:
Define the checkUtil function:
a. If dig is 1 and num is less than base, return true.
b. If dig is greater than 1 and num is greater than or equal to base, divide num by base and decrement dig by 1.
c. Recursively call checkUtil with the updated num, dig, and base.
d. If the recursive call returns true, return true.
e. Otherwise, return false.
Define the check function:
a. Iterate through all bases from 2 to 32:
i. Call checkUtil with the current num, dig, and base.
ii. If checkUtil returns true, return true.
iii. If the base is greater than or equal to num, break out of the loop.
b. If the loop completes without finding a suitable base, return false.
Define the main function:
a. Set num and dig to the desired values.
b. Call the check function with num and dig.
c. If check returns true, print “Yes” to the console.
d. Otherwise, print “No” to the console.
Compile and run the program to test it with various inputs.
Here’s the optimized version of the program:
C++
#include <iostream>
using namespace std;
bool checkUtil( int num, int dig, int base)
{
if (dig == 1 && num < base)
return true ;
if (dig > 1 && num >= base)
return checkUtil(num / base, --dig, base);
return false ;
}
bool check( int num, int dig)
{
for ( int base = 2; base <= 32; base++) {
if (checkUtil(num, dig, base))
return true ;
if (base >= num)
break ;
}
return false ;
}
int main()
{
int num = 8;
int dig = 3;
(check(num, dig)) ? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
public class Main {
public static boolean checkUtil( int num, int dig, int base) {
if (dig == 1 && num < base)
return true ;
if (dig > 1 && num >= base)
return checkUtil(num / base, --dig, base);
return false ;
}
public static boolean check( int num, int dig) {
for ( int base = 2 ; base <= 32 ; base++) {
if (checkUtil(num, dig, base))
return true ;
if (base >= num)
break ;
}
return false ;
}
public static void main(String[] args) {
int num = 8 ;
int dig = 3 ;
System.out.println(check(num, dig) ? "Yes" : "No" );
}
}
|
Python3
def check_util(num, dig, base):
if dig = = 1 and num < base:
return True
if dig > 1 and num > = base:
return check_util(num / / base, dig - 1 , base)
return False
def check(num, dig):
for base in range ( 2 , 33 ):
if check_util(num, dig, base):
return True
if base > = num:
break
return False
num = 8
dig = 3
print ( "Yes" if check(num, dig) else "No" )
|
C#
using System;
public class Mainn
{
public static bool checkUtil( int num, int dig, int baseVal)
{
if (dig == 1 && num < baseVal)
return true ;
if (dig > 1 && num >= baseVal)
return checkUtil(num / baseVal, --dig, baseVal);
return false ;
}
public static bool check( int num, int dig)
{
for ( int baseVal = 2; baseVal <= 32; baseVal++)
{
if (checkUtil(num, dig, baseVal))
return true ;
if (baseVal >= num)
break ;
}
return false ;
}
public static void Main( string [] args)
{
int num = 8;
int dig = 3;
Console.WriteLine(check(num, dig) ? "Yes" : "No" );
}
}
|
Javascript
function check_util(num, dig, base) {
if (dig === 1 && num < base)
return true ;
if (dig > 1 && num >= base)
return check_util(Math.floor(num / base), dig - 1, base);
return false ;
}
function check(num, dig) {
for (let base = 2; base <= 32; base++) {
if (check_util(num, dig, base))
return true ;
if (base >= num)
break ;
}
return false ;
}
const num = 8;
const dig = 3;
console.log(check(num, dig) ? "Yes" : "No" );
|
Output :
No
Time Complexity: O(d * (log n)^2) ,where n is the given number and d is the number of digits we want to check.
Auxiliary Space: O(1)
Last Updated :
27 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...