Skip to content
Related Articles

Related Articles

Improve Article
Print all numbers in given range having digits in strictly increasing order
  • Difficulty Level : Hard
  • Last Updated : 23 May, 2021

Given two positive integers L and R, the task is to print the numbers in the range [L, R] which have their digits in strictly increasing order.

Examples:

Input: L = 10, R = 15 
Output: 12 13 14 15 
Explanation: 
In the range [10, 15], only the numbers {12, 13, 14, 15} have their digits in strictly increasing order.

Input: L = 60, R = 70 
Output: 67 68 69 
Explanation: 
In the range [60, 70], only the numbers {67, 68, 69} have their digits in strictly increasing order.

Approach: The idea is to iterate over the range [L, R] and for each number in this range check if digits of this number are in strictly increasing order or not. If yes then print that number else check for the next number.



Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
void printNum(int L, int R)
{
    // Iterate over the range
    for (int i = L; i <= R; i++) {
 
        int temp = i;
        int c = 10;
        int flag = 0;
 
        // Iterate over the digits
        while (temp > 0) {
 
            // Check if the current digit
            // is >= the previous digit
            if (temp % 10 >= c) {
 
                flag = 1;
                break;
            }
 
            c = temp % 10;
            temp /= 10;
        }
 
        // If the digits are in
        // ascending order
        if (flag == 0)
            cout << i << " ";
    }
}
 
// Driver Code
int main()
{
// Given range L and R
    int L = 10, R = 15;
 
// Function Call
    printNum(L, R);
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
static void printNum(int L, int R)
{
     
    // Iterate over the range
    for(int i = L; i <= R; i++)
    {
        int temp = i;
        int c = 10;
        int flag = 0;
 
        // Iterate over the digits
        while (temp > 0)
        {
             
            // Check if the current digit
            // is >= the previous digit
            if (temp % 10 >= c)
            {
                flag = 1;
                break;
            }
             
            c = temp % 10;
            temp /= 10;
        }
         
        // If the digits are in
        // ascending order
        if (flag == 0)
            System.out.print(i + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given range L and R
    int L = 10, R = 15;
     
    // Function call
    printNum(L, R);
}
}
 
// This code is contributed by offbeat

Python3




# Python3 program for the above approach
 
# Function to print all numbers in
# the range [L, R] having digits
# in strictly increasing order
def printNum(L, R):
     
    # Iterate over the range
    for i in range(L, R + 1):
        temp = i
        c = 10
        flag = 0
 
        # Iterate over the digits
        while (temp > 0):
 
            # Check if the current digit
            # is >= the previous digit
            if (temp % 10 >= c):
                flag = 1
                break
             
            c = temp % 10
            temp //= 10
         
        # If the digits are in
        # ascending order
        if (flag == 0):
            print(i, end = " ")
     
# Driver Code
 
# Given range L and R
L = 10
R = 15
 
# Function call
printNum(L, R)
 
# This code is contributed by code_hunt

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
static void printNum(int L, int R)
{
     
    // Iterate over the range
    for(int i = L; i <= R; i++)
    {
        int temp = i;
        int c = 10;
        int flag = 0;
 
        // Iterate over the digits
        while (temp > 0)
        {
             
            // Check if the current digit
            // is >= the previous digit
            if (temp % 10 >= c)
            {
                flag = 1;
                break;
            }
 
            c = temp % 10;
            temp /= 10;
        }
 
        // If the digits are in
        // ascending order
        if (flag == 0)
            Console.Write(i + " ");
    }
}
 
// Driver Code
public static void Main()
{
     
    // Given range L and R
    int L = 10, R = 15;
 
    // Function call
    printNum(L, R);
}
}
 
// This code is contributed by jrishabh99

Javascript




<script>
 
// Javascript program for the above approach
 
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
function printNum(L, R)
{
     
    // Iterate over the range
    for(let i = L; i <= R; i++)
    {
        let temp = i;
        let c = 10;
        let flag = 0;
 
        // Iterate over the digits
        while (temp > 0)
        {
             
            // Check if the current digit
            // is >= the previous digit
            if (temp % 10 >= c)
            {
                flag = 1;
                break;
            }
            c = temp % 10;
            temp /= 10;
        }
         
        // If the digits are in
        // ascending order
        if (flag == 0)
            document.write(i + " ");
    }
}
 
// Driver code
 
// Given range L and R
let L = 10, R = 15;
 
// Function call
printNum(L, R);
 
// This code is contributed by sravan kumar
 
</script>
Output: 
12 13 14 15

Time Complexity O(N), N is absolute difference between L and R. 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :