Tidy Number are those number whose digits are arranged in non-decreasing order. Here, we are given a number and we have to find another number that is smaller but closest to the given number and also that number should be tidy, i.e., their digit should be in non-decreasing order.
Input : 91234 Output : 89999 Tidy property is violated by appearing 1 after 9. So, we will reduce 9 by 1 and the number right to it will be replaced by 9. So, generated tidy number is 89999. Input : 45000 Output : 44999
The idea is to traverse from end. Whenever tidy property is violated, we reduce digit by one and make all subsequent digits 9.
This question is asked in Google Code Jam 2017 qualification round.
This article is contributed by Aditya kumar. 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.
- Find the number closest to n and divisible by m
- Tidy Number (Digits in non-decreasing Order)
- Find number from given list for which value of the function is closest to A
- Find a triplet in an array whose sum is closest to a given number
- Find largest number smaller than N with same set of digits
- Find the multiple of x which is closest to a^b
- Find closest integer with the same weight
- Find K Closest Points to the Origin
- Find multiple of x closest to or a ^ b (a raised to power b)
- Minimum absolute difference of a number and its closest prime
- Largest number smaller than or equal to N divisible by K
- Largest smaller number possible using only one swap operation
- Maximize a number considering permutations with values smaller than limit
- Largest number smaller than or equal to n and digits in non-decreasing order
- Multiple of x closest to n
Improved By : Mithun Kumar