Check if a number is formed by Concatenation of 1, 14 or 144 only - GeeksforGeeks

08 Nov, 2021
Given a number . The task is to check if the number is formed by concatenating the numbers 1, 14 and 144 only any number of times and in any order.If it is possible, print YES otherwise print NO.Example:  
Input: N = 141411
Output: YES 

Input: N = 14134
Output: NO
 
The idea is to fetch single digit, double digit and triple digit numbers from the end and check if any of them matches with 1, 14 and 144 respectively. If any of them matches, divide the number with that and repeat the above step until the number is greater than zero.Below is the implementation of using above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// 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 onlystring checkNumber(int N){    int temp = N;     while (temp > 0) {        // check for each possible digit        // if given number consist other then        // 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 Codeint main(){    int N = 1414;     cout << checkNumber(N);     return 0;}
// 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 onlystatic String checkNumber(int N){    int temp = N;     while (temp > 0) {        // check for each possible digit        // if given number consist other then        // 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 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 onlydef checkNumber(N):    temp = N     while (temp > 0):                 # check for each possible digit        # if given number consist other then        # 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 CodeN = 1414; print(checkNumber(N)); # This code is contributed# by Akanksha Rai
// 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 onlystatic String checkNumber(int N){    int temp = N;     while (temp > 0) {        // check for each possible digit        // if given number consist other then        // 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 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 onlyfunction checkNumber($N){    $temp = $N;     while ($temp > 0)    {        // check for each possible digit        // if given number consist other then        // 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?>
<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 then            // 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>
YES
 
Time Complexity: O(N/10)
Auxiliary Space: O(1)
vt_m
jit_t
Akanksha_Rai
divyesh072019
rohan07
Constructive Algorithms
number-digits
Competitive Programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Breadth First Traversal ( BFS ) on a 2D array
Runtime Errors
Most important type of Algorithms
Multistage Graph (Shortest Path)
Shortest path in a directed graph by Dijkstra’s algorithm
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7