Given a number and no. of digits to represent the number, find if given number can be represented in given no. of digits in any base from 2 to 32.
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 to 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 number is smaller than base and digit is 1, then return true.
2) Else if digit is more than 1 and 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 above idea.
This article is contributed by Mehboob Elahi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Given a number N in decimal base, find the sum of digits in any base B
- Find the Nth digit from right in base B of the given number in Decimal base
- Find the smallest positive number which can not be represented by given digits
- Check whether a number can be represented as difference of two squares
- Check if N contains all digits as K in base B
- Sum of two numbers where one number is represented as array of digits
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Find the total count of numbers up to N digits in a given base B
- Find smallest number with given number of digits and sum of digits under given constraints
- Program to check if a number is divisible by any of its digits
- Minimum digits to be removed to make either all digits or alternating digits same
- All possible numbers of N digits and base B without leading zeros
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Find maximum number that can be formed using digits of a given number
- Count of integers in a range which have even number of odd digits and odd number of even digits