Leetcode 728: Self Dividing Numbers

Category: Easy

Problem

self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

https://leetcode.com/problems/self-dividing-numbers/

Examples

Example 1:

Note

  • The boundaries of each input argument are 1 <= left <= right <= 10000.

Solution Approach

In this problem, we are to find the numbers whose digits are self dividing i.e the digit divide the number without any remainder in the given interval. We have to return this list of all number which satisfy these criteria as the answer. 

I am using the same logic that I used in the Palindrome Number problem to get the digit of a number i.e by using the mod operation with 10. We have to keep in mind that the number should not contain 0 as stated in the problem.

Coming to the solution part of the problem we create a list to store the value of all the number which satisfy the criteria and I named the variable as outputList. I ran the array through the left to right numbers as asked in question but the checking part is done by a Util method called isSelfDividing. The method takes in a number, fragments it digits and checks it if that digit divides the number or is it 0. If these conditions are true then we straightaway return false otherwise we will keep looking till the number becomes 0 and then return true.

In the method which calls our util method, we add the number to the list if our isSelfDividing method returns true. Then at the end, we just return the same list as the answer.

Solution code

For more Leetcode explained solutions visit Leetcode Solutions. If you like capture the flag challenges visit here.

Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via Contact page I will surely respond. Happy Leetcoding 

Leave a Comment

Your email address will not be published. Required fields are marked *