"Check for star graph | define the size of incidence matrix ; function to find star graph ; initialize number of vertex with deg 1 and n - 1 ; check for S1 ; check for S2 ; check for Sn ( n > 2 ) ; Driver code"
Solution in php:

< ? php $ size = 4 ; function checkStar ( $ mat ) { global $ size ; $ vertexD1 = 0 ; $ vertexDn_1 = 0 ; if ( $ size == 1 ) return ( $ mat [ 0 ] [ 0 ] == 0 ) ; if ( $ size == 2 ) return ( $ mat [ 0 ] [ 0 ] == 0 && $ mat [ 0 ] [ 1 ] == 1 && $ mat [ 1 ] [ 0 ] == 1 && $ mat [ 1 ] [ 1 ] == 0 ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ degreeI = 0 ; for ( $ j = 0 ; $ j < $ size ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] ) $ degreeI ++ ; if ( $ degreeI == 1 ) $ vertexD1 ++ ; else if ( $ degreeI == $ size - 1 ) $ vertexDn_1 ++ ; } return ( $ vertexD1 == ( $ size - 1 ) && $ vertexDn_1 == 1 ) ; } $ mat = array ( array ( 0 , 1 , 1 , 1 ) , array ( 1 , 0 , 0 , 0 ) , array ( 1 , 0 , 0 , 0 ) , array ( 1 , 0 , 0 , 0 ) ) ; if ( checkStar ( $ mat ) ) echo ( " Star ▁ Graph " ) ; else echo ( " Not ▁ a ▁ Star ▁ Graph " ) ; ? >


"Check given matrix is magic square or not | Returns true if mat [ ] [ ] is magic square , else returns false . ; calculate the sum of the prime diagonal ; the secondary diagonal ; For sums of Rows ; check if every row sum is equal to prime diagonal sum ; For sums of Columns ; check if every column sum is equal to prime diagonal sum ; Driver Code"
Solution in php:

< ? php function isMagicSquare ( $ mat ) { $ sum = 0 ; $ N = 3 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ sum = $ sum + $ mat [ $ i ] [ $ i ] ; $ sum2 = 0 ; $ N = 3 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ sum2 = $ sum2 + $ mat [ $ i ] [ $ N - $ i - 1 ] ; if ( $ sum != $ sum2 ) return false ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ rowSum = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ rowSum += $ mat [ $ i ] [ $ j ] ; if ( $ rowSum != $ sum ) return false ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ colSum = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ colSum += $ mat [ $ j ] [ $ i ] ; if ( $ sum != $ colSum ) return false ; } return true ; } { $ mat = array ( array ( 2 , 7 , 6 ) , array ( 9 , 5 , 1 ) , array ( 4 , 3 , 8 ) ) ; if ( isMagicSquare ( $ mat ) ) echo " Magic ▁ Square " ; else echo " Not ▁ a ▁ magic ▁ Square " ; return 0 ; } ? >


"Check if LCM of array elements is divisible by a prime number or not | Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver Code"
Solution in php:

< ? php function func ( $ a , $ k , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] % $ k == 0 ) return true ; return false ; } $ a = array ( 14 , 27 , 38 , 76 , 84 ) ; $ k = 19 ; $ res = func ( $ a , $ k , 5 ) ; if ( $ res ) echo " true " ; else echo " false " ; ? >


"Check if N can be represented as sum of integers chosen from set { A , B } | Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; Driver Code"
Solution in php:

< ? php function checkIfPossibleRec ( $ x , $ a , $ b , $ isPossible , $ n ) { if ( $ x > $ n ) return ; if ( $ isPossible == true ) return ; $ isPossible [ $ x ] = true ; checkIfPossibleRec ( $ x + $ a , $ a , $ b , $ isPossible , $ n ) ; checkIfPossibleRec ( $ x + $ b , $ a , $ b , $ isPossible , $ n ) ; } function checkPossible ( $ n , $ a , $ b ) { $ isPossible [ $ n + 1 ] = array ( false ) ; checkIfPossibleRec ( 0 , $ a , $ b , $ isPossible , $ n ) ; return $ isPossible ; } $ a = 3 ; $ b = 7 ; $ n = 8 ; if ( checkPossible ( $ a , $ b , $ n ) ) echo " No " ; else echo " Yes " ; ? >


"Check if N is divisible by a number which is composed of the digits from the set { A , B } | Function to check whether n is divisible by a number whose digits are either a or b ; base condition ; recursive call ; Check for all numbers beginning with ' a ' or ' b ' ; Driver Code"
Solution in php:

< ? php function isDivisibleRec ( $ x , $ a , $ b , $ n ) { if ( $ x > $ n ) return false ; if ( $ n % $ x == 0 ) return true ; return ( isDivisibleRec ( $ x * 10 + $ a , $ a , $ b , $ n ) || isDivisibleRec ( $ x * 10 + $ b , $ a , $ b , $ n ) ) ; } function isDivisible ( $ a , $ b , $ n ) { return isDivisibleRec ( $ a , $ a , $ b , $ n ) || isDivisibleRec ( $ b , $ a , $ b , $ n ) ; } $ a = 3 ; $ b = 5 ; $ n = 53 ; if ( isDivisible ( $ a , $ b , $ n ) ) echo " Yes " ; else echo " No " ;


"Check if a M | PHP program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code"
Solution in php:


"Check if a binary string contains consecutive same or not | Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code"
Solution in php:

< ? php function isValid ( $ str , $ len ) { for ( $ i = 1 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == $ str [ $ i - 1 ] ) return false ; } return true ; } $ str = "0110" ; $ len = strlen ( $ str ) ; if ( isValid ( $ str , $ len ) ) echo " Valid " ; else echo " Invalid " ; ? >


"Check if a cell can be visited more than once in a String | Function to check if any cell can be visited more than once ; Array to mark cells ; Traverse the string ; Increase the visit count of the left and right cells within the array which can be visited ; If any cell can be visited more than once , Return True ; Driver code"
Solution in php:

< ? php function checkIfOverlap ( $ str ) { $ len = strlen ( $ str ) ; $ visited = array_fill ( 0 , $ len + 1 , NULL ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == ' . ' ) continue ; for ( $ j = max ( 0 , $ i - $ str [ $ i ] ) ; $ j <= min ( $ len , $ i + $ str [ $ i ] ) ; $ j ++ ) $ visited [ $ j ] ++ ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ visited [ $ i ] > 1 ) { return true ; } } return false ; } $ str = " . 2 . . 2 . " ; if ( checkIfOverlap ( $ str ) ) echo " YES " ; else echo " NO " ; ? >


"Check if a given circle lies completely inside the ring formed by two concentric circles | Function to check if circle lies in the ring ; distance between center of circle center of concentric circles ( origin ) using Pythagoras theorem ; Condition to check if circle is strictly inside the ring ; Both circle with radius ' r ' and ' R ' have center ( 0 , 0 )"
Solution in php:


"Check if a given matrix is Hankel or not | PHP Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Driver code"
Solution in php:

