Open In App

Smallest length of number divisible by K formed by using D only

Improve
Improve
Like Article
Like
Save
Share
Report

Given 2 integers D and K, the task is to find the minimum length of a number formed by repeating D that will be divisible by K. If no such number exists, print -1.

Examples:

Input : D = 1, K = 37
Output : 3
Explanation : The minimum number formed by repeating 1 that is divisible by 37 is 111

Input : D = 2, K = 36
Output : -1

 

Naive Approach : The idea is to keep forming the number until it becomes divisible by K or it exceeds the range. 

Time Complexity : O(10^18)
Auxiliary Space : O(1)

Efficient Approach : The idea is to store the remainders that are produced by dividing the number with K and storing the remainders in an array. And when the same remainder appears again, it can be concluded that no such number exists. Follow the steps below to solve the problem:

  • Initialize the variables cnt as 0 to store the answer and m to store the number.
  • Initialize the vector v[] of size K to store the remainders and set the value of v[m] as 1.
  • Iterate in a while loop and perform the following steps
    • If m is equal to 0, then return the value of cnt as the answer.
    • Set the value of m as (((m * (10 % k)) % k) + (d % k)) % k.
    • If v[m] is equal to 1, then return -1 as no such number is possible.
    • Set the value of v[m] as 1 and increase the value of cnt by 1.
  • After performing the above steps, return the value of -1.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to form the smallest number
// possible
int smallest(int k, int d)
{
    int cnt = 1;
    int m = d % k;
 
    // Array to mark the remainders
    // counted already
    vector<int> v(k, 0);
    v[m] = 1;
 
    // Iterate over the range
    while (1) {
        if (m == 0)
            return cnt;
        m = (((m * (10 % k)) % k) + (d % k)) % k;
 
        // If that remainder is already found,
        // return -1
        if (v[m] == 1)
            return -1;
        v[m] = 1;
        cnt++;
    }
    return -1;
}
 
// Driver Code
int main()
{
 
    int d = 1;
    int k = 41;
    cout << smallest(k, d);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to form the smallest number
// possible
static int smallest(int k, int d)
{
    int cnt = 1;
    int m = d % k;
 
    // Array to mark the remainders
    // counted already
    int[] v = new int[k];
    Arrays.fill(v, 0);
    v[m] = 1;
 
    // Iterate over the range
    while (1 != 0)
    {
        if (m == 0)
            return cnt;
             
        m = (((m * (10 % k)) % k) + (d % k)) % k;
 
        // If that remainder is already found,
        // return -1
        if (v[m] == 1)
            return -1;
             
        v[m] = 1;
        cnt++;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int d = 1;
    int k = 41;
     
    System.out.println(smallest(k, d));
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python program for the above approach;
 
# Function to form the smallest number
# possible
def smallest(k, d):
    cnt = 1
    m = d % k
 
    # Array to mark the remainders
    # counted already
    v = [0 for i in range(k)];
    v[m] = 1
 
    # Iterate over the range
    while (1):
        if (m == 0):
            return cnt
        m = (((m * (10 % k)) % k) + (d % k)) % k
 
        # If that remainder is already found,
        # return -1
        if (v[m] == 1):
            return -1
        v[m] = 1
        cnt += 1
 
    return -1
 
# Driver Code
d = 1
k = 41
print(smallest(k, d))
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to form the smallest number
// possible
static int smallest(int k, int d)
{
    int cnt = 1;
    int m = d % k;
 
    // Array to mark the remainders
    // counted already
    int [] v = new int[k];
    for(int i=0;i<k;i++)
        v[i] = 0;
    v[m] = 1;
 
    // Iterate over the range
    while (true) {
        if (m == 0)
            return cnt;
        m = (((m * (10 % k)) % k) + (d % k)) % k;
 
        // If that remainder is already found,
        // return -1
        if ( v[m] == 1)
            return -1;
        
        v[m] = 1;
        cnt++;
    }
   // return -1;
}
 
// Driver Code
public static void Main()
{
 
    int d = 1;
    int k = 41;
    Console.Write(smallest(k, d));
}
}
 
// This code is contributed by bgangwar59.


Javascript




<script>
        // JavaScript program for the above approach;
 
        // Function to form the smallest number
        // possible
        function smallest(k, d) {
            let cnt = 1;
            let m = d % k;
 
            // Array to mark the remainders
            // counted already
            let v = new Array(k).fill(0);
            v[m] = 1;
 
            // Iterate over the range
            while (1) {
                if (m == 0)
                    return cnt;
                m = (((m * (10 % k)) % k) + (d % k)) % k;
 
                // If that remainder is already found,
                // return -1
                if (v[m] == 1)
                    return -1;
                v[m] = 1;
                cnt++;
            }
            return -1;
        }
 
        // Driver Code
        let d = 1;
        let k = 41;
        document.write(smallest(k, d));
         
   // This code is contributed by Potta Lokesh
    </script>


Output

5

Time Complexity : O(K)
Auxiliary Space : O(K)



Last Updated : 04 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads