Skip to main content

Count Digits of a Number

Description

Given a positive integer n, count how many of its individual digits divide n evenly — that is, without leaving a remainder.

A digit d of n divides n evenly when n % d == 0. You must check each digit of n separately. If a digit is 0, skip it entirely because division by zero is undefined.

Return the total count of digits that satisfy this divisibility condition.

Examples

Example 1

Input: n = 12

Output: 2

Explanation: The digits of 12 are 1 and 2. Check each: 12 % 1 = 0 (divides evenly) and 12 % 2 = 0 (divides evenly). Both digits divide 12, so the answer is 2.

Example 2

Input: n = 2446

Output: 1

Explanation: The digits of 2446 are 2, 4, 4, and 6. Check each: 2446 % 2 = 0 (yes), 2446 % 4 = 2 (no), 2446 % 4 = 2 (no), 2446 % 6 = 4 (no). Only the digit 2 divides 2446 evenly, so the answer is 1.

Example 3

Input: n = 23

Output: 0

Explanation: The digits of 23 are 2 and 3. Check each: 23 % 2 = 1 (no) and 23 % 3 = 2 (no). Neither digit divides 23 evenly, so the answer is 0.

Constraints

  • 1 ≤ n ≤ 10^5

Editorial

Brute Force - String Conversion

Intuition

The most straightforward way to access each digit of a number is to convert it into a string. Once we have the string, we can iterate over each character, convert it back to an integer, and check whether it divides the original number evenly.

Think of the number as a word made up of individual letters. By converting the number to its string form, we can "spell it out" and examine each letter (digit) one at a time. For each digit, we simply ask: does the original number divide cleanly by this digit?

Step-by-Step Explanation

Let's trace through with n = 2446:

Step 1: Convert n = 2446 to string → "2446". Initialize count = 0.

Step 2: Process character '2' → digit = 2.

  • digit is not 0, so check: 2446 % 2 = 0.
  • 0 means it divides evenly → increment count to 1.

Step 3: Process character '4' → digit = 4.

  • digit is not 0, so check: 2446 % 4 = 2.
  • 2 ≠ 0, does NOT divide evenly → count stays 1.

Step 4: Process character '4' → digit = 4 (second occurrence).

  • digit is not 0, so check: 2446 % 4 = 2.
  • 2 ≠ 0, does NOT divide evenly → count stays 1.

Step 5: Process character '6' → digit = 6.

  • digit is not 0, so check: 2446 % 6 = 4.
  • 4 ≠ 0, does NOT divide evenly → count stays 1.

Step 6: All digits processed. Return count = 1.

String Conversion — Checking Each Digit of 2446 — Watch how we convert the number to a string, then check each digit one by one for divisibility against the original number.

Algorithm

  1. Convert the number n to its string representation
  2. Initialize a counter count = 0
  3. For each character in the string:
    • Convert the character to an integer digit
    • If digit is 0, skip it (cannot divide by zero)
    • If n % digit == 0, increment count
  4. Return count

Code

#include <string>
using namespace std;

class Solution {
public:
    int evenlyDivides(int n) {
        string s = to_string(n);
        int count = 0;
        
        for (char c : s) {
            int digit = c - '0';
            if (digit != 0 && n % digit == 0) {
                count++;
            }
        }
        
        return count;
    }
};
class Solution:
    def evenlyDivides(self, n: int) -> int:
        count = 0
        
        for char in str(n):
            digit = int(char)
            if digit != 0 and n % digit == 0:
                count += 1
        
        return count
class Solution {
    public int evenlyDivides(int n) {
        String s = String.valueOf(n);
        int count = 0;
        
        for (char c : s.toCharArray()) {
            int digit = c - '0';
            if (digit != 0 && n % digit == 0) {
                count++;
            }
        }
        
        return count;
    }
}

Complexity Analysis

Time Complexity: O(d)

Where d is the number of digits in n. We iterate through each digit exactly once. Since n ≤ 10^5, d is at most 6, making this effectively O(1) for the given constraints.

Space Complexity: O(d)

We create a string of length d to represent the number. This is the extra space used. For the given constraints this is at most 6 characters.

Why This Approach Is Not Efficient