< ? php $ N = 4 ; function checkHankelMatrix ( $ n , $ m ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i + $ j < $ n ) { if ( $ m [ $ i ] [ $ j ] != $ m [ $ i + $ j ] [ 0 ] ) return false ; } else { if ( $ m [ $ i ] [ $ j ] != $ m [ $ i + $ j - $ n + 1 ] [ $ n - 1 ] ) return false ; } } } return true ; } $ n = 4 ; $ m = array ( array ( 1 , 2 , 3 , 5 ) , array ( 2 , 3 , 5 , 8 ) , array ( 3 , 5 , 8 , 0 ) , array ( 5 , 8 , 0 , 9 ) ) ; if ( checkHankelMatrix ( $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a given matrix is sparse or not | PHP code to check if a matrix is sparse . ; Count number of zeros in the matrix ; Driver Code"
Solution in php:

< ? php $ MAX = 100 ; function isSparse ( $ array , $ m , $ n ) { $ counter = 0 ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) for ( $ j = 0 ; $ j < $ n ; ++ $ j ) if ( $ array [ $ i ] [ $ j ] == 0 ) ++ $ counter ; return ( $ counter > ( ( $ m * $ n ) / 2 ) ) ; } $ array = array ( array ( 1 , 0 , 3 ) , array ( 0 , 0 , 4 ) , array ( 6 , 0 , 0 ) ) ; $ m = 3 ; $ n = 3 ; if ( isSparse ( $ array , $ m , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a given number can be represented in given a no . of digits in any base | Returns true if ' num ' can be represented usind ' dig ' digits in ' base ' ; Base case ; If there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; Check for all bases one by one ; Driver Code"
Solution in php:

< ? php function checkUtil ( $ num , $ dig , $ base ) { if ( $ dig == 1 && $ num < $ base ) return true ; if ( $ dig > 1 && $ num >= $ base ) return checkUtil ( $ num / $ base , -- $ dig , $ base ) ; return false ; } function check ( $ num , $ dig ) { for ( $ base = 2 ; $ base <= 32 ; $ base ++ ) if ( checkUtil ( $ num , $ dig , $ base ) ) return true ; return false ; } $ num = 8 ; $ dig = 3 ; if ( check ( $ num , $ dig ) == true ) echo " Yes " ; else echo " No " ; ? >


"Check if a given number divides the sum of the factorials of its digits | Function that returns true if n divides the sum of the factorials of its digits ; To store factorials of digits ; To store sum of the factorials of the digits ; Store copy of the given number ; Store sum of the factorials of the digits ; If it is divisible ; Driver code"
Solution in php:

< ? php function isPossible ( $ n ) { $ fac = array ( ) ; $ fac [ 0 ] = $ fac [ 1 ] = 1 ; for ( $ i = 2 ; $ i < 10 ; $ i ++ ) $ fac [ $ i ] = $ fac [ $ i - 1 ] * $ i ; $ sum = 0 ; $ x = $ n ; while ( $ x ) { $ sum += $ fac [ $ x % 10 ] ; $ x /= 10 ; } if ( $ sum % $ n == 0 ) return true ; return false ; } $ n = 19 ; if ( isPossible ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a given number is Pronic | Efficient Approach | function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code"
Solution in php:

< ? php function pronic_check ( $ n ) { $ x = floor ( sqrt ( $ n ) ) ; if ( $ x * ( $ x + 1 ) == $ n ) return true ; else return false ; } $ n = 56 ; if ( pronic_check ( $ n ) == true ) echo " YES " ; else echo " NO " ; ? >


"Check if a given number is Pronic | function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Printing Pronic Numbers upto 200"
Solution in php:

< ? php function checkPronic ( $ x ) { for ( $ i = 0 ; $ i <= ( sqrt ( $ x ) ) ; $ i ++ ) if ( $ x == $ i * ( $ i + 1 ) ) return true ; return false ; } for ( $ i = 0 ; $ i <= 200 ; $ i ++ ) if ( checkPronic ( $ i ) ) echo $ i , " ▁ " ; ? >


"Check if a large number is divisibility by 15 | to find sum ; function to check if a large number is divisible by 15 ; length of string ; check divisibility by 5 ; Sum of digits ; if divisible by 3 ; Driver Code"
Solution in php:


"Check if a large number is divisible by 11 or not | Function to find that number divisible by 11 or not ; Compute sum of even and odd digit sums ; When i is even , position of digit is odd ; Check its difference is divisible by 11 or not ; Driver code"
Solution in php:

< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; $ oddDigSum = 0 ; $ evenDigSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ oddDigSum += ( $ str [ $ i ] - '0' ) ; else $ evenDigSum += ( $ str [ $ i ] - '0' ) ; } return ( ( $ oddDigSum - $ evenDigSum ) % 11 == 0 ) ; } $ str = "76945" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >


"Check if a large number is divisible by 13 or not | Returns true if number is divisible by 13 else returns false ; Append required 0 s at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code"
Solution in php:

< ? php function checkDivisibility ( $ num ) { $ length = strlen ( $ num ) ; if ( $ length == 1 && $ num [ 0 ] == '0' ) return true ; if ( $ length % 3 == 1 ) { $ num += "00" ; $ length += 2 ; } else if ( $ length % 3 == 2 ) { $ num += "0" ; $ length += 1 ; } $ sum = 0 ; $ p = 1 ; for ( $ i = $ length - 1 ; $ i >= 0 ; $ i -- ) { $ group = 0 ; $ group += $ num [ $ i -- ] - '0' ; $ group += ( $ num [ $ i -- ] - '0' ) * 10 ; $ group += ( $ num [ $ i ] - '0' ) * 100 ; $ sum = $ sum + $ group * $ p ; $ p *= ( -1 ) ; } $ sum = abs ( $ sum ) ; return ( $ sum % 13 == 0 ) ; } $ number = "83959092724" ; if ( checkDivisibility ( $ number ) ) echo ( $ number . " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else echo ( $ number . " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ) ; ? >


"Check if a large number is divisible by 2 , 3 and 5 or not | function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code"
Solution in php:

< ? php function SumOfDigits ( $ str , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( int ) ( $ str [ $ i ] - '0' ) ; return $ sum ; } function Divisible ( $ str , $ n ) { if ( SumOfDigits ( $ str , $ n ) % 3 == 0 and $ str [ $ n - 1 ] == '0' ) return true ; return false ; } $ str = "263730746028908374890" ; $ n = strlen ( $ str ) ; if ( Divisible ( $ str , $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if a large number is divisible by 20 | PHP program to check if a large number is divisible by 20. ; Get number with last two digits ; Check if the number formed by last two digits is divisible by 5 and 4. ; Driver Code"
Solution in php:

< ? php function divisibleBy20 ( $ num ) { $ lastTwoDigits = intval ( substr ( $ num , ( strlen ( $ num ) - 2 ) , 2 ) ) ; return ( ( $ lastTwoDigits % 5 == 0 ) && ( $ lastTwoDigits % 4 == 0 ) ) ; } $ num = "63284689320" ; if ( divisibleBy20 ( $ num ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a large number is divisible by 25 or not | Function to find that number divisible by 25 or not . ; If length of string is single digit then it 's not divisible by 25 ; Driver code"
Solution in php:


"Check if a large number is divisible by 3 or not | Function to find that number divisible by 3 or not ; Compute sum of digits ; Check if sum of digits is divisible by 3. ; Driver code"
Solution in php:

< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; $ digitSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ digitSum += ( $ str [ $ i ] - '0' ) ; return ( $ digitSum % 3 == 0 ) ; } $ str = "1332" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >


"Check if a large number is divisible by 5 or not | Function to find that number divisible by 5 or not . The function assumes that string length is at least one . ; Driver code"
Solution in php:


"Check if a large number is divisible by 6 or not | Function to find that number divisible by 6 or not ; Return false if number is not divisible by 2. ; Compute sum of digits ; Check if sum of digits is divisible by 3 ; Driver code"
Solution in php:

< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; if ( ( $ str [ $ n - 1 ] - '0' ) % 2 != 0 ) return false ; $ digitSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ digitSum += ( $ str [ $ i ] - '0' ) ; return ( $ digitSum % 3 == 0 ) ; } $ str = "1332" ; if ( check ( $ str ) ) echo " Yes " ; else echo " ▁ No ▁ " ; return 0 ; ? >


"Check if a large number is divisible by 75 or not | check divisibleBy3 ; to store sum of Digit ; traversing through each digit ; summing up Digit ; check if sumOfDigit is divisibleBy3 ; check divisibleBy25 ; if a single digit number ; length of the number ; taking the last two digit ; checking if the lastTwo digit is divisibleBy25 ; Function to check divisibleBy75 or not ; check if divisibleBy3 and divisibleBy25 ; Driver Code ; divisible ; if divisibleBy75"
Solution in php:

< ? php function divisibleBy3 ( $ number ) { $ sumOfDigit = 0 ; for ( $ i = 0 ; $ i < strlen ( $ number ) ; $ i ++ ) $ sumOfDigit += $ number [ $ i ] - '0' ; if ( $ sumOfDigit % 3 == 0 ) return true ; return false ; } function divisibleBy25 ( $ number ) { if ( strlen ( $ number ) < 2 ) return false ; $ length = strlen ( $ number ) ; $ lastTwo = ( $ number [ $ length - 2 ] - '0' ) * 10 + ( $ number [ $ length - 1 ] - '0' ) ; if ( $ lastTwo % 25 == 0 ) return true ; return false ; } function divisibleBy75 ( $ number ) { if ( divisibleBy3 ( $ number ) && divisibleBy25 ( $ number ) ) return true ; return false ; } $ number = "754586672150" ; $ divisible = divisibleBy75 ( $ number ) ; if ( $ divisible ) echo " Yes " ; else echo " No " ; ? >


"Check if a large number is divisible by 8 or not | Function to find that number divisible by 8 or not ; Empty string ; If there is single digit ; If there is double digit ; If number formed by last three digits is divisible by 8. ; Driver code"
Solution in php:


"Check if a large number is divisible by 9 or not | Function to find that number divisible by 9 or not ; Compute sum of digits ; Check if sum of digits is divisible by 9. ; Driver code"
Solution in php:

< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; $ digitSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ digitSum += ( $ str [ $ i ] - '0' ) ; return ( $ digitSum % 9 == 0 ) ; } $ str = "99333" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >


"Check if a larger number divisible by 36 | Function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; Driver Code"
Solution in php:


"Check if a line passes through the origin | PHP program to find if line passing through two coordinates also passes through origin or not ; Driver code"
Solution in php:

< ? php function checkOrigin ( $ x1 , $ y1 , $ x2 , $ y2 ) { return ( $ x1 * ( $ y2 - $ y1 ) == $ y1 * ( $ x2 - $ x1 ) ) ; } if ( checkOrigin ( 1 , 28 , 2 , 56 ) == true ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if a number can be expressed as 2 ^ x + 2 ^ y | Utility function to check if a number is power of 2 or not ; Utility function to determine the value of previous power of 2 ; function to check if n can be expressed as 2 ^ x + 2 ^ y or not ; if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y ; if a number is power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; Driver code"
Solution in php:

 < ? php function isPowerOfTwo ( $ n ) { return ( $ n and ! ( $ n & ( $ n - 1 ) ) ) ; } function previousPowerOfTwo ( $ n ) { while ( $ n & $ n - 1 ) { $ n = $ n & $ n - 1 ; } return $ n ; } function checkSum ( $ n ) { if ( $ n == 0 or $ n == 1 ) return false ; else if ( isPowerOfTwo ( $ n ) ) { echo " " ▁ , ▁ $ n ▁ / ▁ 2 ▁ , ▁ " " return true ; } else { $ x = previousPowerOfTwo ( $ n ) ; $ y = $ n - $ x ; if ( isPowerOfTwo ( $ y ) ) { echo $ x , " " , $ y ; return true ; } } return false ; } $ n1 = 20 ; if ( checkSum ( $ n1 ) == false ) echo " No " ; echo "
 " ; $ n2 = 11 ; if ( checkSum ( $ n2 ) == false ) echo " No " ; ? >


"Check if a number can be expressed as a ^ b | Set 2 | PHP program to check if a number can be expressed as a ^ b . ; Driver code"
Solution in php:

< ? php function isPower ( $ a ) { if ( $ a == 1 ) return true ; for ( $ i = 2 ; $ i * $ i <= $ a ; $ i ++ ) { $ val = log ( $ a ) / log ( $ i ) ; if ( ( $ val - $ val ) < 0.00000001 ) return true ; } return false ; } $ n = 16 ; echo ( isPower ( $ n ) ? " Yes " : " No " ) ;


"Check if a number can be expressed as a sum of consecutive numbers | This function returns true if n can be expressed sum of consecutive . ; We basically return true if n is a power of two ; Driver code"
Solution in php:

< ? php function canBeSumofConsec ( $ n ) { return ( ( $ n & ( $ n - 1 ) ) && $ n ) ; } $ n = 15 ; if ( canBeSumofConsec ( $ n ) ) echo " true " ; else echo " false " ; ? >


"Check if a number can be expressed as power | Set 2 ( Using Log ) | PHP program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver code"
Solution in php:

< ? php function isPower ( $ n ) { for ( $ x = 2 ; $ x <= sqrt ( $ n ) ; $ x ++ ) { $ f = log ( $ n ) / log ( $ x ) ; if ( ( $ f - ( int ) $ f ) == 0.0 ) return true ; } return false ; } for ( $ i = 2 ; $ i < 100 ; $ i ++ ) if ( isPower ( ( int ) $ i ) ) echo $ i . " " ; ? >


"Check if a number can be expressed as sum two abundant numbers | Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant number ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code"
Solution in php:

< ? php function ABUNDANT ( ) { $ N = 100005 ; $ v = array ( ) ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ sum = 1 ; for ( $ j = 2 ; $ j * $ j <= $ i ; $ j ++ ) { if ( $ i % $ j == 0 ) { $ sum += $ j ; if ( $ i / $ j != $ j ) $ sum += $ i / $ j ; } } if ( $ sum > $ i ) array_push ( $ v , $ i ) ; } $ v = array_unique ( $ v ) ; return $ v ; } function SumOfAbundant ( $ n ) { $ v = ABUNDANT ( ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( in_array ( $ i , $ v ) && in_array ( $ n - $ i , $ v ) ) { echo $ i , " ▁ " , $ n - $ i ; return ; } } echo - 1 ; } $ n = 24 ; SumOfAbundant ( $ n ) ; ? >


"Check if a number can be represented as a sum of 2 triangular numbers | Function to check if it is possible or not ; Store all triangular numbers up to N in a Set ; Check if a pair exists ; Driver Code"
Solution in php:

< ? php function checkTriangularSumRepresentation ( $ n ) { $ tri = array ( ) ; $ i = 1 ; while ( true ) { $ x = $ i * ( $ i + 1 ) ; if ( $ x >= $ n ) break ; array_push ( $ tri , $ x ) ; $ i += 1 ; } foreach ( $ tri as $ tm ) if ( in_array ( $ n - $ tm , $ tri ) ) return true ; return false ; } $ n = 24 ; if ( checkTriangularSumRepresentation ( $ n ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >


"Check if a number can be represented as sum of non zero powers of 2 | Function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; Driver code"
Solution in php:

< ? php function isSumOfPowersOfTwo ( $ n ) { if ( $ n % 2 == 1 ) return false ; else return true ; } $ n = 10 ; if ( isSumOfPowersOfTwo ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if a number can be written as a sum of ' k ' prime numbers | Checking if a number is prime or not ; check for numbers from 2 to sqrt ( x ) if it is divisible return false ; Returns true if N can be written as sum of K primes ; N < 2 K directly return false ; If K = 1 return value depends on primality of N ; if N is even directly return true ; ; If N is odd , then one prime must be 2. All other primes are odd and cannot have a pair sum as even . ; If K >= 3 return true ; ; Driver Code"
Solution in php:

< ? php function isprime ( $ x ) { for ( $ i = 2 ; $ i * $ i <= $ x ; $ i ++ ) if ( $ x % $ i == 0 ) return false ; return true ; } function isSumOfKprimes ( $ N , $ K ) { if ( $ N < 2 * $ K ) return false ; if ( $ K == 1 ) return isprime ( $ N ) ; if ( $ K == 2 ) { if ( $ N % 2 == 0 ) return true ; return isprime ( $ N - 2 ) ; } return true ; } $ n = 10 ; $ k = 2 ; if ( isSumOfKprimes ( $ n , $ k ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver Code"
Solution in php:


"Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integers . ; if n is multiple of 3 ; else print " - 1" . ; Driver Program"
Solution in php:


"Check if a number from every row can be selected such that xor of the numbers is greater than zero | PHP program to implement the above approach ; Function to check if a number from every row can be selected such that xor of the numbers is greater than zero ; Find the xor of first column for every row ; If Xorr is 0 ; Traverse in the matrix ; Check is atleast 2 distinct elements ; Driver code"
Solution in php:

< ? php $ N = 2 ; $ M = 3 ; function check ( $ mat ) { global $ N ; global $ M ; $ xorr = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ xorr = $ xorr ^ $ mat [ $ i ] [ 0 ] ; } if ( $ xorr != 0 ) return true ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 1 ; $ j < $ M ; $ j ++ ) { if ( $ mat [ $ i ] [ $ j ] != $ mat [ $ i ] [ 0 ] ) return true ; } } return false ; } $ mat = array ( array ( 7 , 7 , 7 ) , array ( 10 , 10 , 7 ) ) ; if ( check ( $ mat ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a number has bits in alternate pattern | Set 1 | Returns true if n has alternate bit pattern else false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver code"
Solution in php:

< ? php function findPattern ( $ n ) { $ prev = $ n % 2 ; $ n = $ n / 2 ; while ( $ n > 0 ) { $ curr = $ n % 2 ; if ( $ curr == $ prev ) return false ; $ prev = $ curr ; $ n = floor ( $ n / 2 ) ; } return true ; } $ n = 10 ; if ( findPattern ( $ n ) ) echo " Yes " ; else echo " No " ; return 0 ; ? >


"Check if a number has bits in alternate pattern | Set | function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; Driver Code"
Solution in php:

< ? php function allBitsAreSet ( $ n ) { if ( ( ( $ n + 1 ) & $ n ) == 0 ) return true ; return false ; } function bitsAreInAltOrder ( $ n ) { $ num = $ n ^ ( $ n >> 1 ) ; return allBitsAreSet ( $ num ) ; } $ n = 10 ; if ( bitsAreInAltOrder ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a number has digits in the given Order | Check if the digits follow the correct order ; to store the previous digit ; pointer to tell what type of sequence are we dealing with ; check if we have same digit as the previous digit ; checking the peak point of the number ; check if we have same digit as the previous digit ; check if the digit is greater than the previous one If true , then break from the loop as we are in descending order part ; Driver code"
Solution in php:

< ? php function isCorrectOrder ( $ n ) { $ flag = true ; $ prev = -1 ; $ type = -1 ; while ( $ n != 0 ) { if ( $ type == -1 ) { if ( $ prev == -1 ) { $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; continue ; } if ( $ prev == $ n % 10 ) { $ flag = false ; break ; } if ( $ prev > $ n % 10 ) { $ type = 1 ; $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; continue ; } $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; } else { if ( $ prev == $ n % 10 ) { $ flag = false ; break ; } if ( $ prev < $ n % 10 ) { $ flag = false ; break ; } $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; } } return $ flag ; } $ n = 123454321 ; if ( isCorrectOrder ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if a number has same number of set and unset bits | Function to check if a number has same setbits and unset bits ; iterate for all bits of a number ; if set ; if unset ; right shift number by 1 ; is number of set bits are equal to unset bits ; Driver Code ; function to check"
Solution in php:

< ? php function checkSame ( $ n ) { $ set = 0 ; $ unset = 0 ; while ( $ n ) { if ( $ n & 1 ) $ set ++ ; else $ unset ++ ; $ n = $ n >> 1 ; } if ( $ set == $ unset ) return true ; else return false ; } $ n = 12 ; if ( checkSame ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code"
Solution in php:

 < ? php function countSetBits ( $ x ) { $ count = 0 ; while ( $ x ) { $ x &= ( $ x - 1 ) ; $ count ++ ; } return $ count ; } function ceilLog2 ( $ x ) { $ count = 0 ; $ x -- ; while ( $ x > 0 ) { $ x = $ x >> 1 ; $ count ++ ; } return $ count ; } function isBleak ( $ n ) { for ( $ x = $ n - ceilLog2 ( $ n ) ; $ x < $ n ; $ x ++ ) if ( $ x + countSetBits ( $ x ) == $ n ) return false ; return true ; } if ( isBleak ( 3 ) ) echo " Yes
 " ; else echo " No
 " ; if ( isBleak ( 4 ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code"
Solution in php:

 < ? php function countSetBits ( $ x ) { $ count = 0 ; while ( $ x ) { $ x &= ( $ x - 1 ) ; $ count ++ ; } return $ count ; } function isBleak ( $ n ) { for ( $ x = 1 ; $ x < $ n ; $ x ++ ) if ( $ x + countSetBits ( $ x ) == $ n ) return false ; return true ; } if ( isBleak ( 3 ) ) echo " Yes
 " ; else echo " No
 " ; if ( isBleak ( 4 ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Check if a number is Full Prime | function to check digits ; check all digits are prime or not ; check if digits are prime or not ; To check if n is prime or not ; check for all factors ; To check if n is Full Prime ; The order is important here for efficiency . ; Driver Code"
Solution in php:

< ? php function checkDigits ( $ n ) { while ( $ n ) { $ dig = $ n % 10 ; if ( $ dig != 2 && $ dig != 3 && $ dig != 5 && $ dig != 7 ) return false ; $ n = ( int ) ( $ n / 10 ) ; } return true ; } function prime ( $ n ) { if ( $ n == 1 ) return false ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) return false ; } return true ; } function isFullPrime ( $ n ) { return ( checkDigits ( $ n ) && prime ( $ n ) ) ; } $ n = 53 ; if ( isFullPrime ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if a number is Quartan Prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 16 * n + 1"
Solution in php:

< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) { if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) { return false ; } } return true ; } $ n = 17 ; if ( isPrime ( $ n ) && ( $ n % 16 == 1 ) ) { echo " YES " ; } else { echo " NO " ; }


"Check if a number is Triperfect Number | Returns true if n is Triperfect ; To store sum of divisors . Adding 1 and n since they are divisors of n . ; Find all divisors and add them ; If sum of divisors is equal to 3 * n , then n is a Triperfect number ; Driver Code"
Solution in php:

< ? php function isTriPerfect ( $ n ) { $ sum = 1 + $ n ; $ i = 2 ; while ( $ i * $ i <= $ n ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ sum = $ sum + $ i ; else $ sum = $ sum + $ i + $ n / $ i ; } $ i += 1 ; } if ( $ sum == 3 * $ n and $ n != 1 ) return true ; else false ; } $ n = 120 ; if ( isTriPerfect ( $ n ) ) echo $ n . " ▁ is ▁ a ▁ Triperfect ▁ number " ; else echo $ n . " ▁ is ▁ not ▁ a ▁ Triperfect ▁ number " ; ? >


"Check if a number is a Mystery Number | Finds reverse of given num x . ; if found print the pair , return"
Solution in php:

< ? php function reverseNum ( $ x ) { $ s = ( string ) $ x ; $ s = strrev ( $ s ) ; $ rev = ( int ) $ s ; return $ rev ; } function isMysteryNumber ( $ n ) { for ( $ i = 1 ; $ i <= $ n / 2 ; $ i ++ ) { $ j = reverseNum ( $ i ) ; if ( $ i + $ j == $ n ) { echo $ i . " ▁ " . $ j ; return true ; } } echo " Not ▁ a ▁ Mystery ▁ Number " ; return false ; } $ n = 121 ; isMysteryNumber ( $ n ) ; return 0 ; ? >


"Check if a number is a Pythagorean Prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 4 * n + 1"
Solution in php:

< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 or $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) { if ( $ n % $ i == 0 or $ n % ( $ i + 2 ) == 0 ) { return false ; } } return true ; } $ n = 13 ; if ( isPrime ( $ n ) && ( $ n % 4 == 1 ) ) { echo " YES " ; } else { echo " NO " ; } ? >


"Check if a number is a power of another number | PHP program to check given number number y ; logarithm function to calculate value ; Note : this is double ; compare to the result1 or result2 both are equal ; Driver Code"
Solution in php:

< ? php function isPower ( $ x , $ y ) { $ res1 = log ( $ y ) / log ( $ x ) ; $ res2 = log ( $ y ) / log ( $ x ) ; return ( $ res1 == $ res2 ) ; } echo isPower ( 27 , 729 ) ; ? >


"Check if a number is a power of another number | Returns 1 if y is a power of x ; The only power of 1 is 1 itself ; Repeatedly comput power of x ; Check if power of x becomes y ; check the result for true / false and print ."
Solution in php:

 < ? php function isPower ( $ x , $ y ) { if ( $ x == 1 ) return ( $ y == 1 ? 1 : 0 ) ; $ pow = 1 ; while ( $ pow < $ y ) $ pow *= $ x ; return ( $ pow == $ y ? 1 : 0 ) ; } echo isPower ( 10 , 1 ) . "
 " ; echo isPower ( 1 , 20 ) . "
 " ; echo isPower ( 2 , 128 ) . "
 " ; echo isPower ( 2 , 30 ) . "
 " ; ? >


"Check if a number is an Achilles number or not | Function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver Code"
Solution in php:

 < ? php function isPowerful ( $ n ) { while ( $ n % 2 == 0 ) { $ power = 0 ; while ( $ n % 2 == 0 ) { $ n /= 2 ; $ power ++ ; } if ( $ power == 1 ) return false ; } for ( $ factor = 3 ; $ factor <= sqrt ( $ n ) ; $ factor += 2 ) { $ power = 0 ; while ( $ n % $ factor == 0 ) { $ n = $ n / $ factor ; $ power ++ ; } if ( $ power == 1 ) return false ; } return ( $ n == 1 ) ; } function isPower ( $ a ) { if ( $ a == 1 ) return true ; for ( $ i = 2 ; $ i * $ i <= $ a ; $ i ++ ) { $ val = log ( $ a ) / log ( $ i ) ; if ( ( $ val - ( int ) $ val ) < 0.00000001 ) return true ; } return false ; } function isAchillesNumber ( $ n ) { if ( isPowerful ( $ n ) && ! isPower ( $ n ) ) return true ; else return false ; } $ n = 72 ; if ( isAchillesNumber ( $ n ) ) echo " YES " , "
 " ; else echo " NO " , "
 " ; $ n = 36 ; if ( isAchillesNumber ( $ n ) ) echo " YES " , "
 " ; else echo " NO " , "
 " ; ? >


"Check if a number is divisible by all prime divisors of another number | PHP program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver Code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { return $ b == 0 ? $ a : gcd ( $ b , $ a % $ b ) ; } function isDivisible ( $ x , $ y ) { if ( $ y == 1 ) return true ; $ z = gcd ( $ x , $ y ) ; if ( $ z == 1 ) return false ; return isDivisible ( $ x , $ y / $ z ) ; } $ x = 18 ; $ y = 12 ; if ( isDivisible ( $ x , $ y ) ) echo " Yes " ; else echo " No " ; ? >


"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 ; check for each possible digit if given number consist other then 1 , 14 , 144 print NO else print YES ; Driver Code"
Solution in php:

< ? php function checkNumber ( $ N ) { $ temp = $ N ; while ( $ temp > 0 ) { 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 " ; } $ N = 1414 ; echo checkNumber ( $ N ) ; ? >


"Check if a number is in given base or not | PHP program to check if given number is in given base or not . ; Allowed bases are till 16 ( Hexadecimal ) ; If base is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver code"
Solution in php:

< ? php function isInGivenBase ( $ str , $ base ) { if ( $ base > 16 ) return false ; else if ( $ base <= 10 ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ! ( $ str [ $ i ] >= '0' and $ str [ $ i ] < ( '0' + $ base ) ) ) return false ; } else { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ! ( ( $ str [ $ i ] >= '0' && $ str [ $ i ] < ( '0' + $ base ) ) || ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] < ( ' A ' + $ base - 10 ) ) ) ) return false ; } return true ; } $ str = " AF87" ; if ( isInGivenBase ( $ str , 16 ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a number is jumbled or not | Function to check if a number is jumbled or not ; Single digit number ; Checking every digit through a loop ; All digits were checked ; Digit at index i ; Digit at index i - 1 ; If difference is greater than 1 ; Number checked ; - 1234 to be checked ; 287 to be checked"
Solution in php:

 < ? php function checkJumbled ( $ num ) { if ( $ num / 10 == 0 ) return true ; while ( $ num != 0 ) { if ( $ num / 10 == 0 ) return true ; $ digit1 = $ num % 10 ; $ digit2 = ( $ num / 10 ) % 10 ; if ( abs ( $ digit2 - $ digit1 ) > 1 ) return false ; $ num = $ num / 10 ; } return true ; } $ num = -1234 ; if ( checkJumbled ( $ num ) ) echo " True ▁
 " ; else echo " False ▁
 " ; $ num = -1247 ; if ( checkJumbled ( $ num ) ) echo " True ▁
 " ; else echo " False ▁
 " ; ? >


"Check if a number is magic ( Recursive sum of digits is 1 ) | PHP program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code"
Solution in php:

< ? php function isMagic ( $ n ) { $ sum = 0 ; while ( $ n > 0 $ sum > 9 ) { if ( $ n == 0 ) { $ n = $ sum ; $ sum = 0 ; } $ sum += $ n % 10 ; $ n /= 10 ; } return ( $ sum == 1 ) ; } $ n = 1234 ; if ( isMagic ( $ n ) ) echo " Magic ▁ Number " ; else echo " Not ▁ a ▁ magic ▁ Number " ; ? >


"Check if a number is multiple of 5 without using / and % operators | Assuming that integre takes 4 bytes , there can be maximum 10 digits in a integer ; Check the last character of string ; Driver Code"
Solution in php:

< ? php $ MAX = 11 ; function isMultipleof5 ( $ n ) { global $ MAX ; $ str = ( string ) $ n ; $ len = strlen ( $ str ) ; if ( $ str [ $ len - 1 ] == '5' $ str [ $ len - 1 ] == '0' ) return true ; return false ; } $ n = 19 ; if ( isMultipleof5 ( $ n ) == true ) echo " $ n ▁ is ▁ multiple ▁ of ▁ 5" ; else echo " $ n ▁ is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ; ? >


"Check if a number is multiple of 5 without using / and % operators | assumes that n is a positive integer ; Driver Code"
Solution in php:

< ? php function isMultipleof5 ( $ n ) { while ( $ n > 0 ) $ n = $ n - 5 ; if ( $ n == 0 ) return true ; return false ; } $ n = 19 ; if ( isMultipleof5 ( $ n ) == true ) echo ( " $ n ▁ is ▁ multiple ▁ of ▁ 5" ) ; else echo ( " $ n ▁ is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ) ; ? >


"Check if a number is positive , negative or zero using bit operators | function to return 1 if it is zero returns 0 if it is negative returns 2 if it is positive ; string array to store all kinds of number ; function call to check the sign of number ; Driver Code"
Solution in php:

 < ? php function index ( $ i ) { return 1 + ( $ i >> 31 ) - ( - $ i >> 31 ) ; } function check ( $ n ) { $ s = array ( " negative " , " zero " , " positive " ) ; $ val = index ( $ n ) ; echo $ n , " ▁ is ▁ " , $ s [ $ val ] , "
 " ; } check ( 30 ) ; check ( -20 ) ; check ( 0 ) ; ? >


"Check if a number is power of 8 or not | function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; Driver Code"
Solution in php:

< ? php function checkPowerof8 ( $ n ) { $ i = log ( $ n ) / log ( 8 ) ; return ( $ i - floor ( $ i ) < 0.000001 ) ; } $ n = 65 ; if ( checkPowerof8 ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a number is power of k using base changing method | PHP program to check if a number can be raised to k ; loop to change base n to base = k ; Find current digit in base k ; If digit is neither 0 nor 1 ; Make sure that only one 1 is present . ; Driver code"
Solution in php:

< ? php function isPowerOfK ( $ n , $ k ) { $ oneSeen = false ; while ( $ n > 0 ) { $ digit = $ n % $ k ; if ( $ digit > 1 ) return false ; if ( $ digit == 1 ) { if ( $ oneSeen ) return false ; $ oneSeen = true ; } $ n = ( int ) $ n / $ k ; } return true ; } $ n = 64 ; $ k = 4 ; if ( isPowerOfK ( $ n , $ k ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a number is sandwiched between primes | returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver Code"
Solution in php:

 < ? php function isPrime ( $ n ) { if ( $ n == 0 $ n == 1 ) return false ; $ root = sqrt ( $ n ) ; for ( $ i = 2 ; $ i <= $ root ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } function isSandwitched ( $ n ) { return ( isPrime ( $ n - 1 ) && isPrime ( $ n + 1 ) ) ; } $ n = 642 ; echo $ n , " ▁ : ▁ " ; if ( isSandwitched ( $ n ) ) echo " Yes
 " ; else echo " No
 " ; $ n = 9 ; echo $ n , " ▁ : ▁ " ; if ( isSandwitched ( $ n ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Check if a number with even number of digits is palindrome or not | Function to check if the number is palindrome ; if divisible by 11 then true ; if not divisible by 11 ; Driver code"
Solution in php:

< ? php function isPalindrome ( $ n ) { if ( $ n % 11 == 0 ) { return true ; } return false ; } echo isPalindrome ( 123321 ) ? " Palindrome " : " Not ▁ Palindrome " ; ? >


"Check if a point is inside , outside or on the ellipse | Function to check the point ; checking the equation of ellipse with the given point ; Driver code"
Solution in php:

< ? php function checkpoint ( $ h , $ k , $ x , $ y , $ a , $ b ) { $ p = ( pow ( ( $ x - $ h ) , 2 ) / pow ( $ a , 2 ) ) + ( pow ( ( $ y - $ k ) , 2 ) / pow ( $ b , 2 ) ) ; return $ p ; } $ h = 0 ; $ k = 0 ; $ x = 2 ; $ y = 1 ; $ a = 4 ; $ b = 5 ; if ( checkpoint ( $ h , $ k , $ x , $ y , $ a , $ b ) > 1 ) echo ( " Outside " ) ; else if ( checkpoint ( $ h , $ k , $ x , $ y , $ a , $ b ) == 1 ) echo ( " On ▁ the ▁ ellipse " ) ; else echo ( " Inside " ) ; ? >


"Check if a point is inside , outside or on the parabola | Function to check the point ; checking the equation of parabola with the given point ; Driver code"
Solution in php:

< ? php function checkpoint ( $ h , $ k , $ x , $ y , $ a ) { $ p = pow ( ( $ y - $ k ) , 2 ) - 4 * $ a * ( $ x - $ h ) ; return $ p ; } $ h = 0 ; $ k = 0 ; $ x = 2 ; $ y = 1 ; $ a = 4 ; if ( checkpoint ( $ h , $ k , $ x , $ y , $ a ) > 0 ) echo " Outside " ; else if ( checkpoint ( $ h , $ k , $ x , $ y , $ a ) == 0 ) echo " On ▁ the ▁ parabola " ; else echo " Inside " ; ? >


"Check if a prime number can be expressed as sum of two Prime Numbers | Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; if the number is prime , and number - 2 is also prime ; Driver code"
Solution in php:

< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) return false ; } return true ; } function isPossible ( $ N ) { if ( isPrime ( $ N ) && isPrime ( $ N - 2 ) ) return true ; else return false ; } $ n = 13 ; if ( isPossible ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if a string contains a palindromic sub | function to check if two consecutive same characters are present ; Driver Code"
Solution in php:

 < ? php function check ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) - 1 ; $ i ++ ) if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) return true ; return false ; } $ s = " xzyyz " ; if ( check ( $ s ) ) echo " YES " , "
 " ; else echo " NO " , "
 " ; ? >


"Check if a string is substring of another | Returns true if s1 is substring of s2 ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; Driver Code"
Solution in php:

< ? php function isSubstring ( $ s1 , $ s2 ) { $ M = strlen ( $ s1 ) ; $ N = strlen ( $ s2 ) ; for ( $ i = 0 ; $ i <= $ N - $ M ; $ i ++ ) { $ j = 0 ; for ( ; $ j < $ M ; $ j ++ ) if ( $ s2 [ $ i + $ j ] != $ s1 [ $ j ] ) break ; if ( $ j == $ M ) return $ i ; } return -1 ; } $ s1 = " for " ; $ s2 = " geeksforgeeks " ; $ res = isSubstring ( $ s1 , $ s2 ) ; if ( $ res == -1 ) echo " Not ▁ present " ; else echo " Present ▁ at ▁ index ▁ " . $ res ; ? >


"Check if a string is suffix of another | PHP program to find if a string is suffix of another ; Driver Code ; Test case - sensitive implementation of endsWith function"
Solution in php:

< ? php function isSuffix ( $ s1 , $ s2 ) { $ n1 = ( $ s1 ) ; $ n2 = strlen ( $ s2 ) ; if ( $ n1 > $ n2 ) return false ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) if ( $ s1 [ $ n1 - $ i - 1 ] != $ s2 [ $ n2 - $ i - 1 ] ) return false ; return true ; } $ s1 = " geeks " ; $ s2 = " geeksforgeeks " ; $ result = isSuffix ( $ s1 , $ s2 ) ; if ( $ result ) echo " Yes " ; else echo " No " ; ? >


"Check if a string is the typed name of the given name | Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver code"
Solution in php:

< ? php function isVowel ( $ c ) { $ vowel = " aeiou " ; for ( $ i = 0 ; $ i < strlen ( $ vowel ) ; ++ $ i ) if ( $ vowel [ $ i ] == $ c ) return true ; return false ; } function printRLE ( $ str , $ typed ) { $ n = strlen ( $ str ) ; $ m = strlen ( $ typed ) ; $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] != $ typed [ $ j ] ) return false ; if ( isVowel ( $ str [ $ i ] ) == false ) { $ j ++ ; continue ; } $ count1 = 1 ; while ( $ i < $ n - 1 && $ str [ $ i ] == $ str [ $ i + 1 ] ) { $ count1 ++ ; $ i ++ ; } $ count2 = 1 ; while ( $ j < $ m - 1 && $ typed [ $ j ] == $ str [ $ i ] ) { $ count2 ++ ; $ j ++ ; } if ( $ count1 > $ count2 ) return false ; } return true ; } $ name = " alex " ; $ typed = " aaalaeex " ; if ( printRLE ( $ name , $ typed ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a triangle of positive area is possible with the given angles | PHP program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Driver Code"
Solution in php:

< ? php function isTriangleExists ( $ a , $ b , $ c ) { if ( $ a != 0 && $ b != 0 && $ c != 0 && ( $ a + $ b + $ c ) == 180 ) if ( ( $ a + $ b ) >= $ c || ( $ b + $ c ) >= $ a || ( $ a + $ c ) >= $ b ) return " YES " ; else return " NO " ; else return " NO " ; } $ a = 50 ; $ b = 60 ; $ c = 70 ; echo isTriangleExists ( $ a , $ b , $ c ) ; ? >


"Check if a two character string can be made using given words | Function to check if str can be made using given words ; If str itself is present ; Match first character of str with second of word and vice versa ; If both characters found . ; Driver Code"
Solution in php:

< ? php function makeAndCheckString ( $ words , $ str ) { $ n = sizeof ( $ words ) ; $ first = false ; $ second = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ words [ $ i ] == $ str ) return true ; if ( $ str [ 0 ] == $ words [ $ i ] [ 1 ] ) $ first = true ; if ( $ str [ 1 ] == $ words [ $ i ] [ 0 ] ) $ second = true ; if ( $ first && $ second ) return true ; } return false ; } $ str = " ya " ; $ words = array ( " ah " , " oy " , " to " , " ha " ) ; if ( makeAndCheckString ( $ words , $ str ) ) echo " Yes " ; else echo " No " ; ? >


