Largest number smaller than or equal to n and digits in non | C ++ program for brute force approach to find largest number having digits in non - decreasing order . ; Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long nondecdigits ( long long n ) { long long int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; bool flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } int main ( ) { long long n = 200 ; cout << nondecdigits ( n ) ; return 0 ; }