Open In App

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

Last Updated : 23 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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)
 



Similar Reads

Generate k digit numbers with digits in strictly increasing order
Given a value k, generate all well-ordered numbers of length k. Well-ordered means that dig­its should be in increas­ing.Examples : Input : K = 7 Output : 1234567 1234568 1234569 1234578 1234579 1234589 1234678 1234679 1234689 1234789 1235678 1235679 1235689 1235789 1236789 1245678 1245679 1245689 1245789 1246789 1256789 1345678 1345679 1345689 134
6 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Print all n-digit strictly increasing numbers
Given number of digits n in a number, print all n-digit numbers whose digits are strictly increasing from left to right.Examples: Input: n = 2 Output: 01 02 03 04 05 06 07 08 09 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47 48 49 56 57 58 59 67 68 69 78 79 89 Input: n = 3 Output: 012 013 014 015 016 017 018 019 023 024 025
6 min read
Count elements in Array having strictly smaller and strictly greater element present
Given an array arr[], the task is to find the count of elements in the given array such that there exists an element strictly smaller and an element strictly greater than it. Examples: Input: arr [] = {11, 7, 2, 15}Output: 2Explanation: For arr[1] = 7, arr[0] is strictly greater than it and arr[2] is strictly smaller than it. Similarly for arr[1],
8 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
29 min read
Print a number strictly less than a given number such that all its digits are distinct.
Given a positive number n, print a number less than n such that all its digits are distinct.Examples: Input : 1134 Output : 1098 1098 is the largest number smaller than 1134 such that all digits are distinct. Input : 4559 Output : 4539 The problem can easily be solved by using counting. Firstly, loop through numbers less than n and for each number
6 min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
Given a Binary Tree, the task is to check if it consists of node values arranged in strictly increasing order at even levels and strictly decreasing at odd levels (Assuming the root node to be at level 0). Examples: Input: 2 / \ 6 3 / \ \ 4 7 11 / \ \ 10 5 1 Output: YES Explanation: At level 1 (odd), Node values 6 and 3 are in strictly decreasing o
10 min read
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.Examples: Input: L = 1, R = 100, N = 8, M = 2 Output: 4 Only 8, 26, 44 and 62 are valid numbers Input: L = 1, R = 200, N = 4, M = 11 Output: 2 Only 2
14 min read
Count of numbers in range [L, R] having sum of digits of its square equal to square of sum of digits
Given two integers L and R, the task is to find the count of numbers in range [L, R] such that the sum of digits of its square is equal to the square of sum of its digits, Example: Input: L = 22, R = 22Output: 1Explanation: 22 is only valid number in this range as sum of digits of its square = S(22*22) = S(484) = 16 and square of sum of its digits
11 min read