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++
#include <bits/stdc++.h>
using namespace std;
void printNum( int L, int R)
{
for ( int i = L; i <= R; i++) {
int temp = i;
int c = 10;
int flag = 0;
while (temp > 0) {
if (temp % 10 >= c) {
flag = 1;
break ;
}
c = temp % 10;
temp /= 10;
}
if (flag == 0)
cout << i << " " ;
}
}
int main()
{
int L = 10, R = 15;
printNum(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printNum( int L, int R)
{
for ( int i = L; i <= R; i++)
{
int temp = i;
int c = 10 ;
int flag = 0 ;
while (temp > 0 )
{
if (temp % 10 >= c)
{
flag = 1 ;
break ;
}
c = temp % 10 ;
temp /= 10 ;
}
if (flag == 0 )
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int L = 10 , R = 15 ;
printNum(L, R);
}
}
|
Python3
def printNum(L, R):
for i in range (L, R + 1 ):
temp = i
c = 10
flag = 0
while (temp > 0 ):
if (temp % 10 > = c):
flag = 1
break
c = temp % 10
temp / / = 10
if (flag = = 0 ):
print (i, end = " " )
L = 10
R = 15
printNum(L, R)
|
C#
using System;
class GFG{
static void printNum( int L, int R)
{
for ( int i = L; i <= R; i++)
{
int temp = i;
int c = 10;
int flag = 0;
while (temp > 0)
{
if (temp % 10 >= c)
{
flag = 1;
break ;
}
c = temp % 10;
temp /= 10;
}
if (flag == 0)
Console.Write(i + " " );
}
}
public static void Main()
{
int L = 10, R = 15;
printNum(L, R);
}
}
|
Javascript
<script>
function printNum(L, R)
{
for (let i = L; i <= R; i++)
{
let temp = i;
let c = 10;
let flag = 0;
while (temp > 0)
{
if (temp % 10 >= c)
{
flag = 1;
break ;
}
c = temp % 10;
temp /= 10;
}
if (flag == 0)
document.write(i + " " );
}
}
let L = 10, R = 15;
printNum(L, R);
</script>
|
Time Complexity O(N), N is absolute difference between L and R.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!