Dados dos enteros positivos L y R , la tarea es imprimir los números en el rango [L, R] que tienen sus dígitos en orden estrictamente creciente.
Ejemplos:
Entrada: L = 10, R = 15
Salida: 12 13 14 15
Explicación:
En el rango [10, 15], solo los números {12, 13, 14, 15} tienen sus dígitos en orden estrictamente creciente.Entrada: L = 60, R = 70
Salida: 67 68 69
Explicación:
En el rango [60, 70], solo los números {67, 68, 69} tienen sus dígitos en orden estrictamente creciente.
Enfoque: la idea es iterar sobre el rango [L, R] y para cada número en este rango verificar si los dígitos de este número están en orden estrictamente creciente o no. En caso afirmativo, imprima ese número; de lo contrario, verifique el siguiente número.
A continuación se muestra la implementación del enfoque anterior:
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>
12 13 14 15
Complejidad temporal O(N), N es la diferencia absoluta entre L y R.
Espacio auxiliar: O(1)