"Check if actual binary representation of a number is palindrome | function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; function to check whether binary representation of a number is palindrome or not ; get the number by reversing bits in the binary representation of ' n ' ; Driver code"
Solution in php:

< ? php function reverseBits ( $ n ) { $ rev = 0 ; while ( $ n > 0 ) { $ rev <<= 1 ; if ( $ n & 1 == 1 ) $ rev ^= 1 ; $ n >>= 1 ; } return $ rev ; } function isPalindrome ( $ n ) { $ rev = reverseBits ( $ n ) ; return ( $ n == $ rev ) ; } $ n = 9 ; if ( isPalindrome ( $ n ) ) echo " Yes " ; else echo " No " ; return 0 ; ? >


"Check if all bits of a number are set | function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; Driver Code"
Solution in php:

< ? php function areAllBitsSet ( $ n ) { if ( $ n == 0 ) return " No " ; if ( ( ( $ n + 1 ) & $ n ) == 0 ) return " Yes " ; return " No " ; } $ n = 7 ; echo areAllBitsSet ( $ n ) ; ? >


"Check if all bits of a number are set | function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; loop till n becomes '0' ; if the last bit is not set ; right shift ' n ' by 1 ; all bits are set ; Driver Code"
Solution in php:

< ? php function areAllBitsSet ( $ n ) { if ( $ n == 0 ) return " No " ; while ( $ n > 0 ) { if ( ( $ n & 1 ) == 0 ) return " No " ; $ n = $ n >> 1 ; } return " Yes " ; } $ n = 7 ; echo areAllBitsSet ( $ n ) ; ? >


