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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Tidy Number (Digits in non-decreasing Order)
- Smaller palindromic number closest to N
- Find the number closest to n and divisible by m
- 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 the greater number closest to N having at most one non-zero digit
- Find a triplet in an array whose sum is closest to a given number
- Previous perfect square and cube number smaller than number N
- Minimum absolute difference of a number and its closest prime
- Find largest number smaller than N with same set of digits
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find the multiple of x which is closest to a^b
- Queries to find the future closest date
- Find closest integer with the same weight
- Find the K closest points to origin using Priority Queue
- Find K Closest Points to the Origin
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Minimum number closest to N made up of odd digits only
- Largest number smaller than or equal to n and digits in non-decreasing order
- Closest perfect square and its distance
Improved By : Mithun Kumar