Largest K digit number divisible by X
Last Updated :
25 Nov, 2023
Integers X and K are given. The task is to find the highest K-digit number divisible by X.
Examples:
Input : X = 30, K = 3
Output : 990
990 is the largest three digit
number divisible by 30.
Input : X = 7, K = 2
Output : 98
A simple solution is to try all numbers starting from the largest K digit number (which is 999…K-times) and return the first number divisible by X.
An efficient solution is to use below formula.
ans = MAX - (MAX % X)
where MAX is the largest K digit
number which is 999...K-times
The formula works on simple school method division. We remove remainder to get the largest divisible number.
C++
#include <bits/stdc++.h>
using namespace std;
int answer( int X, int K)
{
int MAX = pow (10, K) - 1;
return (MAX - (MAX % X));
}
int main()
{
int X = 30;
int K = 3;
cout << answer(X, K);
}
|
Java
import java.io.*;
import java.lang.*;
class GFG {
public static double answer( double X, double K)
{
double i = 10 ;
double MAX = Math.pow(i, K) - 1 ;
return (MAX - (MAX % X));
}
public static void main(String[] args)
{
double X = 30 ;
double K = 3 ;
System.out.println(( int )answer(X, K));
}
}
|
Python3
def answer(X, K):
MAX = pow ( 10 , K) - 1
return ( MAX - ( MAX % X))
X = 30 ;
K = 3 ;
print (answer(X, K));
|
C#
using System;
class GFG {
public static double answer( double X, double K)
{
double i = 10;
double MAX = Math.Pow(i, K) - 1;
return (MAX - (MAX % X));
}
public static void Main()
{
double X = 30;
double K = 3;
Console.WriteLine(( int )answer(X, K));
}
}
|
Javascript
<script>
function answer(X, K)
{
let MAX = Math.pow(10, K) - 1;
return (MAX - (MAX % X));
}
let X = 30;
let K = 3;
document.write(answer(X, K));
</script>
|
PHP
<?php
function answer( $X , $K )
{
$MAX = pow(10, $K ) - 1;
return ( $MAX - ( $MAX % $X ));
}
$X = 30;
$K = 3;
echo answer( $X , $K );
?>
|
Time Complexity: log(k), due to the inbuilt-library pow()
Auxiliary Space: O(1), As constant extra space is used.
This article is contributed by Rohit Thapliyal.
Approach: Mathematical Calculation
In this approach, we use a mathematical calculation to find the largest K-digit number divisible by X. The main steps of this approach are as follows:
- We start by initializing highest_digit as a K-digit number with all digits set to 9. This is achieved by creating a string of length K with all characters as ‘9’ and converting it to an integer.
- We then iterate in a loop from highest_digit downwards to X. We check each number in the loop if it is divisible by X using the modulo operator %. If the number is divisible by X (i.e., the remainder is zero), we return that number as it is the largest K-digit number divisible by X.
- If the loop finishes without finding a divisible number, it means no such number exists, so we return -1.
Implementation :
C++
#include <iostream>
#include <string>
int largestDivisibleNumber( int X, int K) {
int highestDigit = std::stoi(std::string(K, '9' ));
while (highestDigit >= X) {
if (highestDigit % X == 0) {
return highestDigit;
}
highestDigit--;
}
return -1;
}
int main() {
int X = 30;
int K = 3;
int result = largestDivisibleNumber(X, K);
std::cout << result << std::endl;
return 0;
}
|
Java
import java.io.*;
public class LargestDivisibleNumber {
public static int largest_divisible_number( int X, int K) {
int highestDigit = Integer.parseInt( "9" .repeat(K));
while (highestDigit >= X) {
if (highestDigit % X == 0 ) {
return highestDigit;
}
highestDigit--;
}
return - 1 ;
}
public static void main(String[] args) {
int X = 30 ;
int K = 3 ;
int result = largest_divisible_number(X, K);
System.out.println(result);
}
}
|
Python3
def largest_divisible_number(X, K):
highest_digit = int ( '9' * K)
while highest_digit > = X:
if highest_digit % X = = 0 :
return highest_digit
highest_digit - = 1
return - 1
X = 30
K = 3
result = largest_divisible_number(X, K)
print (result)
|
C#
using System;
class Program
{
static int LargestDivisibleNumber( int X, int K)
{
int highestDigit = int .Parse( new string ( '9' , K));
while (highestDigit >= X)
{
if (highestDigit % X == 0)
{
return highestDigit;
}
highestDigit--;
}
return -1;
}
static void Main()
{
int X = 30;
int K = 3;
int result = LargestDivisibleNumber(X, K);
Console.WriteLine(result);
}
}
|
Javascript
function largestDivisibleNumber(X, K) {
let highestDigit = parseInt( '9' .repeat(K));
while (highestDigit >= X) {
if (highestDigit % X === 0) {
return highestDigit;
}
highestDigit--;
}
return -1;
}
let X = 30;
let K = 3;
let result = largestDivisibleNumber(X, K);
console.log(result);
|
Time Complexity: O(K), where K is the number of digits
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...