"Check if all occurrences of a character appear together | To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver Code"
Solution in php:

 < ? php $ oneSeen = false ; $ i = 0 ; $ n = strlen ( $ s ) ; while ( $ i < $ n ) { if ( $ s [ $ i ] == $ c ) { if ( $ oneSeen == true ) return false ; while ( $ i < $ n && $ s [ $ i ] == $ c ) $ i ++ ; $ oneSeen = true ; } else $ i ++ ; } return true ; } $ s = "110029" ; if ( checkIfAllTogether ( $ s , '1' ) ) echo ( " Yes
 " ) ; else echo ( " No
 " ) ; ? >


"Check if all sub | Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; to maintain digit products ; Finding all possible subarrays ; Driver code"
Solution in php:

< ? php function digitProduct ( $ digits , $ start , $ end ) { $ pro = 1 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) { $ pro *= $ digits [ $ i ] ; } return $ pro ; } function isDistinct ( $ N ) { $ s = " $ N " ; $ len = sizeof ( $ s ) ; $ digits = array ( ) ; $ products = array ( ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ digits [ $ i ] = $ s [ $ i ] - '0' ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = $ i ; $ j < $ len ; $ j ++ ) { $ val = digitProduct ( $ digits , $ i , $ j ) ; if ( in_array ( $ val , $ products ) ) return false ; else array_push ( $ products , $ val ) ; } } return true ; } $ N = 324 ; if ( isDistinct ( $ N ) ) echo " YES " ; else echo " NO " ; ? >


