Print all numbers from a given range that are made up of consecutive digits
Last Updated :
05 Jan, 2022
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++
#include <bits/stdc++.h>
using namespace std;
vector< int > solve( int start, int end)
{
string num = "" ;
vector< int > ans;
for ( int i = 1; i <= 9; i++) {
num = to_string(i);
int value = stoi(num);
if (value >= start
and value <= end) {
ans.push_back(value);
}
for ( int j = i + 1; j <= 9; j++) {
num += to_string(j);
value = stoi(num);
if (value >= start
and value <= end) {
ans.push_back(value);
}
}
}
sort(ans.begin(), ans.end());
return ans;
}
int main()
{
int L = 12, R = 87;
vector< int > ans = solve(12, 87);
for ( auto & it : ans)
cout << it << ' ' ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> solve( int start, int end)
{
String num = "" ;
Vector<Integer> ans = new Vector<>();
for ( int i = 1 ; i <= 9 ; i++)
{
num = Integer.toString(i);
int value = i;
if (value >= start &&
value <= end)
{
ans.add(value);
}
for ( int j = i + 1 ; j <= 9 ; j++)
{
num += Integer.toString(j);
value = Integer.valueOf(num);
if (value >= start &&
value <= end)
{
ans.add(value);
}
}
}
Collections.sort(ans);;
return ans;
}
public static void main(String[] args)
{
int L = 12 , R = 87 ;
Vector<Integer> ans = solve(L,R);
for ( int it : ans)
System.out.print(it + " " );
}
}
|
Python3
def solve(start, end):
num = ""
ans = []
for i in range ( 1 , 10 , 1 ):
num = str (i)
value = int (num)
if (value > = start and value < = end):
ans.append(value)
for j in range (i + 1 , 10 , 1 ):
num + = str (j)
value = int (num)
if (value > = start and value < = end):
ans.append(value)
ans.sort()
return ans
if __name__ = = '__main__' :
L = 12
R = 87
ans = solve( 12 , 87 )
for it in ans:
print (it,end = " " )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void solve( int start, int end)
{
string num = "" ;
List< int > ans = new List< int >();
for ( int i = 1; i <= 9; i++)
{
num = i.ToString();
int value = i;
if (value >= start && value <= end)
{
ans.Add(value);
}
for ( int j = i + 1; j <= 9; j++)
{
num += j.ToString();
value = Int32.Parse(num);
if (value >= start &&
value <= end)
{
ans.Add(value);
}
}
}
ans.Sort();
foreach ( int it in ans)
Console.Write(it + " " );
}
static void Main() {
int L = 12, R = 87;
solve(L,R);
}
}
|
Javascript
<script>
function solve(start , end) {
var num = "" ;
var ans = [];
for ( var i = 1; i <= 9; i++) {
num = i.toString();
var value = i;
if (value >= start && value <= end) {
ans.push(value);
}
for (j = i + 1; j <= 9; j++) {
num += j.toString();
value = num;
if (value >= start && value <= end) {
ans.push(value);
}
}
}
return ans;
}
var L = 12, R = 87;
var ans = solve(L, R);
for (it of ans)
document.write(it + " " );
</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
Please Login to comment...