Advertisement

Count the Digits That Divide a Number - LeetCode 2520 Solution

Count the Digits That Divide a Number - Complete Solution Guide

Count the Digits That Divide a Number is LeetCode problem 2520, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer num , return the number of digits in num that divide num . An integer val divides nums if nums % val == 0 . Example 1: Input: num = 7 Output: 1 Explanation: 7 divides itself, hence the answer is 1. Example 2: Input: num = 121 Output: 2 Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2. Example 3: Input: num = 1248 Output: 4 Explanation: 1248 is divisible by all of its digits, hence the answer is 4. Constraints: 1 <= num <= 10 9 num does

Detailed Explanation

The problem asks you to count how many digits of a given integer `num` divide the integer evenly. A digit divides the integer if the remainder of the integer divided by that digit is 0. The input is a positive integer `num` (between 1 and 10<sup>9</sup> inclusive) that does not contain the digit 0. The output is the count of digits in `num` that divide `num` without leaving a remainder.

Solution Approach

The provided code uses a iterative approach. It extracts each digit from the input number `num` one by one. For each digit, it checks if `num` is divisible by that digit. If it is, a counter is incremented. This process continues until all digits have been checked. Finally, the counter, representing the number of digits that divide `num`, is returned.

Step-by-Step Algorithm

  1. Step 1: Initialize a counter `count` to 0. This variable will store the number of digits that divide `num`.
  2. Step 2: Create a temporary variable `temp` and assign it the value of `num`. This is done to avoid modifying the original `num` during the digit extraction process.
  3. Step 3: Enter a `while` loop that continues as long as `temp` is greater than 0. This loop iterates through each digit of the number.
  4. Step 4: Extract the last digit of `temp` using the modulo operator (`temp % 10`) and store it in the `digit` variable.
  5. Step 5: Check if `num` is divisible by `digit` using the modulo operator (`num % digit == 0`). If it is, increment the `count`.
  6. Step 6: Remove the last digit from `temp` using integer division (`temp //= 10`).
  7. Step 7: Repeat steps 4-6 until `temp` becomes 0.
  8. Step 8: After the loop finishes, return the value of `count`.

Key Insights

  • Insight 1: The problem requires iterating through each digit of the input number. This can be efficiently done using modulo (%) and integer division (//) operations.
  • Insight 2: Extracting digits can be done by repeatedly taking the remainder when dividing by 10, then integer dividing by 10 to remove the last digit.
  • Insight 3: The constraint that `num` does not contain 0 simplifies the code, as we don't need to handle division by zero errors.

Complexity Analysis

Time Complexity: O(log(n))

Space Complexity: O(1)

Topics

This problem involves: Math.

Companies

Asked at: tcs.