Dado un número . La tarea es verificar si el número se forma concatenando los números 1, 14 y 144 solo cualquier cantidad de veces y en cualquier orden.
Si es posible, escriba SÍ; de lo contrario, escriba NO.
Ejemplo:
Input: N = 141411 Output: YES Input: N = 14134 Output: NO
La idea es buscar números de un dígito, dos dígitos y tres dígitos desde el final y verificar si alguno de ellos coincide con 1, 14 y 144 respectivamente. Si alguno de ellos coincide, divide el número con eso y repite el paso anterior hasta que el número sea mayor que cero.
A continuación se muestra la implementación del uso del enfoque anterior:
C++
// C++ program to check if a number is formed // by Concatenation of 1, 14 or 144 only #include <iostream> using namespace std; // Function to check if a number is formed // by Concatenation of 1, 14 or 144 only string checkNumber(int N) { int temp = N; while (temp > 0) { // check for each possible digit // if given number consist other than // 1, 14, 144 print NO else print YES if (temp % 1000 == 144) temp /= 1000; else if (temp % 100 == 14) temp /= 100; else if (temp % 10 == 1) temp /= 10; else { return "NO"; } } return "YES"; } // Driver Code int main() { int N = 1414; cout << checkNumber(N); return 0; }
Java
// Java program to check if a number is formed // by Concatenation of 1, 14 or 144 only import java.io.*; class GFG { // Function to check if a number is formed // by Concatenation of 1, 14 or 144 only static String checkNumber(int N) { int temp = N; while (temp > 0) { // check for each possible digit // if given number consist other than // 1, 14, 144 print NO else print YES if (temp % 1000 == 144) temp /= 1000; else if (temp % 100 == 14) temp /= 100; else if (temp % 10 == 1) temp /= 10; else { return "NO"; } } return "YES"; } // Driver Code public static void main (String[] args) { int N = 1414; System.out.println(checkNumber(N)); } } // This code is contributed by anuj_67..
Python 3
# Python 3 program to check if a # number is formed by Concatenation # of 1, 14 or 144 only # Function to check if a number is formed # by Concatenation of 1, 14 or 144 only def checkNumber(N): temp = N while (temp > 0): # check for each possible digit # if given number consist other than # 1, 14, 144 print NO else print YES if (temp % 1000 == 144): temp /= 1000 elif (temp % 100 == 14): temp /= 100 elif (temp % 10 == 1): temp /= 10 else: return "YES" return "NO" # Driver Code N = 1414; print(checkNumber(N)); # This code is contributed # by Akanksha Rai
C#
// C# program to check if a number is formed // by Concatenation of 1, 14 or 144 only using System; class GFG { // Function to check if a number is formed // by Concatenation of 1, 14 or 144 only static String checkNumber(int N) { int temp = N; while (temp > 0) { // check for each possible digit // if given number consist other than // 1, 14, 144 print NO else print YES if (temp % 1000 == 144) temp /= 1000; else if (temp % 100 == 14) temp /= 100; else if (temp % 10 == 1) temp /= 10; else { return "NO"; } } return "YES"; } // Driver Code public static void Main () { int N = 1414; Console.WriteLine(checkNumber(N)); } } // This code is contributed by anuj_67..
PHP
<?php // PHP program to check if a number // is formed by Concatenation of // 1, 14 or 144 only // Function to check if a number is formed // by Concatenation of 1, 14 or 144 only function checkNumber($N) { $temp = $N; while ($temp > 0) { // check for each possible digit // if given number consist other than // 1, 14, 144 print NO else print YES if ($temp % 1000 == 144) $temp /= 1000; else if ($temp % 100 == 14) $temp /= 100; else if ($temp % 10 == 1) $temp /= 10; else { return "YES"; } } return "NO"; } // Driver Code $N = 1414; echo checkNumber($N); // This code is contributed by Tushil ?>
Javascript
<script> // Javascript program to check if a number is formed // by Concatenation of 1, 14 or 144 only // Function to check if a number is formed // by Concatenation of 1, 14 or 144 only function checkNumber(N) { let temp = N; while (temp > 0) { // check for each possible digit // if given number consist other than // 1, 14, 144 print NO else print YES if (temp % 1000 == 144) temp = parseInt(temp / 1000, 10); else if (temp % 100 == 14) temp = parseInt(temp / 100, 10); else if (temp % 10 == 1) temp = parseInt(temp / 10, 10); else { return "NO"; } } return "YES"; } let N = 1414; document.write(checkNumber(N)); </script>
Producción:
YES
Complejidad de tiempo: O (logn)
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.