Find all the possible numbers in a range that can be evenly divided by its digits
Last Updated :
17 Nov, 2021
Given two integers n and m, which represent a range where n is the lower bound and m is the upper bound respectively. The task is to find all the possible numbers that lie between n and m that can be evenly divided by their digits.
Examples:
Input: n = 1, m = 15
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]
Explanation:
The numbers in the array can be divided evenly by their digits except
2 numbers 10 and 13, which lie between 1 and 15.
10 can be broken into digits 1 and 0.
10 % 1 == 0 but 10 % 0 != 0.
13 can be broken into digits 1 and 3.
13 % 1 == 0 but 13 % 3 != 0.
Input: n = 21, m = 25
Output: [22, 24]
Explanation:
The numbers in the array can be divided evenly by their digits except
there are 3 numbers; 21, 23 and 25 lie between 21 and 25.
21 can be broken into digits 2 and 1.
21 % 2 != 0 and 21 % 1 != 0
23 can be broken into digits 2 and 3.
23 % 2 != 0 and 23 % 3 != 0.
25 can be broken into digits 2 and 5.
25 % 2 != 0 but 25 % 5 == 0.
Approach:
The main idea is to iterate each and every number from left to right. For each number, convert it into a string followed by a character array and check whether it contains 0. If it contains 0, ignore it. Otherwise, for each number check, whether the number is evenly divided by its digits or not. If the number is evenly divided by its digits, then add it to the list, else discard it. Finally, return the list.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
#include<sstream>
#include<string>
using namespace std;
bool isDivisible( int num)
{
ostringstream str1;
str1 << num;
string str2 = str1.str();
for ( char c : str2 )
{
if (c == '0' ||
num % (c - '0' ) > 0)
{
return false ;
}
}
return true ;
}
vector< int > selfDividingNumber( int left,
int right)
{
vector< int >list ;
for ( int i = left; i <= right; i++)
{
if (isDivisible(i))
{
list.push_back(i);
}
}
return list;
}
int main()
{
int n1 = 1, m1 = 15;
vector< int > ans =
(selfDividingNumber(n1, m1));
for ( auto i = ans.begin();
i != ans.end(); i++)
cout << (*i) << " " ;
}
|
Java
import java.util.*;
class GFG {
static List<Integer> selfDividingNumber( int left,
int right)
{
List<Integer> list = new ArrayList<Integer>();
for ( int i = left; i <= right; i++) {
if (isDivisible(i)) {
list.add(i);
}
}
return list;
}
static boolean isDivisible( int num)
{
for ( char c : String.valueOf(num).toCharArray()) {
if (c == '0' || num % (c - '0' ) > 0 ) {
return false ;
}
}
return true ;
}
public static void main(String args[])
{
int n1 = 1 , m1 = 15 ;
System.out.print(selfDividingNumber(n1, m1));
}
}
|
Python3
def selfDividingNumber(left, right) :
array_list = [];
for i in range (left, right + 1 ) :
if (isDivisible(i)) :
array_list.append(i);
return array_list;
def isDivisible(num) :
for c in list ( str (num)) :
if (c = = '0' or num % ( ord (c) - ord ( '0' )) > 0 ):
return False ;
return True ;
if __name__ = = "__main__" :
n1 = 1 ; m1 = 15 ;
print (selfDividingNumber(n1, m1));
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static List< int > selfDividingNumber( int left,
int right)
{
List< int > list = new List< int >();
for ( int i = left; i <= right; i++)
{
if (isDivisible(i))
{
list.Add(i);
}
}
return list;
}
static bool isDivisible( int num)
{
foreach ( char c in String.Join( "" , num).ToCharArray())
{
if (c == '0' || num % (c - '0' ) > 0)
{
return false ;
}
}
return true ;
}
public static void Main(String []args)
{
int n1 = 1, m1 = 15;
List< int > t = selfDividingNumber(n1, m1);
foreach ( int val in t)
Console.Write(val + ", " );
}
}
|
Javascript
<script>
function selfDividingNumber(left, right)
{
let list = [];
for (let i = left; i <= right; i++) {
if (isDivisible(i)) {
list.push(i);
}
}
return list;
}
function isDivisible(numm)
{
let num = numm.toString();
for (let c in num.split( '' )) {
if (num == '0' || num % (num - '0' ) > 0) {
return false ;
}
}
return true ;
}
let n1 = 1, m1 = 15;
document.write(selfDividingNumber(n1, m1));
</script>
|
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]
Time Complexity: O(N), where N is the number of integers from left to right.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...