Count Divisible Digits in Number

🧠 Count the Digits That Divide a Number | Python Problem Statement Given an integer n, return the number of digits in n that evenly divide n. 📌 Examples Input: n=248 Output: 3 Explanation: 248 is divisible by 8, 4, 2, hence the output is 3 🔍 Approach 1. Extract the last digit using % 10 2. If the digit is non-zero and divides the original number, increment the count 3. Remove the last digit using // 10 4. Stop when the number becomes 0 🧪 Python Code def countDigits(num: int) -> int:   original = num   countNum = 0   while num > 0:     digit = num % 10     if digit != 0 and original % digit == 0:       countNum += 1     num //= 10   return countNum ⏱️ Complexity Time: O(log₁₀ n) Space: O(1) 🧩 Edge Cases (Interview Tip) 1. Digits equal to 0 → ignored (division by zero) 2. Single-digit numbers → handled naturally 3. Negative numbers → clarify constraint or use abs(n) #Python #DSA #CodingInterview #InterviewPreparation #CollegeStudents #CampusPlacements #LearningInPublic

To view or add a comment, sign in

Explore content categories