Given two numbers, **N** and **M**, the task is to find two numbers made up of M as all its digits such that their difference is divisible by **N**. **Examples:**

Input:N = 8, M = 2Output:22 222Explanation:The difference between 222 and 22 (200) is divisible by 8

Input:N = 17, M = 6Output:6 66666666666666666

**Approach:**

In this problem, we have to find numbers consisting of only one unique digit. Let’s say **M** is equal to 2, then we have to find A and B from numbers like 2, 22, 222, 2222 …so on. The difference between A and B should be divisible by **N**. For this condition to satisfy, we have to pick A and B such that the remainder of A and B when it is divided by **N**, is the same.

For a digit of length **N+1** length, consisting of only one unique digit **M**, we will have **N+1** numbers. If we divide these **N+1 **numbers by **N** we will have **N+1** remainders which will range from **[0, N]**. Since the numbers can exceed the range of integer values, we are storing remainder-length of the number as key-value pairings in a Map. Once a remainder occurs with a value already paired in the Map, the current length and the mapped lengths are the lengths of the desired numbers.

The below code is the implementation of the above approach:

## C++

`// C++ implementation ` `// of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to implement ` `// the above approach ` `void` `findNumbers(` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `m = M; ` ` ` ` ` `// Hashmap to store ` ` ` `// remainder-length of the ` ` ` `// number as key-value pairs ` ` ` `map<` `int` `, ` `int` `> remLen; ` ` ` ` ` `int` `len, remainder; ` ` ` `// Iterate till N + 1 length ` ` ` `for` `(len = 1; len <= N + 1; ++len) { ` ` ` `remainder = M % N; ` ` ` `// Search remainder in the map ` ` ` `if` `(remLen.find(remainder) ` ` ` `== remLen.end()) ` ` ` `// If remainder is not ` ` ` `// already present insert ` ` ` `// the length for the ` ` ` `// corresponding remainder ` ` ` `remLen[remainder] = len; ` ` ` `else` ` ` `break` `; ` ` ` ` ` `// Keep increasing M ` ` ` `M = M * 10 + m; ` ` ` `// To keep M in range of integer ` ` ` `M = M % N; ` ` ` `} ` ` ` `// Length of one number ` ` ` `// is the current Length ` ` ` `int` `LenA = len; ` ` ` `// Length of the other number ` ` ` `// is the length paired with ` ` ` `// current remainder in map ` ` ` `int` `LenB = remLen[remainder]; ` ` ` ` ` `for` `(` `int` `i = 0; i < LenB; ++i) ` ` ` `cout << m; ` ` ` `cout << ` `" "` `; ` ` ` `for` `(` `int` `i = 0; i < LenA; ++i) ` ` ` `cout << m; ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 8, M = 2; ` ` ` ` ` `findNumbers(N, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to implement ` `// the above approach ` `static` `void` `findNumbers(` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `m = M; ` ` ` ` ` `// Hashmap to store ` ` ` `// remainder-length of the ` ` ` `// number as key-value pairs ` ` ` `Map<Integer, Integer> remLen = ` `new` `HashMap<>(); ` ` ` ` ` `int` `len, remainder = ` `0` `; ` ` ` ` ` `// Iterate till N + 1 length ` ` ` `for` `(len = ` `1` `; len <= N + ` `1` `; ++len) ` ` ` `{ ` ` ` `remainder = M % N; ` ` ` ` ` `// Search remainder in the map ` ` ` `if` `(!remLen.containsKey(remainder)) ` ` ` `{ ` ` ` ` ` `// If remainder is not ` ` ` `// already present insert ` ` ` `// the length for the ` ` ` `// corresponding remainder ` ` ` `remLen.put(remainder, len); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Keep increasing M ` ` ` `M = M * ` `10` `+ m; ` ` ` ` ` `// To keep M in range of integer ` ` ` `M = M % N; ` ` ` `} ` ` ` ` ` `// Length of one number ` ` ` `// is the current Length ` ` ` `int` `LenA = len; ` ` ` ` ` `// Length of the other number ` ` ` `// is the length paired with ` ` ` `// current remainder in map ` ` ` `int` `LenB = remLen.getOrDefault(remainder, ` `0` `); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < LenB; ++i) ` ` ` `System.out.print(m); ` ` ` `System.out.print(` `" "` `); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < LenA; ++i) ` ` ` `System.out.print(m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `8` `, M = ` `2` `; ` ` ` ` ` `findNumbers(N, M); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation ` `# of the above approach ` ` ` `# Function to implement ` `# the above approach ` `def` `findNumbers(N, M): ` ` ` ` ` `m ` `=` `M ` ` ` ` ` `# Hashmap to store ` ` ` `# remainder-length of the ` ` ` `# number as key-value pairs ` ` ` `remLen ` `=` `{} ` ` ` ` ` `# Iterate till N + 1 length ` ` ` `for` `len1 ` `in` `range` `(` `1` `, N ` `+` `1` `, ` `1` `): ` ` ` `remainder ` `=` `M ` `%` `N ` ` ` ` ` `# Search remainder in the map ` ` ` `if` `(remLen.get(remainder) ` `=` `=` `None` `): ` ` ` ` ` `# If remainder is not ` ` ` `# already present insert ` ` ` `# the length for the ` ` ` `# corresponding remainder ` ` ` `remLen[remainder] ` `=` `len1 ` ` ` `else` `: ` ` ` `break` ` ` ` ` `# Keep increasing M ` ` ` `M ` `=` `M ` `*` `10` `+` `m ` ` ` ` ` `# To keep M in range of integer ` ` ` `M ` `=` `M ` `%` `N ` ` ` ` ` `# Length of one number ` ` ` `# is the current Length ` ` ` `LenA ` `=` `len1 ` ` ` ` ` `# Length of the other number ` ` ` `# is the length paired with ` ` ` `# current remainder in map ` ` ` `LenB ` `=` `remLen[remainder] ` ` ` ` ` `for` `i ` `in` `range` `(LenB): ` ` ` `print` `(m, end ` `=` `"") ` ` ` `print` `(` `" "` `, end ` `=` `"") ` ` ` ` ` `for` `i ` `in` `range` `(LenA): ` ` ` `print` `(m, end ` `=` `"") ` ` ` ` ` `return` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `8` ` ` `M ` `=` `2` ` ` ` ` `findNumbers(N, M) ` ` ` `# This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to implement ` `// the above approach ` `static` `void` `findNumbers(` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `m = M; ` ` ` ` ` `// To store remainder-length of ` ` ` `// the number as key-value pairs ` ` ` `Dictionary<` `int` `, ` ` ` `int` `> remLen = ` `new` `Dictionary<` `int` `, ` ` ` `int` `>(); ` ` ` ` ` `int` `len, remainder = 0; ` ` ` ` ` `// Iterate till N + 1 length ` ` ` `for` `(len = 1; len <= N + 1; ++len) ` ` ` `{ ` ` ` `remainder = M % N; ` ` ` ` ` `// Search remainder in the map ` ` ` `if` `(!remLen.ContainsKey(remainder)) ` ` ` `{ ` ` ` ` ` `// If remainder is not ` ` ` `// already present insert ` ` ` `// the length for the ` ` ` `// corresponding remainder ` ` ` `remLen.Add(remainder, len); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Keep increasing M ` ` ` `M = M * 10 + m; ` ` ` ` ` `// To keep M in range of integer ` ` ` `M = M % N; ` ` ` `} ` ` ` ` ` `// Length of one number ` ` ` `// is the current Length ` ` ` `int` `LenA = len; ` ` ` ` ` `// Length of the other number ` ` ` `// is the length paired with ` ` ` `// current remainder in map ` ` ` `int` `LenB = remLen[remainder]; ` ` ` ` ` `for` `(` `int` `i = 0; i < LenB; ++i) ` ` ` `Console.Write(m); ` ` ` ` ` `Console.Write(` `" "` `); ` ` ` ` ` `for` `(` `int` `i = 0; i < LenA; ++i) ` ` ` `Console.Write(m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 8, M = 2; ` ` ` ` ` `findNumbers(N, M); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

22 222

**Time Complexity:** *O(N)* **Auxiliary Space:** *O(1)*

## Recommended Posts:

- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count of N-digit numbers having digit XOR as single digit
- Find two numbers such that difference of their squares equal to N
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find two distinct numbers such that their LCM lies in given range
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Find two numbers with sum N such that neither of them contains digit K
- Print all the combinations of N elements by changing sign such that their sum is divisible by M
- Find all matrix elements which are minimum in their row and maximum in their column
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Smallest n digit number divisible by given three numbers
- Count n digit numbers divisible by given number

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.