Leetcode 2520: Count the Digits That Divide a Number

grid47
grid47
Exploring patterns and algorithms
Feb 29, 2024 4 min read

Given a positive integer num, determine how many of its digits divide num without leaving a remainder. In other words, for each digit in num, check if num % digit == 0 and count how many digits satisfy this condition.
Problem
Approach
Steps
Complexity
Input: You are given an integer `num`. You need to find how many of its digits divide the number `num` evenly.
Example: num = 123
Constraints:
• 1 <= num <= 10^9
• num does not contain the digit 0
Output: Return the number of digits in `num` that divide `num` without leaving a remainder.
Example: Output: 2
Constraints:
• The output should be an integer representing the number of digits that divide the number.
Goal: The goal is to count how many digits of `num` divide `num` evenly.
Steps:
• Convert `num` to a string to access each digit.
• For each digit in the number, check if `num % digit == 0`.
• Count how many digits satisfy the condition.
Goal: The number `num` is a positive integer with up to 9 digits.
Steps:
• The integer `num` is guaranteed to have no zeroes as its digits.
• The value of `num` is between 1 and 10^9.
Assumptions:
• All digits in `num` are non-zero.
Input: num = 123
Explanation: For `123`, the digits `1` and `3` divide `123`, but `2` does not. So, the answer is 2.

Input: num = 256
Explanation: For `256`, the digits `2`, `5`, and `6` divide `256`. So, the answer is 3.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus