Find the largest number | Practice | GeeksforGeeks

Given an integer N the task is to find the largest number which is smaller or equal to it and has its digits in non-decreasing order.
 
Examples 1:
Input:
N = 200
Output:
199
Explanation:
If the given number 
is 200, the largest 
number which is smaller 
or equal to it having 
digits in non decreasing 
order is 199.

Example 2:
Input: 
N = 139
Output:
139
Explanation:
139 is itself in 
non-decresing order.

Your Task:
You don't need to read input or print anything. Your task is to complete the function find() which takes one integer value N, as input parameter and return the largest number which is smaller or equal to it and has its digits in non-decreasing order.

Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
1<=N<=105
0
gupta2411sumit1 month ago
bool isDcreasing( int n )   {       string s = to_string(n) ;              for( int i = 1 ; i<s.length() ; i++)       {           if( s[i] < s[i-1] )           {               return false ;           }       }              return true ;          }   int find(int N){       // code here              while(N)       {           if(isDcreasing(N))           {               return N ;           }                      N -= 1 ;       }              return N ;   }
0
sgupta95192 months ago
Java soln using a bit recursion
 
class Solution{   static int find(int N){       // code here       StringBuilder sb=new StringBuilder(String.valueOf(N));       StringBuilder s=new StringBuilder();       int i=0;       for(i=0;i<sb.length()-1;i++)           if(sb.charAt(i)>sb.charAt(i+1))            {                  s.append(sb.charAt(i)-'1');               break;            }           else           {               s.append(sb.charAt(i));           }                  if(i==sb.length()-1)      { s.append(sb.charAt(i));       return Integer.parseInt(s.toString());}       for(int j=i+1;j<sb.length();j++)       s.append('9');              i=0;       while(i<s.length()-1)       if(s.charAt(i)<=s.charAt(i+1))       i++;       else       return find(Integer.parseInt(s.toString()));       return Integer.parseInt(s.toString());   }}
0
neeramrutia3 months ago
class Solution{   static int find(int N){       // code here        if(N/10==0)       {           return N;       }       if(N/100==0)       {           while(N>0)           {               int k=N;               int a=k%10;               k=k/10;               int b=k%10;               if(a>=b)               {                   return N;               }               N--;           }       }        if(N/1000==0)       {           while(N>0)           {               int k=N;               int a=k%10;               k=k/10;               int b=k%10;               k=k/10;               int c=k%10;               if(a>=b && b>=c)               {                   return N;               }               N--;           }       }        if(N/10000==0)       {           while(N>0)           {               int k=N;               int a=k%10;               k=k/10;               int b=k%10;               k=k/10;               int c=k%10;               k=k/10;               int d=k%10;               if(a>=b && b>=c && c>=d)               {                   return N;               }               N--;           }       }        if(N/100000==0)       {           while(N>0)           {               int k=N;               int a=k%10;               k=k/10;               int b=k%10;               k=k/10;               int c=k%10;               k=k/10;               int d=k%10;               k=k/10;               int e=k%10;               if(a>=b && b>=c && c>=d && d>=e)               {                   return N;               }               N--;           }       }       if(N==100000)       return N;       return 0;   }}
0
manishkumar04 months ago
public static boolean isValid(String s)
    {
        for(int i=1;i<s.length();i++)
        {
            if(s.charAt(i)<s.charAt(i-1))
                return false;
        }
        return true;
    }
    
    static int find(int N){
        for(int i=N;i>0;i--)
        {
            if(isValid(String.valueOf(i)))
               return i;
        }
        return 1;
+1
avinav26114 months ago
Easiest C++ Solution
0
vishakhagupta44 months ago
    def find (self, N):           for y in range(N, -1, -1):      if len(str(y))==1 :            return N      else:          numStr= list(str(y))          numStr.sort()          digit="".join(numStr)                    if int(digit) == y:              return y
0
gourmehul824 months ago
class Solution{       public static boolean check_order(int y)        {            Stack<Integer>stack = new Stack<>() ;             int check = Integer. MIN_VALUE  ;             while(y>0) {               int temp = y%10 ;               stack.add(temp) ;               y/=10 ;           }            while(!stack.isEmpty()) {               int s = stack.pop() ;                 if(s>=check) {                     check= s ;                 }                  else                  return false ;           }            return  true ;       }      static int find(int N){         for(int x=N;x>0;--x) {           if( check_order(x))              return  x ;       }        return N ;          }}
0
mayank20214 months ago
C++  : with recursionint find(int N){
    int digit = N%10, N2=N ;    while(N>0)    {        if (digit < N%10)        {         N2=N2-1;          return find(N2);        }        else         {         digit =N%10 ;         N=N/10;        }     }      return N2;
   } 
0
luizhsb5 months ago
    static int find(int N){
       while (N > 0) {
           int lastDigit = -1;
           
           int num = N;
           boolean isDigitsAscending = true;
           while (num > 0 && isDigitsAscending) {
               int digit = num % 10;
               num = num / 10;
               
               if (lastDigit != -1 && lastDigit < digit) {
                   isDigitsAscending = false;
                   break;
               }
               lastDigit = digit;
           }
           
           if (isDigitsAscending) {
               break;
           } else {
               --N;
           }
           
       }
       
       return N;
   }
 
 
0
krushna65 months ago
EASY WAY !!! EASY WAY!!
BY CONVERTING INTO STRING
int chk(int n)   {   string s = to_string(n);   for (int i = 0; i < s.length()-1; i++)       if (s[i]>s[i+1])return 0;      return 1;   }
   int find(int n)   {   int temp = n;   while (n--)   {       if (chk(temp))           return temp;       temp--;   }      } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.