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++
// CPP code to find highest K-digit number divisible by X
#include <bits/stdc++.h>
using namespace std;
// Function to compute the result
int answer(int X, int K)
{
// Computing MAX
int MAX = pow(10, K) - 1;
// returning ans
return (MAX - (MAX % X));
}
// Driver
int main()
{
// Number whose divisible is to be found
int X = 30;
// Max K-digit divisible is to be found
int K = 3;
cout << answer(X, K);
}
Java
// Java code to find highest
// K-digit number divisible by X
import java.io.*;
import java.lang.*;
class GFG {
public static double answer(double X, double K)
{
double i = 10;
// Computing MAX
double MAX = Math.pow(i, K) - 1;
// returning ans
return (MAX - (MAX % X));
}
public static void main(String[] args)
{
// Number whose divisible is to be found
double X = 30;
// Max K-digit divisible is to be found
double K = 3;
System.out.println((int)answer(X, K));
}
}
// Code contributed by Mohit Gupta_OMG <(0_o)>
Python3
# Python code to find highest
# K-digit number divisible by X
def answer(X, K):
# Computing MAX
MAX = pow(10, K) - 1
# returning ans
return (MAX - (MAX % X))
X = 30;
K = 3;
print(answer(X, K));
# Code contributed by Mohit Gupta_OMG <(0_o)>
C#
// C# code to find highest
// K-digit number divisible by X
using System;
class GFG {
public static double answer(double X, double K)
{
double i = 10;
// Computing MAX
double MAX = Math.Pow(i, K) - 1;
// returning ans
return (MAX - (MAX % X));
}
public static void Main()
{
// Number whose divisible is to be found
double X = 30;
// Max K-digit divisible is to be found
double K = 3;
Console.WriteLine((int)answer(X, K));
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// Javascript code to find highest K-digit
// number divisible by X
// Function to compute the result
function answer(X, K)
{
// Computing MAX
let MAX = Math.pow(10, K) - 1;
// returning ans
return (MAX - (MAX % X));
}
// Driver
// Number whose divisible
// is to be found
let X = 30;
// Max K-digit divisible
// is to be found
let K = 3;
document.write(answer(X, K));
// This code is contributed by gfgking
</script>
PHP
<?php
// PHP code to find highest K-digit
// number divisible by X
// Function to compute the result
function answer($X, $K)
{
// Computing MAX
$MAX = pow(10, $K) - 1;
// returning ans
return ($MAX - ($MAX % $X));
}
// Driver
// Number whose divisible
// is to be found
$X = 30;
// Max K-digit divisible
// is to be found
$K = 3;
echo answer($X, $K);
// This code is contributed by ajit
?>
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')); // Create a K-digit number with
// all digits as 9
// Find the largest K-digit number divisible by X
while (highestDigit >= X) {
if (highestDigit % X == 0) {
return highestDigit;
}
highestDigit--;
}
return -1; // Return -1 if no such number exists
}
int main() {
int X = 30;
int K = 3;
int result = largestDivisibleNumber(X, K);
std::cout << result << std::endl; // Output: 990
return 0;
}
Java
// Java code
import java.io.*;
public class LargestDivisibleNumber {
public static int largest_divisible_number(int X, int K) {
int highestDigit = Integer.parseInt("9".repeat(K)); // Create a K-digit number with all digits as 9
// Find the largest K-digit number divisible by X
while (highestDigit >= X) {
if (highestDigit % X == 0) {
return highestDigit;
}
highestDigit--;
}
return -1; // Return -1 if no such number exists
}
public static void main(String[] args) {
int X = 30;
int K = 3;
int result = largest_divisible_number(X, K);
System.out.println(result); // Output: 990
}
}
// This code is contributed by guptapratik
Python3
def largest_divisible_number(X, K):
highest_digit = int('9' * K) # Create a K-digit number with all digits as 9
# Find the largest K-digit number divisible by X
while highest_digit >= X:
if highest_digit % X == 0:
return highest_digit
highest_digit -= 1
return -1 # Return -1 if no such number exists
# Example usage
X = 30
K = 3
result = largest_divisible_number(X, K)
print(result) # Output: 990
C#
using System;
class Program
{
// Function to find the largest K-digit number divisible by X
static int LargestDivisibleNumber(int X, int K)
{
// Create a K-digit number with all digits as 9
int highestDigit = int.Parse(new string('9', K));
// Find the largest K-digit number divisible by X
while (highestDigit >= X)
{
if (highestDigit % X == 0)
{
return highestDigit;
}
highestDigit--;
}
return -1; // Return -1 if no such number exists
}
static void Main()
{
int X = 30;
int K = 3;
int result = LargestDivisibleNumber(X, K);
Console.WriteLine(result); // Output: 990
}
}
JavaScript
function largestDivisibleNumber(X, K) {
let highestDigit = parseInt('9'.repeat(K)); // Create a K-digit number with all digits as 9
// Find the largest K-digit number divisible by X
while (highestDigit >= X) {
if (highestDigit % X === 0) {
return highestDigit;
}
highestDigit--;
}
return -1; // Return -1 if no such number exists
}
// Example usage
let X = 30;
let K = 3;
let result = largestDivisibleNumber(X, K);
console.log(result); // Output: 990
Time Complexity: O(K), where K is the number of digits
Auxiliary Space: O(1).
Similar Reads
Kth largest N digit number divisible by M Given three positive integers N, K, and M. The task is to find Kth largest N digit number divisible by M. Note: K will be such an integer that Kth largest N digit number divisible by M always exists. Examples Input: N = 2, K = 2, M = 2Output: 96Explanation: The 2nd largest 2 digit number divisible b
5 min read
Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples : Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10Recommended PracticeSmallest K digit number divisible by XTry It! A
4 min read
Largest N digit number divisible by given three numbers Given four integers x, y, z, and n, the task is to find the largest n digit number which is divisible by x, y, and z. Examples: Input: x = 2, y = 3, z = 5, n = 4 Output: 9990 9990 is the largest 4-digit number which is divisible by 2, 3 and 5.Input: x = 3, y = 23, z = 6, n = 2 Output: Not possible A
9 min read
Smallest N digit number divisible by N Given a positive integers N, the task is to find the smallest N digit number divisible by N. Examples: Input: N = 2 Output: 10 Explanation: 10 is the smallest 2-digit number which is divisible by 2. Input: N = 3 Output: 102 Explanation: 102 is the smallest 3-digit number which is divisible by 3. Nai
6 min read
Divisible by 37 for large numbers Given a large number n, we need to check whether it is divisible by 37. Print true if it is divisible by 37 otherwise False.Examples: Input : 74 Output : True Input : 73 Output : False Input : 8955795758 (10 digit number) Output : True A r digit number m whose digital form is (ar-1 ar-2â¦.a2 a1 a0) i
11 min read
Largest K digit number divisible by all numbers in given array Given an array arr[] of size N and an integer K. The task is to find the largest K digit number divisible by all number of arr[]. Examples: Input: arr[] = {2, 3, 5}, K = 3Output: 990Explanation: 990 is the largest 3 digit number divisible by 2, 3 and 5. Input: arr[] = {91, 93, 95}, K = 3Output: -1Ex
6 min read