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; // Function to check if N is a super-d number bool isSuperdNum( int n) { for ( int d = 2; d < 10; d++) { string subString = "" ; // create an empty string for ( int i = 0; i < d; i++) { subString += to_string(d); // add d as string to the subString } if (to_string(d * pow (n, d)).find(subString) != string::npos) // check if subString is present in d * n^d as a substring return true ; } return false ; } // Driver Code int main() { int n = 261; if (isSuperdNum(n) == true ) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Java
// Java implementation to // check if N is a super-d number class GFG{ // Function to check if N // is a super-d number 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 ; } // Driver Code private static String newString( int d) { String ans = "" ; for ( int i = 0 ; i < d; i++) { ans += String.valueOf(d); } return ans; } // Driver Code public static void main(String[] args) { int n = 261 ; if (isSuperdNum(n) == true ) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation to # check if N is a super-d number # Function to check if N # is a super-d number 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 # Driver Code n = 261 if isSuperdNum(n) = = True : print ( "Yes" ) else : print ( "No" ) |
C#
// C# implementation to // check if N is a super-d number using System; class GFG{ // Function to check if N // is a super-d number 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; } // Driver Code public static void Main(String[] args) { int n = 261; if (isSuperdNum(n) == true ) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by Rajput-Ji |
Javascript
// Function to check if N is a super-d number 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 ; } // Driver code let n = 261; if (isSuperdNum(n)) { console.log( "Yes" ); } else { console.log( "No" ); } |
Yes
Time Complexity: O(1)
Auxiliary Space: O(1) as it is using constant space
References: OEIS
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!