LeetCode 2520: Count the Digits That Divide a Number Solution

Master LeetCode problem 2520 (Count the Digits That Divide a Number), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2520. Count the Digits That Divide a Number

Easy

Problem Explanation

Explanation:

To solve this problem, we iterate through each digit of the given number and check if the number is divisible by that digit. If it is, we increment a counter. Finally, we return the counter as the result.

  • Initialize a counter variable to keep track of the count of divisible digits.
  • Iterate through each digit of the given number.
  • Check if the digit is not zero and the number is divisible by that digit.
  • If the above conditions are met, increment the counter.
  • Return the counter as the result.

Time Complexity:

The time complexity of this algorithm is O(log n), where n is the given number. This is because we are iterating through each digit of the number.

Space Complexity:

The space complexity of this algorithm is O(1) as we are using only a constant amount of space.

:

Solution Code

class Solution {
    public int countDividingDigits(int num) {
        int count = 0;
        int n = num;
        while (n > 0) {
            int digit = n % 10;
            if (digit != 0 && num % digit == 0) {
                count++;
            }
            n /= 10;
        }
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2520 (Count the Digits That Divide a Number)?

This page provides optimized solutions for LeetCode problem 2520 (Count the Digits That Divide a Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2520 (Count the Digits That Divide a Number)?

The time complexity for LeetCode 2520 (Count the Digits That Divide a Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2520 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2520 in Java, C++, or Python.

Back to LeetCode Solutions