Given three numbers N, K, and B, the task is to check if N contains only K as digits in Base B.
Input: N = 13, B = 3, K = 1
13 base 3 is 111 which contain all one’s(K).
Input: N = 5, B = 2, K = 1
5 base 2 is 101 which doesn’t contains all one’s (K).
Naive Approach: A simple solution is to convert the given number N to base B and one by one check if all its digits are K or not.
Time Complexity: O(D), where D is the number of digits in number N
Auxiliary Space: O(1)
Efficient Approach: The key observation in the problem is that any number with all digits as K in base B can be represented as:
These terms are in the form of the Geometric Progression with the first term as K and the common ratio as B.
Sum of G.P. Series:
Therefore, the number in base B with all digits as K is:
Hence, just check if this sum equals N or not. If it’s equal then print “Yes” otherwise print “No”.
Below is the implementation of the above approach:
Time Complexity: O(log(D)), where D is the number of digits in number N
Auxiliary Space: O(1)
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 number of its digits in any base (base b)
- Given a number N in decimal base, find the sum of digits in any base B
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Find the Nth digit from right in base B of the given number in Decimal base
- Add two integers of different base and represent sum in smaller base of the two
- Minimum digits to be removed to make either all digits or alternating digits same
- Check if given number contains a digit which is the average of all other digits
- Check if a given number can be represented in given a no. of digits in any base
- Check if the number is even or odd whose digits and base (radix) is given
- All possible numbers of N digits and base B without leading zeros
- Check if the sum of digits of number is divisible by all of its digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- How to check if string contains only digits in Java
- Find the total count of numbers up to N digits in a given base B
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Check if the given array contains all the divisors of some integer
- Length of array pair formed where one contains all distinct elements and other all same elements
- Check if a number N starts with 1 in b-base
- Check if a number is power of k using base changing method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.