Print all numbers in given range having digits in strictly increasing order

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++
Java
Python3
C#
Javascript
// 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 ordervoid 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 Codeint main(){// Given range L and R    int L = 10, R = 15; // Function Call    printNum(L, R);    return 0;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to print all numbers// in the range [L, R] having digits// in strictly increasing orderstatic 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 codepublic 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 program for the above approach # Function to print all numbers in# the range [L, R] having digits# in strictly increasing orderdef 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 RL = 10R = 15 # Function callprintNum(L, R) # This code is contributed by code_hunt
// C# program for the above approachusing System; class GFG{ // Function to print all numbers// in the range [L, R] having digits// in strictly increasing orderstatic 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 Codepublic static void Main(){         // Given range L and R    int L = 10, R = 15;     // Function call    printNum(L, R);}} // This code is contributed by jrishabh99
<script> // Javascript program for the above approach // Function to print all numbers// in the range [L, R] having digits// in strictly increasing orderfunction 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 Rlet L = 10, R = 15; // Function callprintNum(L, R); // This code is contributed by sravan kumar </script>
12 13 14 15
Time Complexity O(N), N is absolute difference between L and R. Auxiliary Space: O(1) 
offbeat
jrishabh99
nidhi_biet
code_hunt
sravankumar8128
khushboogoyal499
number-digits
Numbers
Greedy
Mathematical
School Programming
Searching
Sorting
Searching
Greedy
Mathematical
Sorting
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Job Sequencing Problem
Minimum Number of Platforms Required for a Railway/Bus Station
Policemen catch thieves
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
C++ Data Types
Merge two sorted arrays
Operators in C / C++