A Belphegor number is a palindromic number such that the number is of form
1(0...)666(0...)1
The Nth Belphegor number is given by:
Check if N is a Belphegor number
Given a number N, the task is to check if N is an Belphegor Number or not. If N is an Belphegor Number then print “Yes” else print “No”.
Examples:
Input: N = 16661 Output: Yes Explanation: This number is palindrome and is in the form 1(0…)666(0…)1. Input: N = 1661 Output: No
Approach: The idea is to check if the number is palindrome or not, if it is palindrome then check for number should be of form 1(0…)666(0…)1, if the number is in this form then print “Yes”, else print “No”. Below is the implementation of the above approach:
// C++ implementation // of the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if the number // N is in the form // 1(0...)666(0...)1 bool isinform( int n)
{ string temp = to_string(n);
int Len = temp.length();
// basic case
if (temp.find( "666" ) == string::npos || Len < 5
|| Len % 2 == 0)
return false ;
int mid = (Len / 2);
if (!(temp[mid - 1] == temp[mid] && temp[mid - 1] == '6'
&& temp[0] == '1' ))
return false ;
for ( int i = mid + 2; i < Len - 1; i++)
if (temp[i] != '0' )
return false ;
return true ;
} // Function to check if the number // N is palindrome bool ispalindrome( int n)
{ string temp = to_string(n);
string revtemp = to_string(n);
reverse(revtemp.begin(), revtemp.end());
return (temp == revtemp);
} // Function to check if a number // N is Belphegor bool isBelphegor( int n)
{ return ispalindrome(n) && isinform(n);
} // Driver Code int main()
{ int n = 100666001;
if (isBelphegor(n))
cout << "Yes" ;
else
cout << "No" ;
} // This code is contributed by phasing17 |
// Java implementation // of the above approach import java.util.*;
class GFG {
// Function to reverse a String
public static String Reverse(String s)
{
StringBuilder sb = new StringBuilder();
// append a string into StringBuilder input1
sb.append(s);
// reverse StringBuilder input1
sb.reverse();
return new String(sb);
}
// Function to check if the number
// N is in the form
// 1(0...)666(0...)1
static boolean isinform( int n)
{
String temp = String.valueOf(n);
int Len = temp.length();
// basic case
if ((temp.indexOf( "666" ) >= 0 ) || Len < 5
|| Len % 2 == 0 )
return false ;
int mid = (Len / 2 );
if (!(temp.charAt(mid - 1 ) == temp.charAt(mid)
&& temp.charAt(mid - 1 ) == '6'
&& temp.charAt( 0 ) == '1' ))
return false ;
for ( int i = mid + 2 ; i < Len - 1 ; i++)
if (temp.charAt(i) != '0' )
return false ;
return true ;
}
// Function to check if the number
// N is palindrome
static boolean ispalindrome( int n)
{
String temp = String.valueOf(n);
String revtemp = Reverse(String.valueOf(n));
return (temp.equals(revtemp));
}
// Function to check if a number
// N is Belphegor
static boolean isBelphegor( int n)
{
return ispalindrome(n) && isinform(n);
}
// Driver Code
public static void main(String[] args)
{
int n = 100666001 ;
if (isBelphegor(n))
System.out.println( "Yes" );
else
System.out.println( "Yes" );
}
} // This code is contributed by phasing17 |
# Python3 implementation # of the above approach # Function to check if the number # N is in the form # 1(0...)666(0...)1 def isinform(n):
temp = str (n)
Len = len (temp)
# basic case
if "666" not in temp or \
Len < 5 or Len % 2 = = 0 :
return False
mid = Len / / 2
if not (temp[mid - 1 ] = = temp[mid]\
and temp[mid - 1 ] = = '6' and \
temp[ 0 ] = = '1' ):
return False
for i in range (mid + 2 , Len - 1 ):
if temp[i] ! = '0' :
return False
return True
# Function to check if the number # N is palindrome def ispalindrome(n):
temp = str (n)
if temp = = temp[:: - 1 ]:
return True
return False
# Function to check if a number # N is Belphegor def isBelphegor(n):
if ispalindrome(n):
if isinform(n):
return True
return False
# Driver Code n = 100666001 ;
if isBelphegor(n):
print ( "Yes" )
else :
print ( "No" )
|
// C# implementation // of the above approach using System;
using System.Collections.Generic;
class GFG
{ // Function to reverse a string
public static string Reverse( string s )
{
char [] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
// Function to check if the number
// N is in the form
// 1(0...)666(0...)1
static bool isinform( int n)
{
string temp = Convert.ToString(n);
int Len = temp.Length;
// basic case
if ( (temp.IndexOf( "666" ) >= 0) || Len < 5
|| Len % 2 == 0)
return false ;
int mid = (Len / 2);
if (!(temp[mid - 1] == temp[mid] && temp[mid - 1] == '6'
&& temp[0] == '1' ))
return false ;
for ( int i = mid + 2; i < Len - 1; i++)
if (temp[i] != '0' )
return false ;
return true ;
}
// Function to check if the number
// N is palindrome
static bool ispalindrome( int n)
{
string temp = Convert.ToString(n);
string revtemp = Reverse(Convert.ToString(n));
return (temp.Equals(revtemp));
}
// Function to check if a number
// N is Belphegor
static bool isBelphegor( int n)
{
return ispalindrome(n) && isinform(n);
}
// Driver Code
public static void Main( string [] args)
{
int n = 100666001;
if (isBelphegor(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "Yes" );
}
} // This code is contributed by phasing17 |
// JS implementation // of the above approach // Function to check if the number // N is in the form // 1(0...)666(0...)1 function isinform(n)
{ let temp = (n).toString()
let Len = (temp).length
// basic case
if (!temp.includes( "666" ) ||
Len<5 || Len % 2 == 0)
return false
let mid = Math.floor(Len/2)
if (!(temp[mid-1] == temp[mid]
&& temp[mid-1] == '6' &&
temp[0] == '1' ))
return false
for ( var i = mid + 2; i < Len - 1; i++)
if (temp[i] != '0' )
return false
return true
} // Function to check if the number // N is palindrome function ispalindrome(n)
{ let temp = (n).toString()
return (temp == temp.split( "" ).reverse().join( "" ))
} // Function to check if a number // N is Belphegor function isBelphegor(n)
{ return ispalindrome(n) && isinform(n)
} // Driver Code let n = 100666001; if (isBelphegor(n))
console.log( "Yes" )
else console.log( "No" )
// This code is contributed by phasing17 |
Yes