Count the Digits That Divide a Number - Problem

Given an integer num, return the number of digits in num that divide num evenly.

A digit val divides num if num % val == 0.

Note: We only consider non-zero digits, as division by zero is undefined.

Example: For num = 1248, the digits are 1, 2, 4, 8. We check: 1248 % 1 = 0 ✓, 1248 % 2 = 0 ✓, 1248 % 4 = 0 ✓, 1248 % 8 = 0 ✓. All 4 digits divide the number, so we return 4.

This problem tests your understanding of digit extraction and modular arithmetic.

Input & Output

example_1.py — Basic Case
$ Input: num = 7
Output: 1
💡 Note: The only digit is 7, and 7 % 7 = 0, so 7 divides 7. Return 1.
example_2.py — Multiple Digits
$ Input: num = 1248
Output: 4
💡 Note: Digits are 1, 2, 4, 8. Check: 1248 % 1 = 0, 1248 % 2 = 0, 1248 % 4 = 0, 1248 % 8 = 0. All 4 digits divide 1248.
example_3.py — With Non-Divisible Digits
$ Input: num = 121
Output: 2
💡 Note: Digits are 1, 2, 1. Check: 121 % 1 = 0 ✓, 121 % 2 = 1 ✗, 121 % 1 = 0 ✓. Only 2 digits divide 121.

Constraints

  • 1 ≤ num ≤ 109
  • num does not contain 0 as a digit

Visualization

Tap to expand
Quality Control Assembly Line - Digit Division ProblemProduct1248Original NumberExtract81248%104124%10212%1011%10Components (Digits)Test 81248÷8= 156 ✓Test 41248÷4= 312 ✓Test 21248÷2= 624 ✓Test 11248÷1= 1248 ✓Quality Testing StationQuality Report4 / 4All Components Pass!Algorithm Steps1. Extract digit: num % 102. Test divisibility: original % digit3. Count if remainder = 04. Remove digit: num //= 105. Repeat until num = 0Time: O(d) Space: O(1)d = number of digits✓ No string conversion✓ Constant space usage
Understanding the Visualization
1
Component Extraction
Extract each digit component from the product using mathematical operations
2
Quality Test
Test if the component (digit) is compatible with the product (original number)
3
Count Valid Components
Keep track of how many components pass the quality test
4
Final Report
Return the total count of compatible components
Key Takeaway
🎯 Key Insight: Mathematical digit extraction using modulo and division is more efficient than string conversion, providing O(1) space complexity while maintaining O(d) time complexity.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~8 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen