VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-closest-smaller-tidy-number/

⇱ Find the closest and smaller tidy number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the closest and smaller tidy number

Last Updated : 11 Mar, 2024

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. 

Examples: 

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.  

Output: 

11333444999


 

Comment
Article Tags:
Article Tags: