Check if a number is formed by Concatenation of 1, 14 or 144 only
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++
// 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 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 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 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
# 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 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 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 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 // 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 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 ?> |
YES
Recommended Posts:
- Check if the large number formed is divisible by 41 or not
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Check if concatenation of two strings is balanced or not
- Check if B can be formed by permuting the binary digits of A
- Check if a right-angled triangle can be formed by moving any one of the coordinates
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Maximum number formed from array with K number of adjacent swaps allowed
- Find maximum number that can be formed using digits of a given number
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Largest even number that can be formed by any number of swaps
- Number of triangles that can be formed
- Number formed by the rightmost set bit in N
- Number of triangles that can be formed with given N points
- Greatest number less than equal to B that can be formed from the digits of A
- Number of triangles formed from a set of points on three lines
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.