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++
#include<bits/stdc++.h>
using namespace std;
int findNumberOfDigits( int n, int base)
{
int dig = ( floor ( log (n) / log (base)) + 1);
return (dig);
}
int isAllKs( int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
int sum = k * (1 - pow (b, len)) /
(1 - b);
if (sum == n)
{
return (sum);
}
}
int main()
{
int N = 13;
int B = 3;
int K = 1;
if (isAllKs(N, B, K))
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
}
|
C
#include <stdio.h>
#include <math.h>
int findNumberOfDigits( int n, int base)
{
int dig = ( floor ( log (n) / log (base)) + 1);
return (dig);
}
int isAllKs( int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
int sum = k * (1 - pow (b, len)) /
(1 - b);
if (sum == n)
{
return (sum);
}
}
int main( void )
{
int N = 13;
int B = 3;
int K = 1;
if (isAllKs(N, B, K))
{
printf ( "Yes" );
}
else
{
printf ( "No" );
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findNumberOfDigits( int n, int base)
{
int dig = (( int )Math.floor(Math.log(n) /
Math.log(base)) + 1 );
return dig;
}
static boolean isAllKs( int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
int sum = k * ( 1 - ( int )Math.pow(b, len)) /
( 1 - b);
return sum == n;
}
public static void main(String[] args)
{
int N = 13 ;
int B = 3 ;
int K = 1 ;
if (isAllKs(N, B, K))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math
def findNumberOfDigits(n, base):
dig = (math.floor(math.log(n) /
math.log(base)) + 1 )
return dig
def isAllKs(n, b, k):
len = findNumberOfDigits(n, b)
sum = k * ( 1 - pow (b, len )) / ( 1 - b)
return sum = = N
N = 13
B = 3
K = 1
if (isAllKs(N, B, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int findNumberOfDigits( int n, int bas)
{
int dig = (( int )Math.Floor(Math.Log(n) /
Math.Log(bas)) + 1);
return dig;
}
static bool isAllKs( int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
int sum = k * (1 - ( int )Math.Pow(b, len)) /
(1 - b);
return sum == n;
}
public static void Main()
{
int N = 13;
int B = 3;
int K = 1;
if (isAllKs(N, B, K))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function findNumberOfDigits(n, base)
{
var dig = (Math.floor(Math.log(n) / Math.log(base)) + 1);
return (dig);
}
function isAllKs(n, b, k)
{
var len = findNumberOfDigits(n, b);
var sum = k * (1 - Math.pow(b, len)) /
(1 - b);
if (sum == n)
{
return (sum);
}
}
var N = 13;
var B = 3;
var K = 1;
if (isAllKs(N, B, K))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(log(D)), where D is the number of digits in number N
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...