"Check if all the palindromic sub | Function to check if the string is palindrome ; Function that checks whether all the palindromic sub - strings are of odd length . ; Creating each substring ; If the sub - string is of even length and is a palindrome then , we return False ; Driver code"
Solution in php:

< ? php function checkPalindrome ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] != $ s [ strlen ( $ s ) - $ i - 1 ] ) return false ; } return true ; } function CheckOdd ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x = " " ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ x = $ x . $ s [ $ i ] ; if ( strlen ( $ x ) % 2 == 0 && checkPalindrome ( $ x ) == true ) return false ; } } return true ; } $ s = " geeksforgeeks " ; if ( CheckOdd ( $ s ) ) echo " YES " ; else echo " NO " ; ? >


"Check if an array contains all elements of a given range | Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present"
Solution in php:

< ? php function check_elements ( $ arr , $ n , $ A , $ B ) { $ range = $ B - $ A ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( abs ( $ arr [ $ i ] ) >= $ z = abs ( $ arr [ $ i ] ) - $ A ; if ( $ arr [ $ z ] > 0 ) { $ arr [ $ z ] = $ arr [ $ z ] * -1 ; } } } $ A && abs ( $ arr [ $ i ] ) <= $ B ) { $ count = 0 ; for ( $ i = 0 ; $ i <= $ range && $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) return -1 ; else $ count ++ ; } if ( $ count != ( $ range + 1 ) ) return -1 ; return true ; } $ arr = array ( 1 , 4 , 5 , 2 , 7 , 8 , 3 ) ; $ n = sizeof ( $ arr ) ; $ A = 2 ; $ B = 5 ; if ( ( check_elements ( $ arr , $ n , $ A , $ B ) ) == true ) echo " Yes " ; else echo " No " ; ? >


"Check if an array is sorted and rotated | Function to check if an array is sorted and rotated clockwise ; Find the minimum element and it 's index ; Check if all elements before minIndex are in increasing order ; Check if all elements after minIndex are in increasing order ; Check if last element of the array is smaller than the element just starting element of the array for arrays like [ 3 , 4 , 6 , 1 , 2 , 5 ] - not sorted circular array ; Driver code ; Function Call"
Solution in php:

< ? php function checkIfSortRotated ( $ arr , $ n ) { $ minEle = PHP_INT_MAX ; $ maxEle = PHP_INT_MIN ; $ minIndex = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < $ minEle ) { $ minEle = $ arr [ $ i ] ; $ minIndex = $ i ; } } $ flag1 = 1 ; for ( $ i = 1 ; $ i < $ minIndex ; $ i ++ ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] ) { $ flag1 = 0 ; break ; } } $ flag2 = 1 ; for ( $ i = $ minIndex + 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] ) { $ flag2 = 0 ; break ; } } if ( $ flag1 && $ flag2 && ( $ arr [ $ n - 1 ] < $ arr [ $ 0 ] ) ) echo ( " YES " ) ; else echo ( " NO " ) ; } $ arr = array ( 3 , 4 , 5 , 1 , 2 ) ; $ n = count ( $ arr ) ; checkIfSortRotated ( $ arr , $ n ) ; ? >


"Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; Driver Code"
Solution in php:

< ? php function isSpiltPossible ( $ n , $ a ) { $ sum = 0 ; $ c1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ a [ $ i ] ; if ( $ a [ $ i ] == 1 ) { $ c1 ++ ; } } if ( $ sum % 2 != 0 ) return false ; if ( ( $ sum / 2 ) % 2 == 0 ) return true ; if ( $ c1 > 0 ) return true ; else return false ; } $ n = 3 ; $ a = array ( 1 , 1 , 2 ) ; if ( isSpiltPossible ( $ n , $ a ) ) echo ( " YES " ) ; else echo ( " NO " ) ; ? >


"Check if an array represents Inorder of Binary Search tree or not | Function that returns true if array is Inorder traversal of any Binary Search Tree or not . ; Array has one or no element ; Unsorted pair found ; No unsorted pair found ; Driver code"
Solution in php:

< ? php function isInorder ( $ arr , $ n ) { if ( $ n == 0 $ n == 1 ) return true ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i - 1 ] > $ arr [ $ i ] ) return false ; return true ; } $ arr = array ( 19 , 23 , 25 , 30 , 45 ) ; $ n = sizeof ( $ arr ) ; if ( isInorder ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if an encoding represents a unique binary string | PHP program to check if given encoding represents a single string ; Return true if sum becomes k ; Driver Code"
Solution in php:

< ? php function isUnique ( $ a , $ n , $ k ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; $ sum += $ n - 1 ; return ( $ sum == $ k ) ; } $ a = array ( 3 , 3 , 3 ) ; $ n = count ( $ a ) ; $ k = 12 ; if ( isUnique ( $ a , $ n , $ k ) ) echo " Yes " ; else echo " No " ; ? >


"Check if any large number is divisible by 17 or not | Function to check if the number is divisible by 17 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 17 ; Driver code"
Solution in php:


"Check if any large number is divisible by 19 or not | Function to check if the number is divisible by 19 or not ; Extracting the last digit ; Truncating the number ; Adding twice the last digit to the remaining number ; return true if number is divisible by 19 ; Driver code"
Solution in php:

< ? php function isDivisible ( $ n ) { while ( 1 ) { $ d = $ n % 10 ; $ n = $ n / 10 ; $ n = $ n + $ d * 2 ; if ( $ n < 100 ) break ; } return ( $ n % 19 == 0 ) ; } $ n = 38 ; if ( isDivisible ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if any permutation of a large number is divisible by 8 | PHP program to check if any permutation of a large number is divisible by 8 or not ; Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Driver Code"
Solution in php:

< ? php error_reporting ( 0 ) ; function solve ( $ n , $ l ) { if ( $ l < 3 ) { if ( intval ( $ n ) % 8 == 0 ) return true ; strrev ( $ n ) ; if ( intval ( $ n ) % 8 == 0 ) return true ; return false ; } $ hash [ 10 ] = array ( 0 ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) $ hash [ $ n [ $ i ] - '0' ] ++ ; for ( $ i = 104 ; $ i < 1000 ; $ i += 8 ) { $ dup = $ i ; $ freq [ 10 ] = array ( 0 ) ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ dup / 10 ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ dup / 10 ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ i ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; $ dup = $ dup / 10 ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; $ dup = $ dup / 10 ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; return true ; } return false ; } $ number = "31462708" ; $ l = strlen ( $ number ) ; if ( solve ( $ number , $ l ) ) echo " Yes " ; else echo " No " ;


"Check if any permutation of a number is divisible by 3 and is Palindromic | Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code"
Solution in php:

 < ? php function isDivisiblePalindrome ( $ n ) { $ hash = array ( 0 ) ; $ digitSum = 0 ; while ( $ n ) { $ digitSum += $ n % 10 ; $ hash ++ ; $ n /= 10 ; } if ( $ digitSum % 3 != 0 ) return false ; $ oddCount = 0 ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) { if ( $ hash % 2 != 0 ) $ oddCount ++ ; } if ( $ oddCount > 1 ) return true ; else return false ; } $ n = 34734 ; if ( isDivisiblePalindrome ( $ n ) ) echo " True " ; else echo " False " ; # This  Code is contributed by Tushill.
? >


"Check if any square ( with one colored cell ) can be divided into two equal parts | function to check if it 's possible to divide the square in two equal parts ; if the painted square is linked anyway to the center of the square then it 's  not possible ; else yes it 's possible ; initialize the size of the square ; initialize the dimension of the painted square"
Solution in php:

< ? php function halfsquare ( $ n , $ x , $ y ) { $ half = $ n / 2 ; if ( ( $ half == $ x $ half == $ x - 1 ) && ( $ half == $ y $ half == $ y - 1 ) ) echo " NO " ; else echo " YES " ; } $ n = 100 ; $ x = 51 ; $ y = 100 ; halfsquare ( $ n , $ x , $ y ) ; ? >


"Check if array elements are consecutive | Added Method 3 | The function checks if the array elements are consecutive . If elements are consecutive , then returns true , else returns false ; 1 ) Get the minimum element in array ; 2 ) Get the maximum element in array ; 3 ) $max - $min + 1 is equal to $n , then only check all elements ; Create a temp array to hold visited flag of all elements . ; If we see an element again , then return false ; If visited first time , then mark the element as visited ; If all elements occur once , then return true ; if ( $max - $min + 1 != $n ) ; UTILITY FUNCTIONS ; Driver Code"
Solution in php:

< ? php function areConsecutive ( $ arr , $ n ) { if ( $ n < 1 ) return false ; $ min = getMin ( $ arr , $ n ) ; $ max = getMax ( $ arr , $ n ) ; if ( $ max - $ min + 1 == $ n ) { $ visited = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ visited [ $ i ] = false ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ visited [ $ arr [ $ i ] - $ min ] != false ) return false ; $ visited [ $ arr [ $ i ] - $ min ] = true ; } return true ; } return false ; } function getMin ( $ arr , $ n ) { $ min = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] < $ min ) $ min = $ arr [ $ i ] ; return $ min ; } function getMax ( $ arr , $ n ) { $ max = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ max ) $ max = $ arr [ $ i ] ; return $ max ; } $ arr = array ( 5 , 4 , 2 , 3 , 1 , 6 ) ; $ n = count ( $ arr ) ; if ( areConsecutive ( $ arr , $ n ) == true ) echo " Array ▁ elements ▁ are ▁ consecutive ▁ " ; else echo " Array ▁ elements ▁ are ▁ not ▁ consecutive ▁ " ; ? >


