Given two integers n and m, find if there exists a base b such that 2 ⤠b ⤠32 and the representation of n in base b contains exactly m digits.
ReturnĀ trueĀ if such a base exists, otherwise returnĀ false.
Examples :Ā
Input: n = 8, m = 4
Output: true
Explanation: In base 2, the number 8 is represented as 1000, which contains exactly 4 digits.
Input: n = 8, m = 2
Output: true
Explanation: In base 3, the number 8 is represented as 22, which contains exactly 2 digits.
Input: n = 8, m = 3
Output: false
Explanation: There is no base from 2 to 32 in which the representation of 8 contains exactly 3 digits.
Table of Content
[Naive Approach] Using Recursive Approach - O(log n) Time and O(log n) Space
The number of digits needed to represent a number depends on the chosen base. By repeatedly dividing the number by a base, we effectively remove one digit at a time from its representation. If after removing m ā 1 digits the remaining value is less than the base, then the number is represented using exactly m digits in that base.
- Iterate through every base from 2 to 32.
- For each base, recursively remove the last digit by dividing the number by the base.
- Decrease the required digit count by 1 after each division.
- When only one digit is left to be placed, check if the remaining number is smaller than the current base.
- If the condition is satisfied for any base, return true otherwise, return false.
#include <iostream>
using namespace std;
// Returns true if 'n' can be represented using exactly
// 'm' digits in the given base.
bool checkUtil(int n, int m, int base)
{
// If only one digit is left, the number must be
// smaller than the base.
if (m == 1)
return (n < base);
// Remove the last digit and check the remaining part.
if (n >= base)
return checkUtil(n / base, m - 1, base);
return false;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
bool baseEquivalent(int n, int m)
{
for (int base = 2; base <= 32; base++)
{
if (checkUtil(n, m, base))
return true;
}
return false;
}
int main()
{
int n = 8;
int m = 4;
cout << (baseEquivalent(n, m) ? "true" : "false");
return 0;
}
class GFG {
// Returns true if 'n' can be represented using exactly
// 'm' digits in the given base.
static boolean checkUtil(int n, int m, int base)
{
// If only one digit is left, the number must be
// smaller than the base.
if (m == 1)
return n < base;
// Remove the last digit and check the remaining
// part.
if (n >= base)
return checkUtil(n / base, m - 1, base);
return false;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
static boolean baseEquivalent(int n, int m)
{
for (int base = 2; base <= 32; base++) {
if (checkUtil(n, m, base))
return true;
}
return false;
}
public static void main(String[] args)
{
int n = 8;
int m = 4;
System.out.println(baseEquivalent(n, m));
}
}
# Returns True if 'n' can be represented using exactly
# 'm' digits in the given base.
def checkUtil(n, m, base):
# If only one digit is left, the number must be
# smaller than the base.
if m == 1:
return n < base
# Remove the last digit and check the remaining part.
if n >= base:
return checkUtil(n // base, m - 1, base)
return False
# Returns True if 'n' can be represented using exactly
# 'm' digits in any base from 2 to 32.
def baseEquivalent(n, m):
for base in range(2, 33):
if checkUtil(n, m, base):
return True
return False
# Driver Code
if __name__ == "__main__":
n = 8
m = 4
if baseEquivalent(n, m) == True:
print("true")
else:
print("false")
using System;
class GFG {
// Returns true if 'n' can be represented using exactly
// 'm' digits in the given base.
static bool CheckUtil(int n, int m, int baseNum)
{
// If only one digit is left, the number must be
// smaller than the base.
if (m == 1)
return n < baseNum;
// Remove the last digit and check the remaining
// part.
if (n >= baseNum)
return CheckUtil(n / baseNum, m - 1, baseNum);
return false;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
static bool baseEquivalent(int n, int m)
{
for (int baseNum = 2; baseNum <= 32; baseNum++) {
if (CheckUtil(n, m, baseNum))
return true;
}
return false;
}
static void Main()
{
int n = 8;
int m = 4;
if (baseEquivalent(n, m) == true) {
Console.WriteLine("true");
}
else {
Console.WriteLine("false");
}
}
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in the given base.
function checkUtil(n, m, base)
{
// If only one digit is left, the number must be
// smaller than the base.
if (m === 1)
return n < base;
// Remove the last digit and check the remaining part.
if (n >= base)
return checkUtil(Math.floor(n / base), m - 1, base);
return false;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
function baseEquivalent(n, m)
{
for (let base = 2; base <= 32; base++) {
if (checkUtil(n, m, base))
return true;
}
return false;
}
// Driver Code
let n = 8;
let m = 4;
console.log(baseEquivalent(n, m));
Output
true
[Expected Approach] Using Mathematical Approach - O(1) Time and O(1) Space
A number has exactly m digits in a base b if it lies within the range b^(m - 1) to b^m - 1. Therefore, for each base from 2 to 32, we compute these two powers and simply check whether n falls within this range. This avoids explicitly converting the number into different bases.
- Iterate through every base from 2 to 32.
- For each base, compute base^(m - 1) and base^m using exponentiation.
- If a power exceeds n during computation, return n + 1 to avoid overflow and unnecessary calculations.
- Check whether base^(m - 1) <= n < base^m.
- If the condition is satisfied for any base, return true.
- If no valid base is found after checking all bases, return false.
#include <iostream>
using namespace std;
// Returns base^exp.
// If the value exceeds n during computation, return n + 1
// to avoid integer overflow and unnecessary calculations.
int powerLimit(int base, int exp, int n)
{
int res = 1;
for (int i = 0; i < exp; i++)
{
// If multiplying by 'base' would exceed 'n',
// stop early and return a value greater than 'n'.
if (res > n / base)
return n + 1;
res *= base;
}
return res;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
bool baseEquivalent(int n, int m)
{
// Check every valid base from 2 to 32.
for (int base = 2; base <= 32; base++)
{
// A number has exactly 'm' digits in base 'base' if:
// base^(m-1) <= n < base^m
int low = powerLimit(base, m - 1, n);
int high = powerLimit(base, m, n);
if (low <= n && n < high)
return true;
}
return false;
}
int main()
{
int n = 8;
int m = 4;
cout << (baseEquivalent(n, m) ? "true" : "false");
return 0;
}
class GFG {
// Returns base^exp.
// If the value exceeds n during computation, return n +
// 1 to avoid integer overflow and unnecessary
// calculations.
static int powerLimit(int base, int exp, int n)
{
int res = 1;
for (int i = 0; i < exp; i++) {
// If multiplying by 'base' would exceed 'n',
// stop early and return a value greater than
// 'n'.
if (res > n / base)
return n + 1;
res *= base;
}
return res;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
static boolean baseEquivalent(int n, int m)
{
// Check every valid base from 2 to 32.
for (int base = 2; base <= 32; base++) {
// A number has exactly 'm' digits in base
// 'base' if: base^(m-1) <= n < base^m
int low = powerLimit(base, m - 1, n);
int high = powerLimit(base, m, n);
if (low <= n && n < high)
return true;
}
return false;
}
public static void main(String[] args)
{
int n = 8;
int m = 4;
System.out.println(baseEquivalent(n, m));
}
}
# Returns base^exp.
# If the value exceeds n during computation, return n + 1
# to avoid unnecessary calculations.
def powerLimit(base, exp, n):
res = 1
for _ in range(exp):
# If multiplying by 'base' would exceed 'n',
# stop early and return a value greater than 'n'.
if res > n // base:
return n + 1
res *= base
return res
# Returns True if 'n' can be represented using exactly
# 'm' digits in any base from 2 to 32.
def baseEquivalent(n, m):
# Check every valid base from 2 to 32.
for base in range(2, 33):
# A number has exactly 'm' digits in base 'base' if:
# base^(m-1) <= n < base^m
low = powerLimit(base, m - 1, n)
high = powerLimit(base, m, n)
if low <= n < high:
return True
return False
# Driver Code
if __name__ == "__main__":
n = 8
m = 4
if baseEquivalent(n, m) == True:
print("true")
else:
print("false")
using System;
class GFG {
// Returns base^exp.
// If the value exceeds n during computation, return n +
// 1 to avoid integer overflow and unnecessary
// calculations.
static int PowerLimit(int baseNum, int exp, int n)
{
int res = 1;
for (int i = 0; i < exp; i++) {
// If multiplying by 'base' would exceed 'n',
// stop early and return a value greater than
// 'n'.
if (res > n / baseNum)
return n + 1;
res *= baseNum;
}
return res;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
static bool baseEquivalent(int n, int m)
{
// Check every valid base from 2 to 32.
for (int baseNum = 2; baseNum <= 32; baseNum++) {
// A number has exactly 'm' digits in base
// 'base' if: base^(m-1) <= n < base^m
int low = PowerLimit(baseNum, m - 1, n);
int high = PowerLimit(baseNum, m, n);
if (low <= n && n < high)
return true;
}
return false;
}
static void Main()
{
int n = 8;
int m = 4;
if (baseEquivalent(n, m) == true) {
Console.WriteLine("true");
}
else {
Console.WriteLine("false");
}
}
}
// Returns base^exp.
// If the value exceeds n during computation, return n + 1
// to avoid unnecessary calculations.
function powerLimit(base, exp, n)
{
let res = 1;
for (let i = 0; i < exp; i++) {
// If multiplying by 'base' would exceed 'n',
// stop early and return a value greater than 'n'.
if (res > Math.floor(n / base))
return n + 1;
res *= base;
}
return res;
}
// Returns true if 'n' can be represented using exactly
// 'm' digits in any base from 2 to 32.
function baseEquivalent(n, m)
{
// Check every valid base from 2 to 32.
for (let base = 2; base <= 32; base++) {
// A number has exactly 'm' digits in base 'base'
// if: base^(m-1) <= n < base^m
const low = powerLimit(base, m - 1, n);
const high = powerLimit(base, m, n);
if (low <= n && n < high)
return true;
}
return false;
}
// Driver Code
let n = 8;
let m = 4;
console.log(baseEquivalent(n, m));
Output
true
Time Complexity: O(1), since the algorithm checks only 31 possible bases (2 to 32), and the maximum number of digits (m) is also bounded by a constant.
Auxiliary Space: O(1).