# Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B

Given two integers **A** and **B**, the task is to find the minimum positive integer **N** such that **N** is divisible by **A** and the sum of the digits of **N** is equal to **B**. If number is not found then print **-1**.

**Examples:**

Input:A = 20, B = 30

Output:49980

49980 is divisible by 20 and sum of its digit = 4 + 9 + 9 + 8 + 0 = 30

Input:A = 5, B = 2

Output:20

**Approach:**

- Create empty queue
**q**that stores the value of**A**and**B**and output number as a string and create integer type 2-D array**visited[][]**that stores the visited digit. - Insert Node into queue and check if queue is non-empty.
- While the queue is non-empty, pop an element from the queue and for every digit from
**1**to**9**, concatenate the digit after the string num and check whether the number formed is the required number. - If the required number is found, print the number.
- Else repeat the steps while the number is less than
**B**and the queue is non-empty while pushing the non-visited number to the queue.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Array that stores visited digits ` `int` `visited[501][5001]; ` ` ` `// Structure for queue Node. ` `struct` `Node { ` ` ` `int` `a, b; ` ` ` `string str; ` `}; ` ` ` `// Function to return the minimum number such that it is ` `// divisible by 'a' and sum of its digits is equals to 'b' ` `int` `findNumber(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Create queue ` ` ` `queue<Node> q; ` ` ` ` ` `// Initially queue is empty ` ` ` `Node temp = Node{ 0, 0, ` `""` `}; ` ` ` ` ` `// Initialize visited to 1 ` ` ` `visited[0][0] = 1; ` ` ` ` ` `// Push temp in queue ` ` ` `q.push(temp); ` ` ` ` ` `// While queue is not empty ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `// Get the front of the queue and pop it ` ` ` `Node u = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// If popped element is the required number ` ` ` `if` `(u.a == 0 && u.b == b) ` ` ` ` ` `// Parse int from string and return it ` ` ` `return` `std::stoi(u.str); ` ` ` ` ` `// Loop for each digit and check the sum ` ` ` `// If not visited then push it to the queue ` ` ` `for` `(` `int` `i = 0; i < 10; i++) { ` ` ` `int` `dd = (u.a * 10 + i) % a; ` ` ` `int` `ss = u.b + i; ` ` ` `if` `(ss <= b && !visited[dd][ss]) { ` ` ` `visited[dd][ss] = 1; ` ` ` `q.push(Node{ dd, ss, u.str + ` `char` `(` `'0'` `+ i) }); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Required number not found return -1. ` ` ` `return` `-1; ` `} ` ` ` `// Driver code. ` `int` `main() ` `{ ` ` ` `int` `a = 25, b = 1; ` ` ` `cout << findNumber(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` `class` `Solution ` `{ ` ` ` `// Array that stores visited digits ` `static` `int` `visited[][]= ` `new` `int` `[` `501` `][` `5001` `]; ` ` ` `// Structure for queue Node. ` `static` `class` `Node { ` ` ` `int` `a, b; ` ` ` `String str; ` ` ` `Node(` `int` `a1,` `int` `b1,String s) ` ` ` `{ ` ` ` `a=a1; ` ` ` `b=b1; ` ` ` `str=s; ` ` ` `} ` `} ` ` ` `// Function to return the minimum number such that it is ` `// divisible by 'a' and sum of its digits is equals to 'b' ` `static` `int` `findNumber(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Create queue ` ` ` `Queue<Node> q= ` `new` `LinkedList<Node>(); ` ` ` ` ` `// Initially queue is empty ` ` ` `Node temp =` `new` `Node( ` `0` `, ` `0` `, ` `""` `); ` ` ` ` ` `// Initialize visited to 1 ` ` ` `visited[` `0` `][` `0` `] = ` `1` `; ` ` ` ` ` `// Push temp in queue ` ` ` `q.add(temp); ` ` ` ` ` `// While queue is not empty ` ` ` `while` `(q.size()!=` `0` `) { ` ` ` ` ` `// Get the front of the queue and pop it ` ` ` `Node u = q.peek(); ` ` ` `q.remove(); ` ` ` ` ` `// If popped element is the required number ` ` ` `if` `(u.a == ` `0` `&& u.b == b) ` ` ` ` ` `// Parse int from string and return it ` ` ` `return` `Integer.parseInt(u.str); ` ` ` ` ` `// Loop for each digit and check the sum ` ` ` `// If not visited then push it to the queue ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `10` `; i++) { ` ` ` `int` `dd = (u.a * ` `10` `+ i) % a; ` ` ` `int` `ss = u.b + i; ` ` ` `if` `(ss <= b && visited[dd][ss]==` `0` `) { ` ` ` `visited[dd][ss] = ` `1` `; ` ` ` `q.add(` `new` `Node( dd, ss, u.str + (` `char` `)(` `'0'` `+ i) )); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Required number not found return -1. ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code. ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `//initilize visited ` ` ` `for` `(` `int` `i=` `0` `;i<` `500` `;i++) ` ` ` `for` `(` `int` `j=` `0` `;j<` `500` `;j++) ` ` ` `visited[i][j]=` `0` `; ` ` ` ` ` `int` `a = ` `25` `, b = ` `1` `; ` ` ` `System.out.println(findNumber(a, b)); ` ` ` `} ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

# Array that stores visited digits

visited = [[0 for x in range(501)]

for y in range(5001)]

# Structure for queue Node.

class Node:

def __init__(self, a, b, string):

self.a = a

self.b = b

self.string = string

# Function to return the minimum number

# such that it is divisible by ‘a’ and

# sum of its digits is equals to ‘b’

def findNumber(a, b):

# Use list as queue

q = []

# Initially queue is empty

temp = Node(0, 0, “”)

# Initialize visited to 1

visited[0][0] = 1

# Push temp in queue

q.append(temp)

# While queue is not empty

while len(q) > 0:

# Get the front of the queue

# and pop it

u = q.pop(0)

# If popped element is the

# required number

if u.a == 0 and u.b == b:

# Parse int from string

# and return it

return int(u.string)

# Loop for each digit and check the sum

# If not visited then push it to the queue

for i in range(0, 10):

dd = (u.a * 10 + i) % a

ss = u.b + i

if ss <= b and visited[dd][ss] == False: visited[dd][ss] = 1 q.append(Node(dd, ss, u.string + str(i))) # Required number not found return -1. return -1 # Driver code. if __name__ == "__main__": a, b = 25, 1 print(findNumber(a, b)) # This code is contributed by Rituraj Jain [tabbyending]

**Output:**

100

## Recommended Posts:

- Find minimum positive integer x such that a(x^2) + b(x) + c >= k
- Find maximum product of digits among numbers less than or equal to N
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find the minimum number of preprocess moves required to make two strings equal
- Find the minimum number of operations required to make all array elements equal
- Minimum number of digits to be removed so that no two consecutive digits are same
- Smallest number with sum of digits as N and divisible by 10^N
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Check if N is divisible by a number which is composed of the digits from the set {A, B}
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Find permutation of n which is divisible by 3 but not divisible by 6
- Smallest number k such that the product of digits of k is equal to n
- Find a positive number M such that gcd(N^M, N&M) is maximum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.