Category: Easy

**Problem**

A *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 == 0`

, `128 % 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:**

1 2 3 | Input: left = 1, right = 22 Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] |

**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**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Solution { public List<Integer> selfDividingNumbers(int left, int right) { List<Integer> outputList = new ArrayList<>(); for (int number=left; number<=right; number++){ if(isSelfDividing(number)){ outputList.add(number); } } return outputList; } public boolean isSelfDividing(int number){ int originalNumber = number; while(originalNumber!=0){ int d = originalNumber % 10; if(d==0 || number%d!=0){ return false; } originalNumber /= 10; } return true; } } |

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