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 email@example.com. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the number closest to n and divisible by m
- Tidy Number (Digits in non-decreasing Order)
- Find the largest number smaller than integer N with maximum number of set bits
- 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 K Closest Points to the Origin
- Find closest integer with the same weight
- Previous perfect square and cube number smaller than number N
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find the K closest points to origin using Priority Queue
- 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
Improved By : Mithun Kumar