Dado un número N. La tarea es verificar si los dígitos del número siguen alguno de los siguientes órdenes:
- Los dígitos están en orden estrictamente creciente.
- O bien, los dígitos están en orden estrictamente decreciente.
- O bien, los dígitos siguen un orden estrictamente creciente primero y luego estrictamente decreciente.
Si el número sigue cualquiera de los órdenes anteriores, escriba SÍ; de lo contrario, escriba NO.
Ejemplos :
Input : N = 415 Output : NO Input : N = 123454321 Output : YES
Recorra el número de derecha a izquierda extrayendo cada dígito uno por uno. Mantenga un puntero para indicar si la secuencia actual es una secuencia descendente o ascendente, -1 denota una secuencia estrictamente ascendente y 1 una secuencia estrictamente descendente. Al principio, la secuencia debe ser estrictamente creciente a medida que avanzamos de derecha a izquierda. Cuando encontramos un dígito que es menor que el dígito anterior, cambiamos la bandera a decreciente (es decir, -1) y mientras en orden creciente obtenemos cualquier dígito que es igual al dígito anterior, imprimimos directamente NO.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to check if the digits are in // the given order #include<bits/stdc++.h> using namespace std; // Check if the digits follow the correct order bool isCorrectOrder(int n) { bool flag = true; // to store the previous digit int prev = -1; // pointer to tell what type of sequence // are we dealing with int type = -1; while(n != 0) { if(type ==-1) { if(prev ==-1) { prev = n % 10; n = n/10; continue; } // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // checking the peak point of the number if(prev > n % 10) { type = 1; prev = n % 10; n = n/10; continue; } prev = n % 10; n = n / 10; } else { // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // check if the digit is greater than // the previous one // If true, then break from the loop as // we are in descending order part if(prev < n % 10) { flag = false; break; } prev = n % 10; n = n / 10; } } return flag; } // Driver code int main() { int n = 123454321; if(isCorrectOrder(n)) cout<<"YES"; else cout<<"NO"; return 0; }
Java
// Java program to check if the digits are in // the given order import java.io.*; class GFG { // Check if the digits follow the correct order static boolean isCorrectOrder(int n) { boolean flag = true; // to store the previous digit int prev = -1; // pointer to tell what type of sequence // are we dealing with int type = -1; while(n != 0) { if(type ==-1) { if(prev ==-1) { prev = n % 10; n = n/10; continue; } // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // checking the peak point of the number if(prev > n % 10) { type = 1; prev = n % 10; n = n/10; continue; } prev = n % 10; n = n / 10; } else { // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // check if the digit is greater than // the previous one // If true, then break from the loop as // we are in descending order part if(prev < n % 10) { flag = false; break; } prev = n % 10; n = n / 10; } } return flag; } // Driver code public static void main (String[] args) { int n = 123454321; if(isCorrectOrder(n)) System.out.println("YES"); else System.out.println("NO"); } } // This code is contributed by ajit
Python3
# Python3 program to check if the # digits are in the given order # Check if the digits follow # the correct order def isCorrectOrder(n): flag = True; # to store the previous digit prev = -1; # pointer to tell what type of # sequence are we dealing with type = -1; while(n != 0): if(type ==-1): if(prev ==-1): prev = n % 10; n = int(n / 10); continue; # check if we have same digit # as the previous digit if(prev == n % 10): flag = False; break; # checking the peak point # of the number if(prev > n % 10): type = 1; prev = n % 10; n = int(n / 10); continue; prev = n % 10; n = int(n / 10); else: # check if we have same digit # as the previous digit if(prev == n % 10): flag = False; break; # check if the digit is greater # than the previous one # If true, then break from the # loop as we are in descending # order part if(prev < n % 10): flag = False; break; prev = n % 10; n = int(n / 10); return flag; # Driver code n = 123454321; if(isCorrectOrder(n)): print("YES"); else: print("NO"); # This Code is contributed by mits
C#
// C# program to check if the // digits are in the given order using System; class GFG { // Check if the digits follow // the correct order static bool isCorrectOrder(int n) { bool flag = true; // to store the previous digit int prev = -1; // pointer to tell what type of // sequence are we dealing with int type = -1; while(n != 0) { if(type == -1) { if(prev == -1) { prev = n % 10; n = n / 10; continue; } // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // checking the peak point // of the number if(prev > n % 10) { type = 1; prev = n % 10; n = n / 10; continue; } prev = n % 10; n = n / 10; } else { // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // check if the digit is greater // than the previous one // If true, then break from the // loop as we are in descending // order part if(prev < n % 10) { flag = false; break; } prev = n % 10; n = n / 10; } } return flag; } // Driver code public static void Main () { int n = 123454321; if(isCorrectOrder(n)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed by Shashank
PHP
<?php // PHP program to check if the // digits are in the given order // Check if the digits follow // the correct order function isCorrectOrder($n) { $flag = true; // to store the previous digit $prev = -1; // pointer to tell what type of // sequence are we dealing with $type = -1; while($n != 0) { if($type ==-1) { if($prev ==-1) { $prev = $n % 10; $n = (int)$n / 10; continue; } // check if we have same digit // as the previous digit if($prev == $n % 10) { $flag = false; break; } // checking the peak point // of the number if($prev > $n % 10) { $type = 1; $prev = $n % 10; $n = (int)$n / 10; continue; } $prev = $n % 10; $n = (int)$n / 10; } else { // check if we have same digit // as the previous digit if($prev == $n % 10) { $flag = false; break; } // check if the digit is greater // than the previous one // If true, then break from the // loop as we are in descending // order part if($prev < $n % 10) { $flag = false; break; } $prev = $n % 10; $n = (int) $n / 10; } } return $flag; } // Driver code $n = 123454321; if(isCorrectOrder($n)) echo "YES"; else echo "NO"; // This Code is contributed by ajit ?>
Javascript
<script> // JavaScript program to check if the digits are in // the given order // Check if the digits follow the correct order function isCorrectOrder(n) { let flag = true; // to store the previous digit let prev = -1; // pointer to tell what type of sequence // are we dealing with let type = -1; while(n != 0) { if(type ===-1) { if(prev ===-1) { prev = n % 10; n = Math.floor(n/10); continue; } // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // checking the peak point of the number if(prev > n % 10) { type = 1; prev = n % 10; n = Math.floor(n/10); continue; } prev = n % 10; n = Math.floor(n / 10); } else { // check if we have same digit // as the previous digit if(prev == n % 10) { flag = false; break; } // check if the digit is greater than // the previous one // If true, then break from the loop as // we are in descending order part if(prev < n % 10) { flag = false; break; } prev = n % 10; n = Math.floor(n / 10); } } return flag; } // Driver code let n = 123454321; if(isCorrectOrder(n)) document.write("YES"); else document.write("NO"); // This code is contributed by Surbhi Tyagi </script>
YES
Complejidad temporal: O(logN)
Espacio auxiliar: O(1)