While the string conversion approach works correctly, it introduces unnecessary overhead:

  1. String allocation: Converting an integer to a string requires allocating memory for the character array. This is a heap allocation in most languages.
  2. Double conversion cost: We convert the number to a string, then convert each character back to an integer — two conversions per digit.
  3. Extra space: The string copy takes O(d) additional space.

For this specific problem with small constraints, the difference is negligible. However, mathematically we can extract digits directly from the number using the modulo operator (% 10) and integer division (/ 10), which avoids string creation entirely and works purely with arithmetic operations — no extra memory needed.

Optimal Approach - Modulo Extraction

Intuition

Instead of converting the number to a string, we can extract each digit mathematically. The key insight is that any number in base 10 can be decomposed digit by digit using two operations:

  • n % 10 gives us the last (rightmost) digit of the number.
  • n / 10 (integer division) removes the last digit, shifting all remaining digits one place to the right.

By repeating these two operations in a loop until the number becomes 0, we visit every digit exactly once — all without creating any strings or extra data structures.

Imagine peeling digits off the end of a number like peeling layers off an onion. Each peel reveals one digit, and the onion gets smaller until nothing remains.

Step-by-Step Explanation

Let's trace through with n = 2446:

Step 1: Initialize count = 0. Store a copy of the original number: original = 2446. Set temp = 2446.

Step 2: Extract last digit: temp % 10 = 2446 % 10 = 6.

  • digit = 6, digit ≠ 0.
  • Check: 2446 % 6 = 4. Not zero → does NOT divide evenly. Count stays 0.
  • Remove last digit: temp = 2446 / 10 = 244.

Step 3: Extract last digit: temp % 10 = 244 % 10 = 4.

  • digit = 4, digit ≠ 0.
  • Check: 2446 % 4 = 2. Not zero → does NOT divide evenly. Count stays 0.
  • Remove last digit: temp = 244 / 10 = 24.

Step 4: Extract last digit: temp % 10 = 24 % 10 = 4.

  • digit = 4, digit ≠ 0.
  • Check: 2446 % 4 = 2. Not zero → does NOT divide evenly. Count stays 0.
  • Remove last digit: temp = 24 / 10 = 2.

Step 5: Extract last digit: temp % 10 = 2 % 10 = 2.

  • digit = 2, digit ≠ 0.
  • Check: 2446 % 2 = 0. Zero → divides evenly! Increment count to 1.
  • Remove last digit: temp = 2 / 10 = 0.

Step 6: temp = 0, loop ends. Return count = 1.

Modulo Extraction — Peeling Digits from 2446 — Watch how we extract digits from right to left using modulo 10, checking each for divisibility against the original number.

Algorithm

  1. Store the original value of n (we need it for divisibility checks)
  2. Initialize count = 0
  3. While temp > 0:
    • Extract the last digit: digit = temp % 10
    • If digit ≠ 0 and original % digit == 0, increment count
    • Remove the last digit: temp = temp / 10
  4. Return count

Code

class Solution {
public:
    int evenlyDivides(int n) {
        int count = 0;
        int temp = n;
        
        while (temp > 0) {
            int digit = temp % 10;
            if (digit != 0 && n % digit == 0) {
                count++;
            }
            temp /= 10;
        }
        
        return count;
    }
};
class Solution:
    def evenlyDivides(self, n: int) -> int:
        count = 0
        temp = n
        
        while temp > 0:
            digit = temp % 10
            if digit != 0 and n % digit == 0:
                count += 1
            temp //= 10
        
        return count
class Solution {
    public int evenlyDivides(int n) {
        int count = 0;
        int temp = n;
        
        while (temp > 0) {
            int digit = temp % 10;
            if (digit != 0 && n % digit == 0) {
                count++;
            }
            temp /= 10;
        }
        
        return count;
    }
}

Complexity Analysis

Time Complexity: O(d)

Where d is the number of digits in n. The while loop runs exactly d times — once for each digit. Each iteration performs constant-time arithmetic (modulo and division). For n ≤ 10^5, d ≤ 6, making this effectively O(1).

Space Complexity: O(1)

We use only a fixed number of integer variables (count, temp, digit) regardless of the size of n. No strings or arrays are created, so no extra space is consumed.