Skip to content
Related Articles

Related Articles

Find two numbers made up of a given digit such that their difference is divisible by N

Improve Article
Save Article
Like Article
  • Last Updated : 20 Jul, 2021

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 = 2 
Output: 22 222 
Explanation: The difference between 222 and 22 (200) is divisible by 8
Input: N = 17, M = 6 
Output: 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;
}

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

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

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

Javascript




<script>
    // Javascript implementation of the above approach
     
    // Function to implement
    // the above approach
    function findNumbers(N, M)
    {
        let m = M;
 
        // To store remainder-length of
        // the number as key-value pairs
        let remLen = new Map();
 
        let 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.has(remainder))
            {
 
                // If remainder is not
                // already present insert
                // the length for the
                // corresponding remainder
                remLen.set(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
        let LenA = len;
 
        // Length of the other number
        // is the length paired with
        // current remainder in map
        let LenB = remLen.get(remainder);
 
        for(let i = 0; i < LenB; ++i)
            document.write(m);
 
        document.write(" ");
 
        for(let i = 0; i < LenA; ++i)
            document.write(m);
    }
     
    let N = 8, M = 2;
   
    findNumbers(N, M);
 
// This code is contributed by divyeshrabadiya07.
</script>
Output: 
22 222

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!