"Check if array elements are consecutive | Added Method 3 | The function checks if the array elements are consecutive If elements are consecutive , then returns true , else returns false ; 1 ) Get the minimum element in array ; 2 ) Get the maximum element in array ; 3 ) max - min + 1 is equal to n then only check all elements ; if the value at index j is negative then there is repetition ; If we do not see a negative value then all elements are distinct ; if ( max - min + 1 != n ) ; UTILITY FUNCTIONS ; Driver program to test above functions"
Solution in php:

< ? php function areConsecutive ( $ arr , $ n ) { if ( $ n < 1 ) return false ; $ min = getMin ( $ arr , $ n ) ; $ max = getMax ( $ arr , $ n ) ; if ( $ max - $ min + 1 == $ n ) { $ i ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j ; if ( $ arr [ $ i ] < 0 ) $ j = - $ arr [ $ i ] - $ min ; else $ j = $ arr [ $ i ] - $ min ; if ( $ arr [ $ j ] > 0 ) $ arr [ $ j ] = - $ arr [ $ j ] ; else return false ; } return true ; } return false ; } function getMin ( $ arr , $ n ) { $ min = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] < $ min ) $ min = $ arr [ $ i ] ; return $ min ; } function getMax ( $ arr , $ n ) { $ max = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ max ) $ max = $ arr [ $ i ] ; return $ max ; } $ arr = array ( 1 , 4 , 5 , 3 , 2 , 6 ) ; $ n = count ( $ arr ) ; if ( areConsecutive ( $ arr , $ n ) == true ) echo " ▁ Array ▁ elements ▁ are ▁ consecutive ▁ " ; else echo " ▁ Array ▁ elements ▁ are ▁ not ▁ consecutive ▁ " ; ? >


"Check if binary representation of a given number and its complement are anagram | Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code"
Solution in php:

< ? php function bit_anagram_check ( $ a ) { $ longSize = 8 ; $ ULL_SIZE = 8 * $ longSize ; return ( BitCount ( $ a ) == ( $ ULL_SIZE >> 1 ) ) ; } function BitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; } $ a = 4294967295 ; echo ( bit_anagram_check ( $ a ) ) ; ? >


"Check if bits in range L to R of two numbers are complement of each other or not | function to check whether all the bits are set in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if both are equal , then all bits are set in the given range ; else all bits are not set ; function to check whether all the bits in the given range of two numbers are complement of each other ; Driver Code"
Solution in php:

< ? php function allBitsSetInTheGivenRange ( $ n , $ l , $ r ) { $ num = ( ( 1 << $ r ) - 1 ) ^ ( ( 1 << ( $ l - 1 ) ) - 1 ) ; $ new_num = ( $ n & $ num ) ; if ( $ num == $ new_num ) return true ; return false ; } function bitsAreComplement ( $ a , $ b , $ l , $ r ) { $ xor_value = $ a ^ $ b ; return allBitsSetInTheGivenRange ( $ xor_value , $ l , $ r ) ; } $ a = 10 ; $ b = 5 ; $ l = 1 ; $ r = 3 ; if ( bitsAreComplement ( $ a , $ b , $ l , $ r ) ) echo " Yes " ; else echo " No " ; ? >


"Check if bits of a number has count of consecutive set bits in increasing order | Returns true if n has counts of consecutive 1 's are increasing order. ; Initialize previous count ; We traverse bits from right to left and check if counts are decreasing order . ; Ignore 0 s until we reach a set bit . ; Count current set bits ; Compare current with previous and update previous . ; Driver code"
Solution in php:

< ? php function areSetBitsIncreasing ( $ n ) { $ prev_count = PHP_INT_MAX ; while ( $ n > 0 ) { while ( $ n > 0 && $ n % 2 == 0 ) $ n = $ n / 2 ; $ curr_count = 1 ; while ( $ n > 0 and $ n % 2 == 1 ) { $ n = $ n / 2 ; $ curr_count ++ ; } if ( $ curr_count >= $ prev_count ) return false ; $ prev_count = $ curr_count ; } return true ; } $ n = 10 ; if ( areSetBitsIncreasing ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if bitwise AND of any subset is power of two | Check for power of 2 or not ; Check if there exist a subset whose bitwise AND is power of 2. ; if there is only one element in the set . ; Finding a number with all bit sets . ; check all the positions at which the bit is set . ; include all those elements whose i - th bit is set ; check for the set contains elements make a power of 2 or not ; Driver Code"
Solution in php:

< ? php function isPowerOf2 ( $ num ) { return ( $ num && ! ( $ num & ( $ num - 1 ) ) ) ; } function checkSubsequence ( $ arr , $ n ) { $ NUM_BITS = 32 ; if ( $ n == 1 ) return isPowerOf2 ( $ arr [ 0 ] ) ; $ total = 0 ; for ( $ i = 0 ; $ i < $ NUM_BITS ; $ i ++ ) $ total = $ total | ( 1 << $ i ) ; for ( $ i = 0 ; $ i < $ NUM_BITS ; $ i ++ ) { $ ans = $ total ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] & ( 1 << $ i ) ) $ ans = $ ans & $ arr [ $ j ] ; } if ( isPowerOf2 ( $ ans ) ) return true ; } return false ; } $ arr = array ( 12 , 13 , 7 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; if ( checkSubsequence ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if both halves of the string have at least one different character | PHP implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function"
Solution in php:

< ? php $ MAX = 26 ; function function_1 ( $ str ) { global $ MAX ; $ l = strlen ( $ str ) ; $ counter1 = array_fill ( 0 , $ MAX , NULL ) ; $ counter2 = array_fill ( 0 , $ MAX , NULL ) ; for ( $ i = 0 ; $ i < $ l / 2 ; $ i ++ ) $ counter1 [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = $ l / 2 ; $ i < $ l ; $ i ++ ) $ counter2 [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) if ( $ counter2 [ $ i ] != $ counter1 [ $ i ] ) return true ; return false ; } $ str = " abcasdsabcae " ; if ( function_1 ( $ str ) ) echo " Yes , ▁ both ▁ halves ▁ differ " . " ▁ by ▁ at ▁ least ▁ one ▁ character " ; else echo " No , ▁ both ▁ halves ▁ do ▁ " . " not ▁ differ ▁ at ▁ all " ; return 0 ; ? >


"Check if both halves of the string have same set of characters | PHP program to check if it is possible to split string or not ; function to check if we can split string or not ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; String to be checked"
Solution in php:

 < ? php $ MAX_CHAR = 26 ; function checkCorrectOrNot ( $ s ) { global $ MAX_CHAR ; $ count1 = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ count2 = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ n = strlen ( $ s ) ; if ( $ n == 1 ) return true ; for ( $ i = 0 , $ j = $ n - 1 ; $ i < $ j ; $ i ++ , $ j -- ) { $ count1 [ $ s [ $ i ] - ' a ' ] ++ ; $ count2 [ $ s [ $ j ] - ' a ' ] ++ ; } for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ count1 [ $ i ] != $ count2 [ $ i ] ) return false ; return true ; } $ s = " abab " ; if ( checkCorrectOrNot ( $ s ) ) echo " Yes
 " ; else echo " No
 " ; ? >


 < ? php $ MAX_CHAR = 26 ; function checkCorrectOrNot ( $ s ) { global $ MAX_CHAR ; $ count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ n = strlen ( $ s ) ; if ( $ n == 1 ) return true ; for ( $ i = 0 , $ j = $ n - 1 ; $ i < $ j ; $ i ++ , $ j -- ) { $ count [ $ s [ $ i ] - ' a ' ] ++ ; $ count [ $ s [ $ j ] - ' a ' ] -- ; } for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ count [ $ i ] != 0 ) return false ; return true ; } $ s = " abab " ; if ( checkCorrectOrNot ( $ s ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Check if characters of one string can be swapped to form other | PHP program to check if characters of one string can be swapped to form other ; if length is not same print no ; Count frequencies of character in first string . ; iterate through the second string decrement counts of characters in second string ; Since lengths are same , some value would definitely become negative if result is false . ; Driver Code"
Solution in php:

< ? php $ MAX = 26 ; function targetstring ( $ str1 , $ str2 ) { global $ MAX ; $ l1 = strlen ( $ str1 ) ; $ l2 = strlen ( $ str2 ) ; if ( $ l1 != $ l2 ) return false ; $ map [ $ MAX ] = array ( 0 ) ; for ( $ i = 0 ; $ i < $ l1 ; $ i ++ ) $ map [ $ str1 [ $ i ] - ' a ' ] ++ ; for ( $ i = 0 ; $ i < $ l2 ; $ i ++ ) { $ map [ $ str2 [ $ i ] - ' a ' ] -- ; if ( $ map [ $ str2 [ $ i ] - ' a ' ] < 0 ) return false ; } return true ; } $ str1 = " geeksforgeeks " ; $ str2 = " geegeeksksfor " ; if ( targetstring ( $ str1 , $ str2 ) ) echo " YES " ; else echo " NO " ; ? >


"Check if count of divisors is even or odd | Function to count the divisors ; Initialize count of divisors ; Note that this loop runs till square root ; If divisors are equal increment count by one Otherwise increment count by 2 ; Driver Code"
Solution in php:

 < ? php function countDivisors ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) + 1 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ count += ( $ n / $ i == $ i ) ? 1 : 2 ; } if ( $ count % 2 == 0 ) echo " Even
 " ; else echo " Odd
 " ; } echo " The ▁ count ▁ of ▁ divisor : ▁ " ; countDivisors ( 10 ) ; ? >


"Check if edit distance between two strings is one | Returns true if edit distance between s1 and s2 is one , else false ; Find lengths of given strings ; If difference between lengths is more than 1 , then strings can 't  be at one distance ; Count of edits ; If current characters don 't match ; If length of one string is more , then only possible edit is to remove a character ; If lengths of both strings is same ; Increment count of edits ; If current characters match ; If last character is extra in any string ; Driver Code"
Solution in php:

