Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Count Digits

Easy
0/40
profile
Contributed by
478 upvotes

Problem statement

You are given a number ’n’.


Find the number of digits of ‘n’ that evenly divide ‘n’.


Note:
A digit evenly divides ‘n’ if it leaves no remainder when dividing ‘n’.


Example:
Input: ‘n’ = 336

Output: 3

Explanation:
336 is divisible by both ‘3’ and ‘6’. Since ‘3’ occurs twice it is counted two times.
Note:
You don’t need to print anything. Just implement the given function.


Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘n’.


Output Format:
Return an integer as described in the problem statement.


Note
You don’t need to print anything, it has already been taken care of, just complete the given function.
Sample Input 1:
35


Sample Output 1:
1


Explanation of sample output 1:
35 is only divisible by ‘5’, hence answer is 1.


Sample Input 2:
373


Sample Output 2:
0


Explanation of sample output 2:
There’s no digit in 373 that evenly divides it. Hence the output is 0.


Expected Time Complexity:
Try to solve this in O(log(n)) 


Constraints:
1 <= ‘n’ <= 10^9

Time Limit: 1 sec
Hint

Think about how to iterate through each digit of the number 'n' and check whether it divides 'n'.

Approaches (1)
Maths

Approach:

We will iterate through all the digits of the number and increment the answer if the current digit divides the number. Let a ‘k’ digit number be d_k-1 … d_1 d_0, where d_0 to d_k-1 represent its individual digits.

  • Let cur = d_k-1 … d_1 d_0
  • cur = 10 * (d_k-1 … d_1) + d_0
  • We can get the value of d_0 by cur%10.
  • Then if we divide cur by 10, cur = d_k-1 … d_1

We can iterate through all the digits by repeating the above steps until cur > 0. At each iteration we will increment ‘answer’ if d_0 divides ‘n’.


 

The steps are as follows:

function countDigits(int n)

  1. Initialize ‘cur’ with ‘n’
  2. Initialize ‘ans’ with ‘0’
  3. while (cur>0)
    1. d_0 = cur%10
    2. if(d_0 != 0)
      1. ans += (n%d_0 == 0)
    3. cur /= 10
  4. return ans

      

Time Complexity

O(log(n)), where ‘n’ is the given number.


 

We are iterating through all the digits of ‘n’ and there are log(n) such digits.


 

Hence, the time complexity is O(log(n)).

Space Complexity

O(1).


 

We are not using any extra space.


 

Hence, the space complexity is O(1).

Code Solution
(100% EXP penalty)
Count Digits
All tags
Sort by
Search icon

Interview problems

Count Digits Using C++

int countDigits(int n){

    // Write your code here.

    int cnt=0;

    int original= n;

 

    while(n>0){

        int digit =n%10;

        if(digit !=0 && original % digit==0)

        cnt= cnt+1;

        n=n/10;

    }   

    return  cnt;

}

 

91 views
0 replies
0 upvotes

Interview problems

Count digits which evenly divides number ...

public class Solution {

public static int countDigits(int n){

int ct=0;

int orgNum = n;

while(n>0){

int dig = n%10;

if(dig !=0 && orgNum%dig ==0){

ct++;

}

n/=10;

}

return ct;

}

}

62 views
0 replies
0 upvotes

Interview problems

Basic Solution

int orig = n;    

        

        int ct =0;

        while(n!=0){

         int ld = n%10;

          if( ld!=0 && orig%ld==0){

              ct++;

          }

          n=n/10;

        }

        return ct;

java

programming

beginners

88 views
0 replies
0 upvotes

Interview problems

Solution for count digits which evenly divides number

public class Solution {

    public static int countDigits(int n){

        int count = 0;

        String str = Integer.toString(n);

        for(int i=0;i<str.length();i++){

            int digit = str.charAt(i) - '0';

           if(digit!=0 && n % digit == 0) count++;

        }

        return count;

    }

}

116 views
0 replies
0 upvotes

Interview problems

Count digits which evenly divides number

def countDigits(n: int) -> int:
    count = 0
    num = n

    for i in range(len(str(n))):
        divisor = num % 10
        if divisor > 0 and n % divisor == 0: 
            count += 1
        num //= 10
    return count
165 views
0 replies
2 upvotes

Interview problems

Count Digits

int countDigits(int n){

    int orignal = n;

    int count = 0;

    while(n>0){

        int lastdigit = n%10;

        if(lastdigit!=0 && orignal%lastdigit == 0){

            count++;

 

        }

        n = n/10;

    }   

    return count;

}

322 views
0 replies
0 upvotes

Interview problems

Count digits which divides the number equally

int countDigits(int n){

    // Write your code here.

    int count=0, a=n;

    if(n==0)

    return 1;

 

    while(a>0)

    {

        int l=a%10;

                if (l != 0 && n % l == 0 ) {

                  count = count + 1;

                }

        a=a/10;

    }   

    return count;

}

programming

beginners

286 views
0 replies
0 upvotes

Interview problems

C++ Easy Solution Easily understandable :

int countDigits(int n) {  

    int count = 0;  

    for (int temp = n; temp > 0; temp /= 10) {  

        int ld = temp % 10;  

        if (ld != 0 && n % ld == 0) {  

            count++;  

        }  

    }  

    return count;  

}  

323 views
0 replies
1 upvote

Interview problems

Easy Solution

public class Solution {

 public static int countDigits(int n){

  int count=0;

  int lastdigit=0;

  int m=n;   //declare this otherwise get error

  while(n>0){

    lastdigit=n%10;

    if(lastdigit!=0){  //don't merge this if condition with given below.

    if(m%lastdigit==0){

      count++;

    }

   }

  n=n/10;

 

  }

return count;

}

}

303 views
0 replies
0 upvotes

Interview problems

Easy Solution

int countDigits(int n){

    // Write your code here

    int cnt=0;  

    int temp=n;

    while(temp>0){

        int x = temp%10;

        if(x==0) cnt+=0;

        else if(n%x==0) cnt++;

        temp /=10;

    }

    return cnt;

}

410 views
0 replies
2 upvotes
Full screen
Console