Print all numbers from a given range that are made up of consecutive digits | C ++ program for the above approach ; Function to find the consecutive digit numbers in the given range ; Initialize num as empty string ; Stores the resultant number ; Iterate over the range [ 1 , 9 ] ; Check if the current number is within range ; Iterate on the digits starting from i ; Checking the consecutive digits numbers starting from i and ending at j is within range or not ; Sort the numbers in the increasing order ; Driver Code ; Print the required numbers
#include <bits/stdc++.h> NEW_LINE 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 ; }