Super-D Numbers
Last Updated :
17 Feb, 2023
Given an integer N, the task is to check N is a super-d Number.
Super-D Number is a number N such that D*ND contains a substring made of D digits containing D only, where D is more than 1 and less than 10
Examples:
Input: N = 261
Output: Yes
Explanation:
It will be true for D = 3
D*ND = 3*2613 = 53338743
which contains a substring made of 3 digits 333 containing 3 only.
Input: N = 10
Output: No
Approach: The idea is to create every possible string concatenating digit D, D number of times and then will check if the concatenation is present as a substring in D*ND or not, where D will be in the range [2, 9].
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string> // include string library for string manipulation
#include <cmath> // include cmath library for pow function
using namespace std;
bool isSuperdNum( int n)
{
for ( int d = 2; d < 10; d++)
{
string subString = "" ;
for ( int i = 0; i < d; i++)
{
subString += to_string(d);
}
if (to_string(d * pow (n, d)).find(subString) != string::npos)
return true ;
}
return false ;
}
int main()
{
int n = 261;
if (isSuperdNum(n) == true )
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
class GFG{
static boolean isSuperdNum( int n)
{
for ( int d = 2 ; d < 10 ; d++)
{
String subString = newString(d);
if (String.valueOf(
(d * Math.pow(n, d))).contains(subString))
return true ;
}
return false ;
}
private static String newString( int d)
{
String ans = "" ;
for ( int i = 0 ; i < d; i++)
{
ans += String.valueOf(d);
}
return ans;
}
public static void main(String[] args)
{
int n = 261 ;
if (isSuperdNum(n) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isSuperdNum(n):
for d in range ( 2 , 10 ):
substring = str (d) * d;
if substring in str (d * pow (n, d)):
return True
return False
n = 261
if isSuperdNum(n) = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isSuperdNum( int n)
{
for ( int d = 2; d < 10; d++)
{
String subString = newString(d);
if (String.Join( "" ,
(d * Math.Pow(n, d))).Contains(subString))
return true ;
}
return false ;
}
private static String newString( int d)
{
String ans = "" ;
for ( int i = 0; i < d; i++)
{
ans += String.Join( "" , d);
}
return ans;
}
public static void Main(String[] args)
{
int n = 261;
if (isSuperdNum(n) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isSuperdNum(n) {
for (let d = 2; d < 10; d++) {
let substring = String(d).repeat(d);
if (String(d * Math.pow(n, d)).includes(substring)) {
return true ;
}
}
return false ;
}
let n = 261;
if (isSuperdNum(n)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(1)
Auxiliary Space: O(1) as it is using constant space
References: OEIS
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...