< ? php function isEditDistanceOne ( $ s1 , $ s2 ) { $ m = strlen ( $ s1 ) ; $ n = strlen ( $ s2 ) ; if ( abs ( $ m - $ n ) > 1 ) return false ; $ count = 0 ; $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ s1 [ $ i ] != $ s2 [ $ j ] ) { if ( $ count == 1 ) return false ; if ( $ m > $ n ) $ i ++ ; else if ( $ m < $ n ) $ j ++ ; else { $ i ++ ; $ j ++ ; } $ count ++ ; } else { $ i ++ ; $ j ++ ; } } if ( $ i < $ m $ j < $ n ) $ count ++ ; return $ count == 1 ; } $ s1 = " gfg " ; $ s2 = " gf " ; if ( isEditDistanceOne ( $ s1 , $ s2 ) ) echo " Yes " ; else echo " No " ; ? >


"Check if factorial of N is divisible by the sum of squares of first N natural numbers | Function to count number of times prime P divide factorial N ; Lengendre Formula ; Function to find count number of times all prime P divide summation ; Formula for summation of square after removing n and constant 6 ; Loop to traverse over all prime P which divide summation ; If Number itself is a Prime Number ; Driver Code"
Solution in php:

< ? php function checkfact ( $ N , $ countprime , $ prime ) { $ countfact = 0 ; if ( $ prime == 2 $ prime == 3 ) $ countfact ++ ; $ divide = $ prime ; while ( ( int ) ( $ N / $ divide ) != 0 ) { $ countfact += ( int ) ( $ N / $ divide ) ; $ divide = $ divide * $ divide ; } if ( $ countfact >= $ countprime ) return true ; else return false ; } function check ( $ N ) { $ sumsquares = ( $ N + 1 ) * ( 2 * $ N + 1 ) ; $ countprime = 0 ; for ( $ i = 2 ; $ i <= sqrt ( $ sumsquares ) ; $ i ++ ) { $ flag = 0 ; while ( $ sumsquares % $ i == 0 ) { $ flag = 1 ; $ countprime ++ ; $ sumsquares = ( int ) ( $ sumsquares / $ i ) ; } if ( $ flag == 1 ) { if ( checkfact ( $ N - 1 , $ countprime , $ i ) ) return false ; $ countprime = 0 ; } } if ( $ sumsquares != 1 ) if ( checkfact ( $ N - 1 , 1 , $ sumsquares ) ) return false ; return true ; } $ N = 5 ; if ( check ( $ N ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if frequency of all characters can become same by one removal | PHP program to get same frequency character string by removal of at most one char ; Utility method to get index of character ch in lower alphabet characters ; Returns true if all non - zero elements values are same ; get first non - zero element ; check equality of each element with variable same ; Returns true if we can make all character frequencies same ; fill frequency array ; if all frequencies are same , then return true ; Try decreasing frequency of all character by one and then check all equality of all non - zero frequencies ; Check character only if it occurs in str ; Driver code"
Solution in php:

< ? php $ M = 26 ; function getIdx ( $ ch ) { return ( $ ch - ' a ' ) ; } function allSame ( & $ freq , $ N ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ freq [ $ i ] > 0 ) { $ same = $ freq [ $ i ] ; break ; } } for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ freq [ $ j ] > 0 && $ freq [ $ j ] != $ same ) return false ; return true ; } function possibleSameCharFreqByOneRemoval ( $ str ) { global $ M ; $ l = strlen ( $ str ) ; $ freq = array_fill ( 0 , $ M , NULL ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) $ freq [ getIdx ( $ str [ $ i ] ) ] ++ ; if ( allSame ( $ freq , $ M ) ) return true ; for ( $ c = ' a ' ; $ c <= ' z ' ; $ c ++ ) { $ i = getIdx ( $ c ) ; if ( $ freq [ $ i ] > 0 ) { $ freq [ $ i ] -- ; if ( allSame ( $ freq , $ M ) ) return true ; $ freq [ $ i ] ++ ; } } return false ; } $ str = " xyyzz " ; if ( possibleSameCharFreqByOneRemoval ( $ str ) ) echo " Yes " ; else echo " No " ; ? >


"Check if frequency of each digit is less than the digit | Function to validate number ( Check if frequency of adigit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; Driver Code"
Solution in php:

< ? php function validate ( $ n ) { for ( $ i = 0 ; $ i < 10 ; $ i ++ ) { $ temp = $ n ; $ count = 0 ; while ( $ temp ) { if ( $ temp % 10 == $ i ) $ count ++ ; if ( $ count > $ i ) return -1 ; $ temp /= 10 ; } } return 1 ; } $ n = 1552793 ; $ geek = validate ( $ n ) ? " True " : " False " ; echo ( $ geek ) ; ? >


"Check if given array is almost sorted ( elements are at | function for checking almost sort ; One by one compare adjacents . ; check whether resultant is sorted or not ; is resultant is sorted return true ; Driver Code"
Solution in php:


"Check if given number is a power of d where d is a power of 2 | PHP program to find if a number is power of d where d is power of 2. ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver Code"
Solution in php:

< ? php function Log2n ( $ n ) { return ( $ n > 1 ) ? 1 + Log2n ( $ n / 2 ) : 0 ; } function isPowerOfd ( $ n , $ d ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) { while ( $ n > 1 ) { $ n >>= 1 ; $ count += 1 ; } return ( $ count % ( Log2n ( $ d ) ) == 0 ) ; } return false ; } $ n = 64 ; $ d = 8 ; if ( isPowerOfd ( $ n , $ d ) ) echo $ n , " ▁ " , " is ▁ a ▁ power ▁ of ▁ " , $ d ; else echo $ n , " ▁ " , " is ▁ not ▁ a ▁ power ▁ of ▁ " , $ d ; ? >


"Check if given number is perfect square | PHP program to find if x is a perfect square . ; Find floating point value of square root of x . ; If square root is an integer ; Driver code"
Solution in php:

