KatInterview

728. Self Dividing Numbers

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Iteration and Digit Extraction

This problem asks us to find all "self-dividing numbers" in a given range. A number is self-dividing if it's divisible by every digit it contains, and it does not contain the digit zero.

Solution

Self Dividing Numbers in Python

class Solution:
    def selfDividingNumbers(self, left: int, right: int) -> List[int]:
        result = []
        # Iterate through each number in the range [left, right].
        for n in range(left, right + 1):
            is_sdn = True
            # Convert number to string to iterate through its digits.
            for digit_char in str(n):
                # A self-dividing number cannot contain the digit zero.
                if digit_char == '0':
                    is_sdn = False
                    break
                
                # The original number must be divisible by the digit.
                if n % int(digit_char) != 0:
                    is_sdn = False
                    break
            
            # If all checks passed, add the number to the result list.
            if is_sdn:
                result.append(n)
        
        return result

Self Dividing Numbers in Java

class Solution {
    public List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> result = new ArrayList<>();
        // Iterate from left to right, inclusive.
        for (int i = left; i <= right; i++) {
            // Check if the current number is self-dividing.
            if (isSelfDividing(i)) {
                result.add(i);
            }
        }
        return result;
    }

    // Helper function to check if a number is self-dividing.
    private boolean isSelfDividing(int num) {
        int originalNum = num;
        while (num > 0) {
            int digit = num % 10;
            // Condition 1: Digit cannot be zero.
            if (digit == 0) {
                return false;
            }
            // Condition 2: Original number must be divisible by the digit.
            if (originalNum % digit != 0) {
                return false;
            }
            num /= 10;
        }
        // If all digits satisfy the conditions, it's a self-dividing number.
        return true;
    }
}

Self Dividing Numbers in C++

class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        vector<int> result;
        // Iterate through each number in the range [left, right].
        for (int i = left; i <= right; ++i) {
            int num = i;
            bool is_self_dividing = true;
            
            // Process each digit of the number.
            while (num > 0) {
                int digit = num % 10;
                
                // A self-dividing number cannot contain the digit zero.
                if (digit == 0) {
                    is_self_dividing = false;
                    break;
                }
                
                // The original number must be divisible by the digit.
                if (i % digit != 0) {
                    is_self_dividing = false;
                    break;
                }
                
                num /= 10;
            }
            
            // If all checks passed, add the number to the result list.
            if (is_self_dividing) {
                result.push_back(i);
            }
        }
        return result;
    }
};

Self Dividing Numbers in TypeScript

function selfDividingNumbers(left: number, right: number): number[] {
    const result: number[] = [];

    // Iterate through each number in the range [left, right].
    for (let i = left; i <= right; i++) {
        let num = i;
        let is_sdn = true;
        
        // Process each digit of the number.
        while (num > 0) {
            const digit = num % 10;

            // A self-dividing number cannot contain the digit zero.
            if (digit === 0) {
                is_sdn = false;
                break;
            }
            
            // The original number must be divisible by the digit.
            if (i % digit !== 0) {
                is_sdn = false;
                break;
            }
            
            num = Math.floor(num / 10);
        }
        
        // If all checks passed, add the number to the result list.
        if (is_sdn) {
            result.push(i);
        }
    }
    
    return result;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.