Open In App

Largest K digit number divisible by X

Last Updated : 25 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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
?>

Output
990

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

Output
990

Time Complexity: O(K), where K is the number of digits

Auxiliary Space: O(1).


Similar Reads