Count divisors of n that have at-least one digit common with n
Last Updated :
18 Sep, 2023
Given a number n, find the number of divisors whose at least one digit in the decimal representation matches with the number n.
Examples:
Input : n = 10
Output: 2
Explanation: numbers are 1 and 10, 1 and 10
both have a nimbus of 1 digit common with n = 10.
Input : n = 15
Output: 3
Explanation: the numbers are 1, 5, 15, all of them
have a minimum of 1 digit common.
A naive approach is to iterate from 1 to n and check for all the divisors and use hashing to determine if any of the digits match with n or not.
Time complexity: O(n)
Auxiliary space: O(1)
An efficient approach is to iterate from 1 to sqrt(n) and check for all the divisors i and n/i, if both are different, we check if there is any match for i and n/i, if yes we simply add 1 to the answer. We use hashing to store if a number is present or not.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
bool isDigitPresent( int m, bool hash[])
{
while (m) {
if (hash[m % 10])
return true ;
m = m / 10;
}
return false ;
}
int countDivisibles( int n)
{
bool hash[10] = { 0 };
int m = n;
while (m) {
hash[m % 10] = true ;
m = m / 10;
}
int ans = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (isDigitPresent(i, hash))
ans++;
if (n / i != i) {
if (isDigitPresent(n/i, hash))
ans++;
}
}
}
return ans;
}
int main()
{
int n = 15;
cout << countDivisibles(n);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean isDigitPresent( int m,
boolean hash[])
{
while (m > 0 ) {
if (hash[m % 10 ])
return true ;
m = m / 10 ;
}
return false ;
}
static int countDivisibles( int n)
{
boolean hash[] = new boolean [ 10 ];
int m = n;
while (m > 0 ) {
hash[m % 10 ] = true ;
m = m / 10 ;
}
int ans = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n);
i++)
{
if (n % i == 0 ) {
if (isDigitPresent(i, hash))
ans++;
if (n / i != i) {
if (isDigitPresent(n/i, hash))
ans++;
}
}
}
return ans;
}
public static void main (String[] args)
{
int n = 15 ;
System.out.print(countDivisibles(n));
}
}
|
Python3
import math
def isDigitPresent(m, Hash ):
while (m):
if ( Hash [m % 10 ]):
return True
m = m / / 10
return False
def countDivisibles(n):
Hash = [ False for i in range ( 10 )]
m = n
while (m):
Hash [m % 10 ] = True
m = m / / 10
ans = 0
for i in range ( 1 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
if (isDigitPresent(i, Hash )):
ans + = 1
if (n / / i ! = i):
if (isDigitPresent(n / / i, Hash )):
ans + = 1
return ans
n = 15
print (countDivisibles(n))
|
C#
using System;
class GFG {
static bool isDigitPresent( int m, bool [] hash)
{
while (m > 0) {
if (hash[m % 10])
return true ;
m = m / 10;
}
return false ;
}
static int countDivisibles( int n)
{
bool [] hash = new bool [10];
int m = n;
while (m > 0) {
hash[m % 10] = true ;
m = m / 10;
}
int ans = 0;
for ( int i = 1; i <= Math.Sqrt(n); i++) {
if (n % i == 0) {
if (isDigitPresent(i, hash))
ans++;
if (n / i != i) {
if (isDigitPresent(n / i, hash))
ans++;
}
}
}
return ans;
}
public static void Main()
{
int n = 15;
Console.Write(countDivisibles(n));
}
}
|
PHP
<?php
function isDigitPresent( $m , $hash )
{
while ( $m )
{
if ( $hash [ $m % 10])
return true;
$m = (int)( $m / 10);
}
return false;
}
function countDivisibles( $n )
{
$hash = array_fill (0, 10, false);
$m = $n ;
while ( $m )
{
$hash [ $m % 10] = true;
$m = (int)( $m / 10);
}
$ans = 0;
for ( $i = 1; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
{
if (isDigitPresent( $i , $hash ))
$ans ++;
if ((int)( $n / $i ) != $i )
{
if (isDigitPresent((int)( $n / $i ), $hash ))
$ans ++;
}
}
}
return $ans ;
}
$n = 15;
echo countDivisibles( $n );
?>
|
Javascript
<script>
function isDigitPresent(m, hash)
{
while (m > 0) {
if (hash[m % 10])
return true ;
m = Math.floor(m / 10);
}
return false ;
}
function countDivisibles(n)
{
let hash = Array.from({length: 10}, (_, i) => 0);
let m = n;
while (m > 0) {
hash[m % 10] = true ;
m = Math.floor(m / 10);
}
let ans = 0;
for (let i = 1; i <= Math.sqrt(n);
i++)
{
if (n % i == 0) {
if (isDigitPresent(i, hash))
ans++;
if (n / i != i) {
if (isDigitPresent(n/i, hash))
ans++;
}
}
}
return ans;
}
let n = 15;
document.write(countDivisibles(n));
</script>
|
Time complexity: O(sqrt n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Next
Shuffle array {a1, a2, .. an, b1, b2, .. bn} as {a1, b1, a2, b2, a3, b3, ……, an, bn} without using extra space
Share your thoughts in the comments
Please Login to comment...