Given two integers x and k, find the smallest k-digit number that is divisible by x.
Note: It is guaranteed that the number of digits in x is less than or equal to k.
Examples:
Input: x = 6, k = 1
Output: 6
Explanation: 6 is the smallest 1 digit number which is divisible by 6.Input: x = 5, k = 2
Output: 10
Explanation: 10 is the smallest 2 digit number which is divisible by 5.
Table of Content
[Naive Approach] Check Every K-Digit Number - O(10 ^ k) Time and O(1) Space
The idea is to start from the smallest k-digit number and check every number one by one. As soon as a number divisible by x is found, return it.
Working of Approach:
- Compute the smallest k-digit number (10^(k-1)).
- Compute the largest k-digit number (10^k - 1).
- Iterate from the smallest to the largest number.
- Return the first number divisible by x.
- If no such number exists, return -1.
#include <bits/stdc++.h>
using namespace std;
int smallestKDigitNum(int x, int k)
{
// Find the smallest k-digit number.
int start = 1;
for (int i = 1; i < k; i++)
start *= 10;
// Find the largest k-digit number.
int end = start * 10 - 1;
// Check every k-digit number.
for (int num = start; num <= end; num++)
{
// Return the first divisible number.
if (num % x == 0)
return num;
}
return -1;
}
int main()
{
int x = 5, k = 2;
cout << smallestKDigitNum(x, k);
return 0;
}
import java.util.*;
public class GFG {
public static int smallestKDigitNum(int x, int k)
{
// Find the smallest k-digit number.
int start = 1;
for (int i = 1; i < k; i++)
start *= 10;
// Find the largest k-digit number.
int end = start * 10 - 1;
// Check every k-digit number.
for (int num = start; num <= end; num++) {
// Return the first divisible number.
if (num % x == 0)
return num;
}
return -1;
}
public static void main(String[] args)
{
int x = 5, k = 2;
System.out.println(smallestKDigitNum(x, k));
}
}
def smallestKDigitNum(x, k):
# Find the smallest k-digit number.
start = 1
for i in range(1, k):
start *= 10
# Find the largest k-digit number.
end = start * 10 - 1
# Check every k-digit number.
for num in range(start, end + 1):
# Return the first divisible number.
if num % x == 0:
return num
return -1
if __name__ == "__main__":
x = 5
k = 2
print(smallestKDigitNum(x, k))
using System;
public class GFG {
public static int smallestKDigitNum(int x, int k)
{
// Find the smallest k-digit number.
int start = 1;
for (int i = 1; i < k; i++)
start *= 10;
// Find the largest k-digit number.
int end = start * 10 - 1;
// Check every k-digit number.
for (int num = start; num <= end; num++) {
// Return the first divisible number.
if (num % x == 0)
return num;
}
return -1;
}
public static void Main()
{
int x = 5, k = 2;
Console.WriteLine(smallestKDigitNum(x, k));
}
}
function smallestKDigitNum(x, k)
{
// Find the smallest k-digit number.
let start = 1;
for (let i = 1; i < k; i++)
start *= 10;
// Find the largest k-digit number.
let end = start * 10 - 1;
// Check every k-digit number.
for (let num = start; num <= end; num++) {
// Return the first divisible number.
if (num % x === 0)
return num;
}
return -1;
}
// Driver Code
let x = 5, k = 2;
console.log(smallestKDigitNum(x, k));
Output
10
[Expected Approach] Find First Multiple using Ceiling Division - O(k) Time and O(1) Space
Compute smallest K-digit number
start = (1000...(K-1)times)
If start % x is 0, then result is start. Else there must be a number in range [start...start+x] divisible by x. We can find the multiple using ((start + x - 1) / x) * x.
Let us understand with an example
k = 3, x = 23
start = 100
((100 + 23 - 1) / 23) * 23
= (122 / 23) * 23
= 5 * 23
= 115
#include <bits/stdc++.h>
using namespace std;
int smallestKDigitNum(int x, int k)
{
int start = 1;
// Calculate the smallest K-digit number.
for (int i = 1; i < k; i++)
{
start *= 10;
}
// If the smallest K-digit number is divisible by X, return it.
if (start % x == 0)
return start;
// Find the smallest multiple of X greater than start.
return ((start + x - 1) / x) * x;
}
int main()
{
int x = 5, k = 2;
cout << smallestKDigitNum(x, k);
return 0;
}
import java.util.*;
public class GFG {
public static int smallestKDigitNum(int x, int k)
{
int start = 1;
// Calculate the smallest K-digit number.
for (int i = 1; i < k; i++) {
start *= 10;
}
// If the smallest K-digit number is divisible by X,
// return it.
if (start % x == 0)
return start;
// Find the smallest multiple of X greater than
// start.
return ((start + x - 1) / x) * x;
}
public static void main(String[] args)
{
int x = 5, k = 2;
System.out.println(smallestKDigitNum(x, k));
}
}
def smallestKDigitNum(x, k):
start = 1
# Calculate the smallest K-digit number.
for i in range(1, k):
start *= 10
# If the smallest K-digit number is divisible by X, return it.
if start % x == 0:
return start
# Find the smallest multiple of X greater than start.
return ((start + x - 1) // x) * x
if __name__ == '__main__':
x = 5
k = 2
print(smallestKDigitNum(x, k))
using System;
class GFG {
static int smallestKDigitNum(int x, int k)
{
int start = 1;
// Calculate the smallest K-digit number.
for (int i = 1; i < k; i++) {
start *= 10;
}
// If the smallest K-digit number is divisible by X,
// return it.
if (start % x == 0)
return start;
// Find the smallest multiple of X greater than
// start.
return ((start + x - 1) / x) * x;
}
static void Main()
{
int x = 5, k = 2;
Console.WriteLine(smallestKDigitNum(x, k));
}
}
function smallestKDigitNum(x, k)
{
let start = 1;
// Calculate the smallest K-digit number.
for (let i = 1; i < k; i++) {
start *= 10;
}
// If the smallest K-digit number is divisible by X,
// return it.
if (start % x === 0)
return start;
// Find the smallest multiple of X greater than start.
return (Math.floor((start + x - 1) / x)) * x;
}
// Driver Code
let x = 5, k = 2;
console.log(smallestKDigitNum(x, k));
Output
10