Open In App

Print all numbers from a given range that are made up of consecutive digits

Last Updated : 05 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a range [L, R], the task is to find all the numbers from the range [L, R] whose digits are consecutive. Print all those numbers in increasing order.

Examples:

Input: L = 12, R = 34
Output: 12 23 34

Input: L = 12, R = 25
Output: 12 23

Approach: The given problem can be solved by generating all possible numbers and store all those numbers that satisfy the given condition. After generating all the numbers print all the stored numbers in sorted order. Follow the steps below to solve the given problem:

  • Initialize a variable, say num as “” that stores the string form of all possible numbers having consecutive digits and in increasing order.
  • Iterate over the range [1, 9] using the variable i and perform the following steps:
    • Update the string num to the string form of i and if this value lies over the range [L, R], then store this in the vector Ans[].
    • Iterate over the range [1, 9] using the variable j add the character form of j to the string num, and if the integer form of the string num lies over the range [L, R], then store this in the vector Ans[].
  • After completing the above steps, sort the vector Ans[] to print all the numbers generated.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the consecutive
// digit numbers in the given range
vector<int> solve(int start, int end)
{
    // Initialize num as empty string
    string num = "";
 
    // Stores the resultant number
    vector<int> ans;
 
    // Iterate over the range [1, 9]
    for (int i = 1; i <= 9; i++) {
 
        num = to_string(i);
        int value = stoi(num);
 
        // Check if the current number
        // is within range
        if (value >= start
            and value <= end) {
            ans.push_back(value);
        }
 
        // Iterate on the digits starting
        // from i
        for (int j = i + 1; j <= 9; j++) {
 
            num += to_string(j);
            value = stoi(num);
 
            // Checking the consecutive
            // digits numbers starting
            // from i and ending at j
            // is within range or not
            if (value >= start
                and value <= end) {
                ans.push_back(value);
            }
        }
    }
 
    // Sort the numbers in the
    // increasing order
    sort(ans.begin(), ans.end());
 
    return ans;
}
 
// Driver Code
int main()
{
    int L = 12, R = 87;
 
    vector<int> ans = solve(12, 87);
 
    // Print the required numbers
    for (auto& it : ans)
        cout << it << ' ';
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the consecutive
// digit numbers in the given range
static Vector<Integer> solve(int start, int end)
{
     
    // Initialize num as empty String
    String num = "";
 
    // Stores the resultant number
    Vector<Integer> ans = new Vector<>();
 
    // Iterate over the range [1, 9]
    for(int i = 1; i <= 9; i++)
    {
        num = Integer.toString(i);
        int value = i;
 
        // Check if the current number
        // is within range
        if (value >= start &&
            value <= end)
        {
            ans.add(value);
        }
 
        // Iterate on the digits starting
        // from i
        for(int j = i + 1; j <= 9; j++)
        {
            num += Integer.toString(j);
            value = Integer.valueOf(num);
 
            // Checking the consecutive
            // digits numbers starting
            // from i and ending at j
            // is within range or not
            if (value >= start &&
                value <= end)
            {
                ans.add(value);
            }
        }
    }
     
    // Sort the numbers in the
    // increasing order
    Collections.sort(ans);;
     
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int L = 12, R = 87;
     
    Vector<Integer> ans = solve(L,R);
     
    // Print the required numbers
    for(int it : ans)
        System.out.print(it + " ");
}
}
 
// This code is contributed by gauravrajput1


Python3




# Python3 program for the above approach
 
# Function to find the consecutive
# digit numbers in the given range
def solve(start, end):
    # Initialize num as empty string
    num = ""
 
    # Stores the resultant number
    ans = []
 
    # Iterate over the range [1, 9]
    for i in range(1,10,1):
        num = str(i)
        value = int(num)
 
        # Check if the current number
        # is within range
        if (value >= start and value <= end):
            ans.append(value)
 
        # Iterate on the digits starting
        # from i
        for j in range(i + 1,10,1):
            num += str(j)
            value = int(num)
 
            # Checking the consecutive
            # digits numbers starting
            # from i and ending at j
            # is within range or not
            if (value >= start and value <= end):
                ans.append(value)
 
    # Sort the numbers in the
    # increasing order
    ans.sort()
 
    return ans
 
# Driver Code
if __name__ == '__main__':
    L = 12
    R = 87
 
    ans = solve(12, 87)
 
    # Print the required numbers
    for it in ans:
        print(it,end = " ")
         
        # This code is contributed by SURENDRA_GANGWAR.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class Program
{
     
// Function to find the consecutive
// digit numbers in the given range
static void solve(int start, int end)
{
     
    // Initialize num as empty String
    string num = "";
 
    // Stores the resultant number
    List<int> ans = new List<int>();
 
    // Iterate over the range [1, 9]
    for(int i = 1; i <= 9; i++)
    {
        num = i.ToString();
        int value = i;
 
        // Check if the current number
        // is within range
        if (value >= start && value <= end)
        {
            ans.Add(value);
        }
 
        // Iterate on the digits starting
        // from i
        for(int j = i + 1; j <= 9; j++)
        {
            num += j.ToString();
            value = Int32.Parse(num);
 
            // Checking the consecutive
            // digits numbers starting
            // from i and ending at j
            // is within range or not
            if (value >= start &&
                value <= end)
            {
                ans.Add(value);
            }
        }
    }
     
    // Sort the numbers in the
    // increasing order
    ans.Sort();
     
    // Print the required numbers
    foreach(int it in ans)
        Console.Write(it + " ");
}
 
  // Driver code
    static void Main() {
        int L = 12, R = 87;
        solve(L,R);
    }
}
 
// This code is contributed by SoumikMondal


Javascript




<script>
// javascript program for the above approach
 
    // Function to find the consecutive
    // digit numbers in the given range
     function solve(start , end) {
 
        // Initialize num as empty String
        var num = "";
 
        // Stores the resultant number
        var ans = [];
 
        // Iterate over the range [1, 9]
        for (var i = 1; i <= 9; i++) {
            num = i.toString();
            var value = i;
 
            // Check if the current number
            // is within range
            if (value >= start && value <= end) {
                ans.push(value);
            }
 
            // Iterate on the digits starting
            // from i
            for (j = i + 1; j <= 9; j++) {
                num += j.toString();
                value = num;
 
                // Checking the consecutive
                // digits numbers starting
                // from i and ending at j
                // is within range or not
                if (value >= start && value <= end) {
                    ans.push(value);
                }
            }
        }
 
        // Sort the numbers in the
        // increasing order
        return ans;
    }
 
    // Driver Code
        var L = 12, R = 87;
        var ans = solve(L, R);
 
        // Print the required numbers
        for (it of ans)
            document.write(it + " ");
 
// This code is contributed by Rajput-Ji
</script>


Output: 

12 23 34 45 56 67 78

 

Time Complexity: O(1)
Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads