# Check if N contains all digits as K in base B

• Last Updated : 16 Aug, 2021

Given three numbers N, K, and B, the task is to check if N contains only K as digits in Base B.

Examples:

Input: N = 13, B = 3, K = 1
Output: Yes
Explanation:
13 base 3 is 111 which contain all one’s(K).

Input: N = 5, B = 2, K = 1
Output: No
Explanation:
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:

## C++

 // C++ implementation of the approach#includeusing namespace std; // Function to print the number of digitsint findNumberOfDigits(int n, int base){         // Calculate log using base change    // property and then take its floor    // and then add 1    int dig = (floor(log(n) / log(base)) + 1);     // Return the output    return (dig);} // Function that returns true if n contains// all one's in base bint isAllKs(int n, int b, int k){    int len = findNumberOfDigits(n, b);     // Calculate the sum    int sum = k * (1 - pow(b, len)) /                  (1 - b);    if(sum == n)    {        return(sum);    }} // Driver codeint main(){         // Given number N    int N = 13;         // Given base B    int B = 3;         // Given digit K    int K = 1;         // Function call    if (isAllKs(N, B, K))    {        cout << "Yes";    }    else    {        cout << "No";    }} // This code is contributed by vikas_g

## C

 // C implementation of the approach#include #include  // Function to print the number of digitsint findNumberOfDigits(int n, int base){         // Calculate log using base change    // property and then take its floor    // and then add 1    int dig = (floor(log(n) / log(base)) + 1);     // Return the output    return (dig);} // Function that returns true if n contains// all one's in base bint isAllKs(int n, int b, int k){    int len = findNumberOfDigits(n, b);     // Calculate the sum    int sum = k * (1 - pow(b, len)) /                  (1 - b);    if(sum == n)    {        return(sum);    }} // Driver codeint main(void){         // Given number N    int N = 13;         // Given base B    int B = 3;         // Given digit K    int K = 1;         // Function call    if (isAllKs(N, B, K))    {        printf("Yes");    }    else    {        printf("No");    }    return 0;} // This code is contributed by vikas_g

## Java

 // Java implementation of above approachimport java.util.*; class GFG{ // Function to print the number of digitsstatic int findNumberOfDigits(int n, int base){         // Calculate log using base change    // property and then take its floor    // and then add 1    int dig = ((int)Math.floor(Math.log(n) /                    Math.log(base)) + 1);         // Return the output    return dig;} // Function that returns true if n contains// all one's in base bstatic boolean isAllKs(int n, int b, int k){    int len = findNumberOfDigits(n, b);         // Calculate the sum    int sum = k * (1 - (int)Math.pow(b, len)) /                  (1 - b);         return sum == n;} // Driver codepublic static void main(String[] args){         // Given number N    int N = 13;         // Given base B    int B = 3;         // Given digit K    int K = 1;         // Function call    if (isAllKs(N, B, K))        System.out.println("Yes");    else        System.out.println("No");}} // This code is contributed by offbeat

## Python3

 # Python3 program for the above approachimport math # Function to print the number of digitsdef findNumberOfDigits(n, base):     # Calculate log using base change    # property and then take its floor    # and then add 1    dig = (math.floor(math.log(n) /                      math.log(base)) + 1)     # Return the output    return dig # Function that returns true if n contains# all one's in base bdef isAllKs(n, b, k):     len = findNumberOfDigits(n, b)     # Calculate the sum    sum = k * (1 - pow(b, len)) / (1 - b)     return sum == N # Driver code # Given number NN = 13 # Given base BB = 3 # Given digit KK = 1 # Function callif (isAllKs(N, B, K)):    print("Yes")else:    print("No")

## C#

 // C# implementation of above approachusing System; class GFG{     // Function to print the number of digitsstatic int findNumberOfDigits(int n, int bas){         // Calculate log using base change    // property and then take its floor    // and then add 1    int dig = ((int)Math.Floor(Math.Log(n) /                               Math.Log(bas)) + 1);         // Return the output    return dig;} // Function that returns true if n contains// all one's in base bstatic bool isAllKs(int n, int b, int k){    int len = findNumberOfDigits(n, b);         // Calculate the sum    int sum = k * (1 - (int)Math.Pow(b, len)) /                  (1 - b);         return sum == n;} // Driver codepublic static void Main(){         // Given number N    int N = 13;         // Given base B    int B = 3;         // Given digit K    int K = 1;         // Function call    if (isAllKs(N, B, K))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by vikas_g

## Javascript

 

Output:

Yes

Time Complexity: O(log(D)), where D is the number of digits in number N
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up