Integers X and K are given. The task is to find the highest K-digit number divisible by X.
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.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- C++ Program for Largest K digit number divisible by X
- Largest N digit number divisible by given three numbers
- Java Program for Largest K digit number divisible by X
- Largest number less than N with digit sum greater than the digit sum of N
- Smallest K digit number divisible by X
- Sum of n digit numbers divisible by a given number
- Find nth number that contains the digit k or divisible by k.
- C++ Program for Smallest K digit number divisible by X
- Smallest n digit number divisible by given three numbers
- Count n digit numbers divisible by given number
- Largest and smallest digit of a number
- Largest even digit number not greater than N
- Java Program for Smallest K digit number divisible by X
- Print digit's position to be removed to make a number divisible by 6
- Largest number divisible by 90 that can be made using 0 and 5