- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two numbers A and B, we have to find the minimum positive number M so that M is divisible by A and the sum of the digits of M is same as B. So, if there is no such result, then return -1.

So, if the input is like A = 50, B = 2, then the output will be 200 as this is divisible by 50 and sum of its digit = 2 + 0 + 0 = 2.

To solve this, we will follow these steps −

Define one element type container, that contains two numbers a and b and one string

que := a new list

elem := a new element with (0, 0, blank string)

visited[0, 0] := 1

insert elem at the end of que

while size of que > 0, do

temp_elem := delete first element from que

if temp_elem.a is 0 and temp_elem.b is b, then

return integer of temp_elem.string

for i in range 0 to 9, do

x :=(temp_elem.a * 10 + i) mod a

y := temp_elem.b + i

if y <= b and visited[x, y] is False, then

visited[x, y] := 1

insert new element with x, y and temp_elem.string concatenate i into que

return -1

Let us see the following implementation to get better understanding −

visited = [[0 for x in range(501)] for y in range(5001)] class Element: def __init__(self, a, b, string): self.a = a self.b = b self.string = string def get_number(a, b): que = [] elem = Element(0, 0, "") visited[0][0] = 1 que.append(elem) while len(que) > 0: temp_elem = que.pop(0) if temp_elem.a == 0 and temp_elem.b == b: return int(temp_elem.string) for i in range(0, 10): x = (temp_elem.a * 10 + i) % a y = temp_elem.b + i if y <= b and visited[x][y] == False: visited[x][y] = 1 que.append(Element(x, y, temp_elem.string + str(i))) return -1 a, b = 50, 2 print(get_number(a, b))

50, 2

200

- Related Questions & Answers
- Add N digits to A such that it is divisible by B after each addition?
- Minimum positive integer divisible by C and is not in range [A, B] in C++
- Add N digits to A such that it is divisible by B after each addition in C++?
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Maximum positive integer divisible by C and is in the range [A, B] in C++
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- C Program to check if a number is divisible by sum of its digits
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Count minimum bits to flip such that XOR of A and B equal to C in C++
- Find a Number X whose sum with its digits is equal to N in C++
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- C Program to check if a number is divisible by any of its digits
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++

Advertisements