Largest number smaller than or equal to n and digits in non-decreasing order

3.4

Given a number n, find the Largest number smaller than or equal to n and digits in non-decreasing order.

Examples:

Input  : n = 200
Output : 199
If the given number is 200, the largest 
number which is smaller or equal to it 
having digits in non decreasing order is
199.

Input  : n = 139
Output : 139

Method 1 (Brute Force)

Start from n, for every number check if its digits are in non decreasing order. If yes, then return. Else check check for the next number until we find the result.

/* C++ program for brute force approach to find
   largest number having digits in non-decreasing
   order. */
#include<bits/stdc++.h>
using namespace std;

// Returns the required number
long long nondecdigits(long long n)
{
    /* loop to recursively check the numbers less
       than or equal to given number*/
    long long int x = 0;
    for (x=n; x>=1; x--)
    {
        int no = x;
        int prev_dig = 11;

        // Keep traversing digits from
        // right to left. For every digit
        // check if it is smaller than prev_dig
        bool flag = true;
        while (no != 0)
        {
            if (prev_dig < no%10)
            {
               flag = false;
               break;
            }
            prev_dig = no % 10;
            no /= 10;
        }

        // We found the required number
        if (flag == true)
           break;
    }

    return x;
}

// Driver program
int main()
{
    long long n = 200;
    cout << nondecdigits(n);
    return 0;
}

Output:

199

Efficient approach

The method discussed above is not much efficient as would only give results for numbers upto 10^5, but if the number is very big such that it contains 10^5 digits.

So, we will discuss an another method for such big numbers.
Step 1: Store the digits of the number in array or a vector.

Step 2: Start traversing the array from the digit from rightmost position to leftmost in given number.

Step 3: If a digit is greater than the digit in the right to it, note the index of that digit in that array and decrease that digit by one.

Step 4 : Keep updating that index until you completely traverse the array accordingly as discussed in step 3.

Step 4: Set all the digits right to that index as 9 .

Step 5 : Print the array as this is the required number.

Suppose the number is 200 the digits will be 2, 0, 0. The index at which leftmost digit is greater than the right digit is index 1 (following 1-index) so the number at index 1 will be 2 – 1 = 1 and all the digits right to it will be 9. So the final array will be 1, 9, 9. And the required number will be 199.

/* C++ program for efficient approach to find
   largest number having digits in non-decreasing
   order. */
#include<bits/stdc++.h>
using namespace std;

// Prints the largest number smaller than s and
// digits in non-decreasing order.
void nondecdigits(string s)
{
    long long m = s.size();

    /* array to store digits of number */
    long long a[m];

    /* conversion of characters of string int number */
    for (long long i=0; i<m; i++)
        a[i] = s[i] - '0';

    /* variable holds the value of index after which
       all digits are set 9 */
    long long level = m-1;
    for (long long i=m-1; i>0; i--)
    {
        /* Checking the condition if the digit is
           less than its left digit */
        if (a[i] < a[i-1])
        {
            a[i-1]--;
            level=i-1;
        }
    }

    /* If first digit is 0 no need to print it */
    if (a[0] != 0)
    {
        for (long long i=0; i<=level; i++)
            cout << a[i];
        for (long long i=level+1; i<m; i++)
            cout << "9";
    }
    else
    {
        for (long long i=1; i<level; i++)
            cout << a[i];
        for (long long i=level+1; i<m; i++)
            cout << "9";
    }
}

// Driver function
int main()
{
    string n = "200";
    nondecdigits(n);
    return 0;
}

Output:

199

Time Complexity Time complexity is O(d) where d is no. of digits in the number.

This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3.4 Average Difficulty : 3.4/5.0
Based on 7 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.