Smallest length of number divisible by K formed by using D only
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++
#include <bits/stdc++.h>
using namespace std;
int smallest( int k, int d)
{
int cnt = 1;
int m = d % k;
vector< int > v(k, 0);
v[m] = 1;
while (1) {
if (m == 0)
return cnt;
m = (((m * (10 % k)) % k) + (d % k)) % k;
if (v[m] == 1)
return -1;
v[m] = 1;
cnt++;
}
return -1;
}
int main()
{
int d = 1;
int k = 41;
cout << smallest(k, d);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int smallest( int k, int d)
{
int cnt = 1 ;
int m = d % k;
int [] v = new int [k];
Arrays.fill(v, 0 );
v[m] = 1 ;
while ( 1 != 0 )
{
if (m == 0 )
return cnt;
m = (((m * ( 10 % k)) % k) + (d % k)) % k;
if (v[m] == 1 )
return - 1 ;
v[m] = 1 ;
cnt++;
}
}
public static void main(String[] args)
{
int d = 1 ;
int k = 41 ;
System.out.println(smallest(k, d));
}
}
|
Python3
def smallest(k, d):
cnt = 1
m = d % k
v = [ 0 for i in range (k)];
v[m] = 1
while ( 1 ):
if (m = = 0 ):
return cnt
m = (((m * ( 10 % k)) % k) + (d % k)) % k
if (v[m] = = 1 ):
return - 1
v[m] = 1
cnt + = 1
return - 1
d = 1
k = 41
print (smallest(k, d))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int smallest( int k, int d)
{
int cnt = 1;
int m = d % k;
int [] v = new int [k];
for ( int i=0;i<k;i++)
v[i] = 0;
v[m] = 1;
while ( true ) {
if (m == 0)
return cnt;
m = (((m * (10 % k)) % k) + (d % k)) % k;
if ( v[m] == 1)
return -1;
v[m] = 1;
cnt++;
}
}
public static void Main()
{
int d = 1;
int k = 41;
Console.Write(smallest(k, d));
}
}
|
Javascript
<script>
function smallest(k, d) {
let cnt = 1;
let m = d % k;
let v = new Array(k).fill(0);
v[m] = 1;
while (1) {
if (m == 0)
return cnt;
m = (((m * (10 % k)) % k) + (d % k)) % k;
if (v[m] == 1)
return -1;
v[m] = 1;
cnt++;
}
return -1;
}
let d = 1;
let k = 41;
document.write(smallest(k, d));
</script>
|
Time Complexity : O(K)
Auxiliary Space : O(K)
Last Updated :
04 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...