< ? php function isPerfectSquare ( $ x ) { $ sr = sqrt ( $ x ) ; return ( ( $ sr - floor ( $ sr ) ) == 0 ) ; } $ x = 2502 ; if ( isPerfectSquare ( $ x ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if given two straight lines are identical or not | Function to check if they are identical ; Driver Code"
Solution in php:

 < ? php function idstrt ( $ a1 , $ b1 , $ c1 , $ a2 , $ b2 , $ c2 ) { if ( ( $ a1 / $ a2 == $ b1 / $ b2 ) && ( $ a1 / $ a2 == $ c1 / $ c2 ) && ( $ b1 / $ b2 == $ c1 / $ c2 ) ) echo " The ▁ given ▁ straight ▁ lines ▁ are ▁ identical " , "
 " ; else echo " The ▁ given ▁ straight ▁ lines ▁ are ▁ not ▁ identical " , "
 " ; } $ a1 = -2 ; $ b1 = 4 ; $ c1 = 3 ; $ a2 = -6 ; $ b2 = 12 ; $ c2 = 9 ; idstrt ( $ a1 , $ b1 , $ c1 , $ a2 , $ b2 , $ c2 ) ; ? >


"Check if it is possible to create a polygon with a given angle | Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer"
Solution in php:

< ? php function makePolygon ( $ a ) { $ n = 360 / ( 180 - $ a ) ; if ( $ n == ( int ) $ n ) echo " YES " ; else echo " NO " ; } $ a = 90 ; makePolygon ( $ a ) ; ? >


"Check if it is possible to create a polygon with given n sides | Function that returns true if it is possible to form a polygon with the given sides ; Sum stores the sum of all the sides and maxS stores the length of the largest side ; If the length of the largest side is less than the sum of the other remaining sides ; Driver code"
Solution in php:

< ? php function isPossible ( $ a , $ n ) { $ sum = 0 ; $ maxS = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ a [ $ i ] ; $ maxS = max ( $ a [ $ i ] , $ maxS ) ; } if ( ( $ sum - $ maxS ) > $ maxS ) return true ; return false ; } $ a = array ( 2 , 3 , 4 ) ; $ n = count ( $ a ) ; if ( isPossible ( $ a , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if it is possible to draw a straight line with the given direction cosines | Function that returns true if a straight line is possible ; Driver code"
Solution in php:

< ? php function isPossible ( $ x , $ y , $ z ) { $ a = round ( $ x * $ x + $ y * $ y + $ z * $ z ) ; if ( ceil ( $ a ) == 1 && floor ( $ a ) == 1 ) return true ; return false ; } $ l = 0.70710678 ; $ m = 0.5 ; $ n = 0.5 ; if ( isPossible ( $ l , $ m , $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ;


"Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; Check if jth bit in the counter is set If set then print jth element from set ; $sum += $a [ $j ] ; if set then consider as ' + ' ; $sum -= $a [ $j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver Code"
Solution in php:

< ? php function checkCombinations ( $ a , $ n ) { $ pow_set_size = pow ( 2 , $ n ) ; for ( $ counter = 0 ; $ counter < $ pow_set_size ; $ counter ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ counter & ( 1 << $ j ) ) else } if ( $ sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } $ a = array ( 60 , 60 , 120 ) ; $ n = sizeof ( $ a ) ; if ( checkCombinations ( $ a , $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver code"
Solution in php:


"Check if it is possible to move from ( 0 , 0 ) to ( x , y ) in N steps | Function to check whether it is possible or not to move from ( 0 , 0 ) to ( x , y ) in exactly n steps ; Driver code"
Solution in php:

< ? php function Arrive ( $ a , $ b , $ n ) { if ( $ n >= abs ( $ a ) + abs ( $ b ) and ( $ n - ( abs ( $ a ) + abs ( $ b ) ) ) % 2 == 0 ) return true ; return false ; } $ a = 5 ; $ b = 5 ; $ n = 11 ; if ( Arrive ( $ a , $ b , $ n ) ) echo " Yes " ; else echo " No " ;


"Check if it is possible to move from ( a , 0 ) to ( b , 0 ) with given jumps | Function to check if it is possible ; Driver code ; function call"
Solution in php:


"Check if it is possible to reach vector B by rotating vector A and adding vector C to it | function to check if vector B is possible from vector A ; if d = 0 , then you need to add nothing to vector A ; for all four quadrants ; initialize all three vector coordinates"
Solution in php:


"Check if it is possible to rearrange rectangles in a non | Function to check if it possible to form rectangles with heights as non - ascending ; set maximum ; replace the maximum with previous maximum ; replace the minimum with previous minimum ; print NO if the above two conditions fail at least once ; initialize the number of rectangles ; initialize n rectangles with length and breadth"
Solution in php:


"Check if it is possible to return to the starting position after moving in the given directions | Main method ; $n = 0 ; Count of North $s = 0 ; Count of South $e = 0 ; Count of East $w = 0 ; Count of West"
Solution in php:


"Check if it is possible to serve customer queue with different notes | Function that returns true is selling of the tickets is possible ; Nothing to return to the customer ; Check if 25 can be returned to customer . ; Try returning one 50 and one 25 ; Try returning three 25 ; If the loop did not break , all the tickets were sold ; Driver Code"
Solution in php:


"Check if it is possible to sort an array with conditional swapping of adjacent allowed | Returns true if it is possible to sort else false ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver Code"
Solution in php:

< ? php function checkForSorting ( $ arr , $ n ) { $ temp = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] > $ arr [ $ i + 1 ] ) { if ( $ arr [ $ i ] - $ arr [ $ i + 1 ] == 1 ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ i + 1 ] ; $ arr [ $ i + 1 ] = $ temp ; } else return false ; } } return true ; } $ arr = array ( 1 , 0 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; if ( checkForSorting ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if it is possible to survive on Island | Function to find the minimum $days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver code"
Solution in php:


"Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | PHP implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; First binary matrix ; Second binary matrix"
Solution in php:


"Check if matrix can be converted to another matrix by transposing square sub | PHP implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code"
Solution in php:


"Check if mirror image of a number is same if displayed in seven segment display | Return " Yes " , if the mirror image of number is same as the given number Else return " No " ; Checking if the number contain only 0 , 1 , 8. ; Checking if the number is palindrome or not . ; If corresponding index is not equal . ; Driver Code"
Solution in php:

< ? php function checkEqual ( $ S ) { for ( $ i = 0 ; $ i < strlen ( $ S ) ; $ i ++ ) { if ( $ S [ $ i ] != '1' && $ S [ $ i ] != '0' && $ S [ $ i ] != '8' ) { return " No " ; } } $ start = 0 ; $ end = strlen ( $ S ) - 1 ; while ( $ start < $ end ) { if ( $ S [ $ start ] != $ S [ $ end ] ) { return " No " ; } $ start ++ ; $ end -- ; } return " Yes " ; } $ S = "101" ; echo checkEqual ( $ S ) ; ? >


"Check if n is divisible by power of 2 without using arithmetic operators | function to check whether n is divisible by pow ( 2 , m ) ; if expression results to 0 , then n is divisible by pow ( 2 , m ) ; n is not divisible ; Driver Code"
Solution in php:

< ? php function isDivBy2PowerM ( $ n , $ m ) { if ( ( $ n & ( ( 1 << $ m ) - 1 ) ) == 0 ) return true ; return false ; } $ n = 8 ; $ m = 2 ; if ( isDivBy2PowerM ( $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >


"Check if number can be displayed using seven segment led | Pre - computed values of segment used by digit 0 to 9. ; Check if it is possible to display the number ; Finding sum of the segments used by each digit of the number ; Driver Code ; Function call to print required answer"
Solution in php:

< ? php $ seg = array ( 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ) ; function LedRequired ( $ s , $ led ) { $ count = 0 ; global $ seg ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; ++ $ i ) { $ count += $ seg [ ord ( $ s [ $ i ] ) - 48 ] ; } if ( $ count <= $ led ) return " YES " ; else return " NO " ; } $ S = "123456789" ; $ led = 20 ; echo LedRequired ( $ S , $ led ) ; ? >


"Check if number is palindrome or not in Octal | PHP program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver Code"
Solution in php:

< ? php $ MAX_DIGITS = 20 ; function isOctal ( $ n ) { while ( $ n ) { if ( ( $ n % 10 ) >= 8 ) return false ; else $ n = ( int ) $ n / 10 ; } return true ; } function isPalindrome ( $ n ) { global $ MAX_DIGITS ; $ divide = ( isOctal ( $ n ) == false ) ? 8 : 10 ; $ octal ; $ i = 0 ; while ( $ n != 0 ) { $ octal [ $ i ++ ] = $ n % $ divide ; $ n = ( int ) $ n / $ divide ; } for ( $ j = $ i - 1 , $ k = 0 ; $ k <= $ j ; $ j -- , $ k ++ ) if ( $ octal [ $ j ] != $ octal [ $ k ] ) return -1 ; return 0 ; } $ n = 97 ; if ( isPalindrome ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if one of the numbers is one 's complement of the other | function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; function to check if one of the two numbers is one 's complement of the other ; Driver Code"
Solution in php:

< ? php function areAllBitsSet ( $ n ) { if ( $ n == 0 ) return false ; if ( ( ( $ n + 1 ) & $ n ) == 0 ) return true ; return false ; } function isOnesComplementOfOther ( $ a , $ b ) { return areAllBitsSet ( $ a ^ $ b ) ; } $ a = 10 ; $ b = 5 ; if ( isOnesComplementOfOther ( $ a , $ b ) ) echo " Yes " ; else echo " No " ; ? >


"Check if possible to move from given coordinate to desired coordinate | Returns GCD of i and j ; Returns true if it is possible to go to ( a , b ) from ( x , y ) ; Find absolute values of all as sign doesn 't  matter. ; If gcd is equal then it is possible to reach . Else not possible . ; Driver Code ; Converting coordinate into positive integer"
Solution in php:

< ? php function gcd ( $ i , $ j ) { if ( $ i == $ j ) return $ i ; if ( $ i > $ j ) return gcd ( $ i - $ j , $ j ) ; return gcd ( $ i , $ j - $ i ) ; } function ispossible ( $ x , $ y , $ a , $ b ) { $ x = abs ( $ x ) ; $ y = abs ( $ y ) ; $ a = abs ( $ a ) ; $ b = abs ( $ b ) ; return ( gcd ( $ x , $ y ) == gcd ( $ a , $ b ) ) ; } { $ x = 35 ; $ y = 15 ; $ a = 20 ; $ b = 25 ; if ( ispossible ( $ x , $ y , $ a , $ b ) ) echo ( " Yes " ) ; else echo ( " No " ) ; return 0 ; } ? >


"Check if product of first N natural numbers is divisible by their sum | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; Driver code"
Solution in php:

< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function isDivisible ( $ n ) { if ( isPrime ( $ n + 1 ) ) return false ; return true ; } $ n = 6 ; if ( isDivisible ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if reversing a sub array make the array sorted | Return true , if reversing the subarray will sort the array , else return false . ; Copying the array . ; Sort the copied array . ; Finding the first mismatch . ; Finding the last mismatch . ; If whole array is sorted ; Checking subarray is decreasing or not . ; Driver Code"
Solution in php:

 < ? php function checkReverse ( $ arr , $ n ) { $ temp [ $ n ] = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ temp [ $ i ] = $ arr [ $ i ] ; sort ( $ temp , 0 ) ; $ front ; for ( $ front = 0 ; $ front < $ n ; $ front ++ ) if ( $ temp [ $ front ] != $ arr [ $ front ] ) break ; $ back ; for ( $ back = $ n - 1 ; $ back >= 0 ; $ back -- ) if ( $ temp [ $ back ] != $ arr [ $ back ] ) break ; if ( $ front >= $ back ) return true ; do { $ front ++ ; if ( $ arr [ $ front - 1 ] < $ arr [ $ front ] ) return false ; } while ( $ front != $ back ) ; return true ; } $ arr = array ( 1 , 2 , 5 , 4 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( checkReverse ( $ arr , $ n ) ) echo " Yes " . "
 " ; else echo " No " . "
 " ; ? >


"Check if reversing a sub array make the array sorted | Return true , if reversing the subarray will sort the array , else return false . ; Find first increasing part ; Find reversed part ; Find last increasing part ; To handle cases like { 1 , 2 , 3 , 4 , 20 , 9 , 16 , 17 } ; Driver Code"
Solution in php:

< ? php function checkReverse ( $ arr , $ n ) { if ( $ n == 1 ) return true ; for ( $ i = 1 ; $ i < $ n && $ arr [ $ i - 1 ] < $ arr [ $ i ] ; $ i ++ ) ; if ( $ i == $ n ) return true ; $ j = $ i ; while ( $ arr [ $ j ] < $ arr [ $ j - 1 ] ) { if ( $ i > 1 && $ arr [ $ j ] < $ arr [ $ i - 2 ] ) return false ; $ j ++ ; } if ( $ j == $ n ) return true ; $ k = $ j ; if ( $ arr [ $ k ] < $ arr [ $ i - 1 ] ) return false ; while ( $ k > 1 && $ k < $ n ) { if ( $ arr [ $ k ] < $ arr [ $ k - 1 ] ) return false ; $ k ++ ; } return true ; } $ arr = array ( 1 , 3 , 4 , 10 , 9 , 8 ) ; $ n = sizeof ( $ arr ) ; if ( checkReverse ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if string can be made lexicographically smaller by reversing any substring | Function that returns true if s can be made lexicographically smaller by reversing a sub - string in s ; Traverse in the string ; Check if $s [ $i + 1 ] < $s [ $i ] ; Not possible ; Driver code"
Solution in php:

< ? php function check ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ s [ $ i ] > $ s [ $ i + 1 ] ) return true ; } return false ; } $ s = " geeksforgeeks " ; if ( check ( $ s ) ) echo " Yes " ; else echo " No " ; ? >


"Check if suffix and prefix of a string are palindromes | Function to check whether the string is a palindrome ; reverse the string to compare with the original string ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code"
Solution in php:

 < ? php function isPalindrome ( $ r ) { $ p = $ r ; strrev ( $ p ) ; return ( $ r == $ p ) ; } function CheckStr ( $ s ) { $ l = strlen ( $ s ) ; for ( $ i = 2 ; $ i <= $ l ; $ i ++ ) { if ( isPalindrome ( substr ( $ s , 0 , $ i ) ) ) break ; } if ( $ i == ( $ l + 1 ) ) return false ; $ i = 2 ; for ( $ i = 2 ; $ i <= $ l ; $ i ++ ) { if ( isPalindrome ( substr ( $ s , $ l - $ i , $ i ) ) ) return true ; } return false ; } $ s = " abccbarfgdbd " ; if ( CheckStr ( $ s ) ) echo ( " YES
 " ) ; else echo ( " NO
 " ) ; ? >


"Check if sum of divisors of two numbers are same | Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Driver code"
Solution in php:

< ? php function divSum ( $ n ) { $ sum = 1 ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) $ sum = $ sum + $ i + floor ( $ n / $ i ) ; return $ sum ; } function areEquivalent ( $ num1 , $ num2 ) { return divSum ( $ num1 ) == divSum ( $ num2 ) ; } $ num1 = 559 ; $ num2 = 703 ; if ( areEquivalent ( $ num1 , $ num2 ) == true ) echo " Equivalent " ; else echo " Not ▁ Equivalent " ; ? >


"Check if sums of i | Function to check the if sum of a row is same as corresponding column ; number of rows ; number of columns"
Solution in php:


"Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Driver Code"
Solution in php:

< ? php function CheckArray ( $ arr , $ n ) { $ prod = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ prod *= $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) if ( $ arr [ $ i ] == $ prod / $ arr [ $ i ] ) return true ; return false ; } $ arr = array ( 1 , 2 , 12 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; if ( CheckArray ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ;


"Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square ; then check if its square root exist in the array or not ; Driver code"
Solution in php:

< ? php function CheckArray ( $ arr , $ n ) { $ prod = 1 ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { array_push ( $ freq , $ arr [ $ i ] ) ; $ prod *= $ arr [ $ i ] ; } $ freq = array_unique ( $ freq ) ; $ root = ( int ) ( sqrt ( $ prod ) ) ; if ( $ root * $ root == $ prod ) if ( in_array ( $ root , $ freq ) ) return true ; return false ; } $ arr = array ( 1 , 2 , 12 , 3 , 2 ) ; $ n = count ( $ arr ) ; if ( CheckArray ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | function to convert decimal to binary ; Count same bits in last block ; If n is 0 or it has all 1 s , then it is not considered to have equal number of 0 s and 1 s in blocks . ; Count same bits in all remaining blocks . ; Driver Code"
Solution in php:

< ? php function isEqualBlock ( $ n ) { $ first_bit = $ n % 2 ; $ first_count = 1 ; $ n = ( int ) ( $ n / 2 ) ; while ( $ n % 2 == $ first_bit && $ n > 0 ) { $ n = ( int ) ( $ n / 2 ) ; $ first_count ++ ; } if ( $ n == 0 ) return false ; while ( $ n > 0 ) { $ first_bit = $ n % 2 ; $ curr_count = 1 ; $ n = ( int ) ( $ n / 2 ) ; while ( $ n % 2 == $ first_bit ) { $ n = ( int ) ( $ n / 2 ) ; $ curr_count ++ ; } if ( $ curr_count != $ first_count ) return false ; } return true ; } $ n = 51 ; if ( isEqualBlock ( $ n ) ) echo " Yes " ; else echo " No " ; ? >