"Finding number of digits in n 'th Fibonacci number | This function returns the number of digits in nth Fibonacci number after ceiling it Formula used ( n * log ( phi ) - ( log 5 ) / 2 ) ; using phi = 1.6180339887498948 ; Driver Code"
Solution in php:

 < ? php function numberOfDigits ( $ n ) { if ( $ n == 1 ) return 1 ; $ d = ( $ n * log10 ( 1.6180339887498948 ) ) - ( ( log10 ( 5 ) ) / 2 ) ; return ceil ( $ d ) ; } $ i ; for ( $ i = 1 ; $ i <= 10 ; $ i ++ ) echo " Number ▁ of ▁ Digits ▁ in ▁ F ( $ i ) ▁ - ▁ " , numberOfDigits ( $ i ) , "
 " ; ? >


"Finding power of prime number p in n ! | Returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver function"
Solution in php:

 < ? php function PowerOFPINnfactorial ( $ n , $ p ) { $ ans = 0 ; $ temp = $ p ; while ( $ temp <= $ n ) { $ ans += $ n / $ temp ; $ temp = $ temp * $ p ; } return $ ans ; } echo PowerOFPINnfactorial ( 4 , 2 ) . "
 " ; ? >


"Finding sum of digits of a number until sum becomes single digit | Driver program to test the above function"
Solution in php:

< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } $ n = 9999 ; echo digSum ( $ n ) ; ? >


"Finding sum of digits of a number until sum becomes single digit | PHP program to find sum of digits of a number until sum becomes single digit . ; Loop to do sum while sum is not less than or equal to 9 ; Driver Code"
Solution in php:

< ? php function digSum ( $ n ) { $ sum = 0 ; while ( $ n > 0 $ sum > 9 ) { if ( $ n == 0 ) { $ n = $ sum ; $ sum = 0 ; } $ sum += $ n % 10 ; $ n = ( int ) $ n / 10 ; } return $ sum ; } $ n = 1234 ; echo digSum ( $ n ) ; ? >


"Finding the Parity of a number Efficiently | Function to find the parity ; Rightmost bit of y holds the parity value if ( y & 1 ) is 1 then parity is odd else even ; Driver code"
Solution in php:

 < ? php function findParity ( $ x ) { $ y = $ x ^ ( $ x >> 1 ) ; $ y = $ y ^ ( $ y >> 2 ) ; $ y = $ y ^ ( $ y >> 4 ) ; $ y = $ y ^ ( $ y >> 8 ) ; $ y = $ y ^ ( $ y >> 16 ) ; if ( $ y & 1 ) return 1 ; return 0 ; } ( findParity ( 9 ) == 0 ) ? print ( " Even ▁ Parity
 " ) : print ( " Odd ▁ Parity
 " ) ; ( findParity ( 13 ) == 0 ) ? print ( " Even ▁ Parity
 " ) : print ( " Odd ▁ Parity
 " ) ; ? >


"Finding the maximum square sub | Java program to find maximum K such that K x K is a submatrix with equal elements . ; Returns size of the largest square sub - matrix with all same elements . ; If elements is at top row or first column , it wont form a square matrix 's   bottom-right ; Check if adjacent elements are equal ; If not equal , then it will form a 1 x1 submatrix ; Update result at each ( i , j ) ; Driver Code"
Solution in php:


"First N natural can be divided into two sets with given difference and co | PHP code to determine whether numbers 1 to N can be divided into two sets such that absolute difference between sum of these two sets is M and these two sum are co - prime ; function that returns boolean value on the basis of whether it is possible to divide 1 to N numbers into two sets that satisfy given conditions . ; initializing total sum of 1 to n numbers ; since ( 1 ) total_sum = sum_s1 + sum_s2 and ( 2 ) m = sum_s1 - sum_s2 assuming sum_s1 > sum_s2 . solving these 2 equations to get sum_s1 and sum_s2 ; total_sum = sum_s1 + sum_s2 and therefore ; if total sum is less than the absolute difference then there is no way we can split n numbers into two sets so return false ; check if these two sums are integers and they add up to total sum and also if their absolute difference is m . ; Now if two sum are co - prime then return true , else return false . ; if two sums don 't add up to total  sum or if their absolute difference  is not m, then there is no way to  split n numbers, hence return false ; Driver code ; function call to determine answer"
Solution in php:

< ? php function __gcd ( $ a , $ b ) { return $ b == 0 ? $ a : __gcd ( $ b , $ a % $ b ) ; } function isSplittable ( $ n , $ m ) { $ total_sum = ( int ) ( ( $ n * ( $ n + 1 ) ) / 2 ) ; $ sum_s1 = ( int ) ( ( $ total_sum + $ m ) / 2 ) ; $ sum_s2 = $ total_sum - $ sum_s1 ; if ( $ total_sum < $ m ) return false ; if ( $ sum_s1 + $ sum_s2 == $ total_sum && $ sum_s1 - $ sum_s2 == $ m ) return ( __gcd ( $ sum_s1 , $ sum_s2 ) == 1 ) ; return false ; } $ n = 5 ; $ m = 7 ; if ( isSplittable ( $ n , $ m ) ) echo " Yes " ; else echo " No " ;


"First and Last Three Bits | Function to print the first and last 3 bits equivalent decimal number ; Number formed from last three bits ; Let us get first three bits in n ; Number formed from first three bits ; Printing result ; Driver code"
Solution in php:


"First collision point of two series | PHP program to calculate the colliding point of two series ; Iterating through n terms of the first series ; x is i - th term of first series ; d is first element of second series and c is common difference for second series . ; If no term of first series is found ; Driver Code"
Solution in php:


"First digit in factorial of a number | PHP program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; Driver Code"
Solution in php:

< ? php function firstDigit ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fact = $ fact * $ i ; while ( $ fact % 10 == 0 ) $ fact = $ fact / 10 ; } while ( $ fact >= 10 ) $ fact = $ fact / 10 ; return floor ( $ fact ) ; } $ n = 5 ; echo firstDigit ( $ n ) ; ? >


"First digit in product of an array of numbers | PHP implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; Driver Code"
Solution in php:

 < ? php function firstDigit ( $ x ) { while ( $ x >= 10 ) $ x = $ x / 10 ; return floor ( $ x ) ; } echo firstDigit ( 12345 ) , "
 " ; echo firstDigit ( 5432 ) ; ? >


"First digit in product of an array of numbers | Returns the first digit of product of elements of arr [ ] ; stores the logarithm of product of elements of arr [ ] ; fractional ( s ) = s - floor ( s ) ; ans = 10 ^ fract_s ; Driver Code"
Solution in php:

< ? php function FirstDigit ( $ arr , $ n ) { $ S = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ S = $ S + log10 ( $ arr [ $ i ] * 1.0 ) ; $ fract_S = $ S - floor ( $ S ) ; $ ans = pow ( 10 , $ fract_S ) ; return floor ( $ ans ) ; } $ arr = array ( 5 , 8 , 3 , 7 ) ; $ n = sizeof ( $ arr ) ; echo FirstDigit ( $ arr , $ n ) ; ? >


"First occurrence of a digit in a given fraction | function to print the first digit ; reduce the number to its mod ; traverse for every decimal places ; get every fraction places when ( a * 10 / b ) / c ; check if it is equal to the required integer ; mod the number ; Driver Code"
Solution in php:


"First strictly greater element in a sorted array in Java | PHP program to find first element that is strictly greater than given target . ; Move to right side if target is greater . ; Move left side . ; Driver code"
Solution in php:

< ? php function next0 ( $ arr , $ target ) { $ start = 0 ; $ end = sizeof ( $ arr ) - 1 ; $ ans = -1 ; while ( $ start <= $ end ) { $ mid = ( int ) ( ( $ start + $ end ) / 2 ) ; if ( $ arr [ $ mid ] <= $ target ) { $ start = $ mid + 1 ; } else { $ ans = $ mid ; $ end = $ mid - 1 ; } } return $ ans ; } { $ arr = array ( 1 , 2 , 3 , 5 , 8 , 12 ) ; echo ( next0 ( $ arr , 8 ) ) ; } ? >


"First strictly smaller element in a sorted array in Java | PHP program to find first element that is strictly smaller than given target . ; Minimum size of the array should be 1 ; If target lies beyond the max element , than the index of strictly smaller value than target should be ( end - 1 ) ; Move to the left side if the target is smaller ; Move right side ; Driver code"
Solution in php:

< ? php function next0 ( $ arr , $ target ) { $ start = 0 ; $ end = sizeof ( $ arr ) - 1 ; if ( $ end == 0 ) return -1 ; if ( $ target > $ arr [ $ end ] ) return $ end ; $ ans = -1 ; while ( $ start <= $ end ) { $ mid = ( int ) ( ( $ start + $ end ) / 2 ) ; if ( $ arr [ $ mid ] >= $ target ) { $ end = $ mid - 1 ; } else { $ ans = $ mid ; $ start = $ mid + 1 ; } } return $ ans ; } { $ arr = array ( 1 , 2 , 3 , 5 , 8 , 12 ) ; echo ( next0 ( $ arr , 5 ) ) ; }


"First string from the given array whose reverse is also present in the same array | Function that returns true if s1 is equal to reverse of s2 ; If both the strings differ in length ; In case of any character mismatch ; Function to return the first word whose reverse is also present in the array ; Check every string ; Pair with every other string appearing after the current string ; If first string is equal to the reverse of the second string ; No such string exists ; Driver code"
Solution in php:

< ? php function isReverseEqual ( $ s1 , $ s2 ) { if ( strlen ( $ s1 ) != strlen ( $ s2 ) ) return false ; $ len = strlen ( $ s1 ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) if ( $ s1 [ $ i ] != $ s2 [ $ len - $ i - 1 ] ) return false ; return true ; } function getWord ( $ str , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( isReverseEqual ( $ str [ $ i ] , $ str [ $ j ] ) ) return $ str [ $ i ] ; return " - 1" ; } $ str = array ( " geeks " , " for " , " skeeg " ) ; $ n = count ( $ str ) ; print ( getWord ( $ str , $ n ) ) ; ? >


"First uppercase letter in a string ( Iterative and Recursive ) | Function to find string which has first character of each word . ; Driver code"
Solution in php:

 < ? php function first ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ctype_upper ( $ str [ $ i ] ) ) { return $ str [ $ i ] ; } return 0 ; } $ str = " geeksforGeeKS " ; $ res = first ( $ str ) ; if ( ord ( $ res ) == ord ( 0 ) ) echo " No ▁ uppercase ▁ letter " ; else echo $ res . "
 " ; ? >


"For every set bit of a number toggle bits of other | function for the Nega_bit ; Driver code"
Solution in php:

 < ? php function toggleBits ( $ n1 , $ n2 ) { return $ n1 ^ $ n2 ; } $ n1 = 2 ; $ n2 = 5 ; echo toggleBits ( $ n1 , $ n2 ) . "
 " ; ? >


"Form N | Returns the minimum cost to form a n - copy string . Here , x -> Cost to add / remove a single character ' G ' and y -> cost to append the string to itself ; Base Case : to form a 1 - copy string we need to perform an operation of type 1 ( i . e Add ) ; Case1 . Perform a Add operation on ( i - 1 ) - copy string , Case2 . Perform a type 2 operation on ( ( i + 1 ) / 2 ) - copy string ; Case1 . Perform a Add operation on ( i - 1 ) - copy string , Case2 . Perform a type 3 operation on ( i / 2 ) - copy string ; Driver Code"
Solution in php:


"Form a number using corner digits of powers | Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code"
Solution in php:

< ? php function nextPower ( $ N , & $ power ) { $ carry = 0 ; for ( $ i = 0 ; $ i < count ( $ power ) ; $ i ++ ) { $ prod = ( $ power [ $ i ] * $ N ) + $ carry ; $ power [ $ i ] = $ prod % 10 ; $ carry = ( int ) ( $ prod / 10 ) ; } while ( $ carry ) { array_push ( $ power , $ carry % 10 ) ; $ carry = ( int ) ( $ carry / 10 ) ; } } function printPowerNumber ( $ X , $ N ) { $ power = array ( ) ; array_push ( $ power , 1 ) ; $ res = array ( ) ; for ( $ i = 1 ; $ i <= $ X ; $ i ++ ) { nextPower ( $ N , $ power ) ; array_push ( $ res , $ power [ count ( $ power ) - 1 ] ) ; array_push ( $ res , $ power [ 0 ] ) ; } for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo ( $ res [ $ i ] ) ; } $ N = 19 ; $ X = 4 ; printPowerNumber ( $ X , $ N ) ; ? >


"Form coils in a matrix | Print coils in a matrix of size 4 n x 4 n ; Number of elements in each coil ; Let us fill elements in coil 1. ; First element of coil1 4 * n * 2 * n + 2 * n ; ; Fill remaining m - 1 elements in coil1 [ ] ; Fill elements of current step from down to up ; Next element from current element ; Fill elements of current step from up to down . ; get coil2 from coil1 ; Print both coils ; Driver code"
Solution in php:

< ? php function printCoils ( $ n ) { $ m = 8 * $ n * $ n ; $ coil1 = array ( ) ; $ coil1 [ 0 ] = 8 * $ n * $ n + 2 * $ n ; $ curr = $ coil1 [ 0 ] ; $ nflg = 1 ; $ step = 2 ; $ index = 1 ; while ( $ index < $ m ) { for ( $ i = 0 ; $ i < $ step ; $ i ++ ) { $ curr = $ coil1 [ $ index ++ ] = ( $ curr - 4 * $ n * $ nflg ) ; if ( $ index >= $ m ) break ; } if ( $ index >= $ m ) break ; for ( $ i = 0 ; $ i < $ step ; $ i ++ ) { $ curr = $ coil1 [ $ index ++ ] = $ curr + $ nflg ; if ( $ index >= $ m ) break ; } $ nflg = $ nflg * ( -1 ) ; $ step += 2 ; } $ coil2 = array ( ) ; for ( $ i = 0 ; $ i < 8 * $ n * $ n ; $ i ++ ) $ coil2 [ $ i ] = 16 * $ n * $ n + 1 - $ coil1 [ $ i ] ; echo " Coil ▁ 1 ▁ : ▁ " ; for ( $ i = 0 ; $ i < 8 * $ n * $ n ; $ i ++ ) echo $ coil1 [ $ i ] , " ▁ " ; echo " Coil 2 : " ; for ( $ i = 0 ; $ i < 8 * $ n * $ n ; $ i ++ ) echo $ coil2 [ $ i ] , " ▁ " ; } $ n = 1 ; printCoils ( $ n ) ; ? >


"Form the largest number using at most one swap operation | function to form the largest number by applying atmost one swap operation ; for the rightmost digit , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; else ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and break ; performing the required swap operation ; required largest number ; Driver Code"
Solution in php:

< ? php function largestNumber ( $ num ) { $ n = strlen ( $ num ) ; $ rightMax [ $ n ] = array ( 0 ) ; $ right ; $ rightMax [ $ n - 1 ] = -1 ; $ right = $ n - 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ num [ $ i ] < $ num [ $ right ] ) $ rightMax [ $ i ] = $ right ; else { $ rightMax [ $ i ] = -1 ; $ right = $ i ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ rightMax [ $ i ] != -1 ) { list ( $ num [ $ i ] , $ num [ $ rightMax [ $ i ] ] ) = array ( $ num [ $ rightMax [ $ i ] ] , $ num [ $ i ] ) ; break ; } } return $ num ; } $ num = "8725634" ; echo " Largest ▁ number : ▁ " , largestNumber ( $ num ) ; ? >


"Forming smallest array with given constraints | Return the size of smallest array with given constraint . ; Driver Code"
Solution in php:

< ? php function minimumLength ( $ x , $ y , $ z ) { return 1 + abs ( $ x - $ y ) + abs ( $ y - $ z ) ; } $ x = 3 ; $ y = 1 ; $ z = 2 ; echo minimumLength ( $ x , $ y , $ z ) ; ? >


"Fraction | Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's  simplest form ; Driver Code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function lowest ( & $ den3 , & $ num3 ) { $ common_factor = gcd ( $ num3 , $ den3 ) ; $ den3 = ( int ) $ den3 / $ common_factor ; $ num3 = ( int ) $ num3 / $ common_factor ; } function addFraction ( $ num1 , $ den1 , $ num2 , $ den2 , & $ num3 , & $ den3 ) { $ den3 = gcd ( $ den1 , $ den2 ) ; $ den3 = ( $ den1 * $ den2 ) / $ den3 ; $ num3 = ( $ num1 ) * ( $ den3 / $ den1 ) + ( $ num2 ) * ( $ den3 / $ den2 ) ; lowest ( $ den3 , $ num3 ) ; } $ num1 = 1 ; $ den1 = 500 ; $ num2 = 2 ; $ den2 = 1500 ; $ den3 ; $ num3 ; addFraction ( $ num1 , $ den1 , $ num2 , $ den2 , $ num3 , $ den3 ) ; echo $ num1 , " / " , $ den1 , " ▁ + ▁ " , $ num2 , " / " , $ den2 , " ▁ is ▁ equal ▁ to ▁ " , $ num3 , " / " , $ den3 , "
 " ; ? >


"Freivaldâ €™ s Algorithm to check if a matrix is product of two | PHP code to implement FreivaldaTMs Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now comput B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code"
Solution in php:


"Frequencies of even and odd numbers in a matrix | PHP Program to Find the frequency of even and odd numbers in a matrix ; function for calculating frequency ; modulo by 2 to check even and odd ; print Frequency of numbers ; Driver code"
Solution in php:

 < ? php $ MAX = 100 ; function freq ( $ ar , $ m , $ n ) { $ even = 0 ; $ odd = 0 ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) { for ( $ j = 0 ; $ j < $ n ; ++ $ j ) { if ( ( $ ar [ $ i ] [ $ j ] % 2 ) == 0 ) ++ $ even ; else ++ $ odd ; } } echo " ▁ Frequency ▁ of ▁ odd ▁ number ▁ = ▁ " , $ odd , "
 " ; echo " ▁ Frequency ▁ of ▁ even ▁ number ▁ = ▁ " , $ even ; } $ m = 3 ; $ n = 3 ; $ array = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; freq ( $ array , $ m , $ n ) ; ? >


"Frequency of a substring in a string | Simple PHP program to count occurrences of pat in txt . ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; if pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Driver Code"
Solution in php:

< ? php function countFreq ( $ pat , $ txt ) { $ M = strlen ( $ pat ) ; $ N = strlen ( $ txt ) ; $ res = 0 ; for ( $ i = 0 ; $ i <= $ N - $ M ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) if ( $ txt [ $ i + $ j ] != $ pat [ $ j ] ) break ; if ( $ j == $ M ) { $ res ++ ; $ j = 0 ; } } return $ res ; } $ txt = " dhimanman " ; $ pat = " man " ; echo countFreq ( $ pat , $ txt ) ;


"Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver Code"
Solution in php:

< ? php function countFriendsPairings ( $ n ) { $ dp = array_fill ( 0 , 1000 , -1 ) ; if ( $ dp [ $ n ] != -1 ) return $ dp [ $ n ] ; if ( $ n > 2 ) { $ dp [ $ n ] = countFriendsPairings ( $ n - 1 ) + ( $ n - 1 ) * countFriendsPairings ( $ n - 2 ) ; return $ dp [ $ n ] ; } else { $ dp [ $ n ] = $ n ; return $ dp [ $ n ] ; } } $ n = 4 ; echo countFriendsPairings ( $ n ) ? >


"Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code"
Solution in php:

< ? php function countFriendsPairings ( $ n ) { $ a = 1 ; $ b = 2 ; $ c = 0 ; if ( $ n <= 2 ) { return $ n ; } for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ c = $ b + ( $ i - 1 ) * $ a ; $ a = $ b ; $ b = $ c ; } return $ c ; } $ n = 4 ; print ( countFriendsPairings ( $ n ) ) ; ? >


"Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver code"
Solution in php:

< ? php function countFriendsPairings ( $ n ) { $ dp [ $ n + 1 ] = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { if ( $ i <= 2 ) $ dp [ $ i ] = $ i ; else $ dp [ $ i ] = $ dp [ $ i - 1 ] + ( $ i - 1 ) * $ dp [ $ i - 2 ] ; } return $ dp [ $ n ] ; } $ n = 4 ; echo countFriendsPairings ( $ n ) ; ? >


"Frobenius coin problem | Utility function to find gcd ; Function to print the desired output ; Solution doesn 't exist   if GCD is not 1 ; Else apply the formula ; Driver Code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { $ c ; while ( $ a != 0 ) { $ c = $ a ; $ a = $ b % $ a ; $ b = $ c ; } return $ b ; } function forbenius ( $ X , $ Y ) { if ( gcd ( $ X , $ Y ) != 1 ) { echo " NA
 " ; return ; } $ A = ( $ X * $ Y ) - ( $ X + $ Y ) ; $ N = ( $ X - 1 ) * ( $ Y - 1 ) / 2 ; echo " Largest Amount = " , ▁ $ A , ▁ " " ;
 	 echo ▁ " Total Count = " , ▁ $ N , ▁ " " } $ X = 2 ; $ Y = 5 ; forbenius ( $ X , $ Y ) ; $ X = 5 ; $ Y = 10 ; echo "
 " ; forbenius ( $ X , $ Y ) ; ? >


"Front and Back Search in unsorted array | PHP program to implement front and back search ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver Code"
Solution in php:

< ? php function search ( $ arr , $ n , $ x ) { $ front = 0 ; $ back = $ n - 1 ; while ( $ front <= $ back ) { if ( $ arr [ $ front ] == $ x $ arr [ $ back ] == $ x ) return true ; $ front ++ ; $ back -- ; } return false ; } $ arr = array ( 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ) ; $ x = 130 ; $ n = sizeof ( $ arr ) ; if ( search ( $ arr , $ n , $ x ) ) echo " Yes " ; else echo " No " ; ? >


"Frugal Number | Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Returns number of digits in n ; Checking whether a number is Frugal or not ; Finding number of digits in prime factorization of the number ; Exponent for current factor ; Counting number of times this prime factor divides ( Finding exponent ) ; Finding number of digits in the exponent Avoiding exponents of value 1 ; Checking condition for frugal number ; Driver Code"
Solution in php:

 < ? php function primes ( $ n ) { $ prime = array ( ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) $ prime [ $ i ] = true ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] == true ) { for ( $ j = $ i * 2 ; $ j <= $ n ; $ j += $ i ) $ prime [ $ j ] = false ; } } $ arr = array ( ) ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ prime [ $ i ] ) array_push ( $ arr , $ i ) ; return $ arr ; } function countDigits ( $ n ) { $ temp = $ n ; $ c = 0 ; while ( $ temp != 0 ) { $ temp = intval ( $ temp / 10 ) ; $ c ++ ; } return $ c ; } function frugal ( $ n ) { $ r = primes ( $ n ) ; $ t = $ n ; $ s = 0 ; for ( $ i = 0 ; $ i < count ( $ r ) ; $ i ++ ) { if ( $ t % $ r [ $ i ] == 0 ) { $ k = 0 ; while ( $ t % $ r [ $ i ] == 0 ) { $ t = intval ( $ t / $ r [ $ i ] ) ; $ k ++ ; } if ( $ k == 1 ) $ s = $ s + countDigits ( $ r [ $ i ] ) ; else if ( $ k != 1 ) $ s = $ s + countDigits ( $ r [ $ i ] ) + countDigits ( $ k ) ; } } return ( countDigits ( $ n ) > $ s && $ s != 0 ) ; } $ n = 343 ; if ( frugal ( $ n ) ) echo ( " A ▁ Frugal ▁ number
 " ) ; else echo ( " Not ▁ a ▁ frugal ▁ number
 " ) ; ? >


"GCD of a number raised to some power and another number | Calculates modular exponentiation , i . e . , ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Returns GCD of a ^ n and b ; Driver code"
Solution in php:

< ? php function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function powerGCD ( $ a , $ b , $ n ) { $ e = power ( $ a , $ n , $ b ) ; return gcd ( $ e , $ b ) ; } $ a = 5 ; $ b = 4 ; $ n = 2 ; echo powerGCD ( $ a , $ b , $ n ) ; ? >


"GCD of a number raised to some power and another number | PHP program to find GCD of a ^ n and b ; Returns GCD of a ^ n and b . ; Driver code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function powGCD ( $ a , $ n , $ b ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ a = $ a * $ a ; return gcd ( $ a , $ b ) ; } $ a = 10 ; $ b = 5 ; $ n = 2 ; echo powGCD ( $ a , $ n , $ b ) ; ? >


"GCD of factorials of two numbers | PHP program to find GCD of factorial of two numbers . ; Driver Code"
Solution in php:

< ? php function factorial ( $ x ) { if ( $ x <= 1 ) return 1 ; $ res = 2 ; for ( $ i = 3 ; $ i <= $ x ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function gcdOfFactorial ( $ m , $ n ) { return factorial ( min ( $ m , $ n ) ) ; } $ m = 5 ; $ n = 9 ; echo gcdOfFactorial ( $ m , $ n ) ; ? >


"GCD of more than two ( or array ) numbers | Function to return gcd of a and b ; Function to find gcd of array of numbers ; Driver code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findGCD ( $ arr , $ n ) { $ result = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ result = gcd ( $ arr [ $ i ] , $ result ) ; if ( $ result == 1 ) { return 1 ; } } return $ result ; } $ arr = array ( 2 , 4 , 6 , 8 , 16 ) ; $ n = sizeof ( $ arr ) ; echo ( findGCD ( $ arr , $ n ) ) ; ? >


"GCD of two numbers formed by n repeating x and y times | Return the Greatest common Divisor of two numbers . ; Prints Greatest Common Divisor of number formed by n repeating x times and y times . ; Finding GCD of x and y . ; Print n , g times . ; Driver Code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findgcd ( $ n , $ x , $ y ) { $ g = gcd ( $ x , $ y ) ; for ( $ i = 0 ; $ i < $ g ; $ i ++ ) echo ( $ n ) ; } $ n = 123 ; $ x = 5 ; $ y = 2 ; findgcd ( $ n , $ x , $ y ) ; ? >


"Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | Function that returns true if the game cannot be won ; Driver code ; Common length"
Solution in php:


"Game of Nim with removal of one stone allowed | Return true if player A wins , return false if player B wins . ; Checking the last bit of N . ; Driver Code"
Solution in php:

< ? php function findWinner ( $ N ) { return $ N & 1 ; } $ N = 15 ; if ( findWinner ( $ N ) ) echo " Player ▁ A " ; else echo " Player ▁ B " ; ? >


"Generate 0 and 1 with 25 % and 75 % probability | Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 1 with 75 % probability and 0 with 25 % probability using Bitwise OR ; Driver Code Initialize random number generator ; This code is contributed m_kit"
Solution in php:

< ? php function rand50 ( ) { return rand ( ) & 1 ; } function rand75 ( ) { return rand50 ( ) | rand50 ( ) ; } srand ( time ( NULL ) ) ; for ( $ i = 0 ; $ i < 50 ; $ i ++ ) echo rand75 ( ) ; ? >


"Generate a list of n consecutive composite numbers ( An interesting method ) | function to find factorial of given number ; Prints n consecutive numbers . ; Driver Code"
Solution in php:

< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res *= $ i ; return $ res ; } function printNComposite ( int $ n ) { $ fact = factorial ( $ n + 1 ) ; for ( $ i = 2 ; $ i <= $ n + 1 ; ++ $ i ) echo $ fact + $ i , " ▁ " ; } $ n = 4 ; printNComposite ( $ n ) ; ? >


"Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation"
Solution in php:

< ? php $ permutation = array ( ) ; function printPermutation ( ) { global $ permutation ; foreach ( $ permutation as $ i ) echo $ i . " " ; } function give_random_number ( $ l , $ r ) { $ x = rand ( ) % ( $ r - $ l + 1 ) + $ l ; return $ x ; } function generate_random_permutation ( $ l , $ r ) { global $ permutation ; if ( $ l > $ r ) return ; $ n = give_random_number ( $ l , $ r ) ; array_push ( $ permutation , $ n ) ; generate_random_permutation ( $ l , $ n - 1 ) ; generate_random_permutation ( $ n + 1 , $ r ) ; } $ l = 5 ; $ r = 15 ; generate_random_permutation ( $ l , $ r ) ; printPermutation ( ) ; ? >


"Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code"
Solution in php:


"Generate all binary strings from given pattern | Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; No need to backtrack as string is passed by value to the function ; Driver code"
Solution in php:

 < ? php function print1 ( $ str , $ index ) { if ( $ index == strlen ( $ str ) ) { echo $ str . "
 " ; return ; } if ( $ str [ $ index ] == ' ? ' ) { $ str [ $ index ] = '0' ; print1 ( $ str , $ index + 1 ) ; $ str [ $ index ] = '1' ; print1 ( $ str , $ index + 1 ) ; } else print1 ( $ str , $ index + 1 ) ; } $ str = "1 ? ? 0?101" ; print1 ( $ str , 0 ) ; ? >


"Generate all cyclic permutations of a number | Function to count the total number of digits in a number . ; Function to generate all cyclic permutations of a number ; Following three lines generates a circular pirmutation of a number . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Code"
Solution in php:

 < ? php function countdigits ( $ N ) { $ count = 0 ; while ( $ N ) { $ count ++ ; $ N = floor ( $ N / 10 ) ; } return $ count ; } function cyclic ( $ N ) { $ num = $ N ; $ n = countdigits ( $ N ) ; while ( 1 ) { echo ( $ num ) ; echo "
 " ; $ rem = $ num % 10 ; $ div = floor ( $ num / 10 ) ; $ num = ( pow ( 10 , $ n - 1 ) ) * $ rem + $ div ; if ( $ num == $ N ) break ; } } $ N = 5674 ; cyclic ( $ N ) ; ? >


"Generate all possible sorted arrays from alternate elements of two given sorted arrays | Function to generates and prints all sorted arrays from alternate elements of ' A [ i . . m - 1 ] ' and ' B [ j . . n - 1 ] ' If ' flag ' is true , then current element is to be included from A otherwise from B . ' len ' is the index in output array C [ ] . We print output array each time before including a character from A only if length of output array is greater than 0. We try than all possible combinations ; Include valid element from A ; Print output if there is at least one ' B ' in output array ' C ' ; Recur for all elements of A after current index ; this block works for the very first call to include the first element in the output array ; don 't increment lem as B  is included yet ; include valid element from A and recur ; Include valid element from B and recur ; Wrapper function ; output array ; A utility function to print an array ; Driver Code"
Solution in php:


"Generate all rotations of a given string | Print all the rotated string . ; Concatenate str with itself ; Print all substrings of size n . Note that size of temp is 2 n ; Driver code"
Solution in php:

 < ? php function printRotatedString ( $ str ) { $ n = strlen ( $ str ) ; $ temp = $ str . $ str ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j != $ n ; $ j ++ ) print ( $ temp [ $ i + $ j ] ) ; print ( "
 " ) ; } } $ str = " geeks " ; printRotatedString ( $ str ) ; ? >


"Generate all rotations of a given string | Print all the rotated string . ; Generate all rotations one by one and print ; Current index in str ; Current index in temp ; Copying the second part from the point of rotation . ; Copying the first part from the point of rotation . ; Driver Code"
Solution in php:

 < ? php function printRotatedString ( $ str ) { $ len = strlen ( $ str ) ; $ temp = " ▁ " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ j = $ i ; $ k = 0 ; while ( $ j < $ len ) { $ temp [ $ k ] = $ str [ $ j ] ; $ k ++ ; $ j ++ ; } $ j = 0 ; while ( $ j < $ i ) { $ temp [ $ k ] = $ str [ $ j ] ; $ j ++ ; $ k ++ ; } echo $ temp . "
 " ; } } $ str = " geeks " ; printRotatedString ( $ str ) ; ? >


"Generate all rotations of a number | Function to return the count of digits of n ; Function to print the left shift numbers ; Formula to calculate left shift from previous number ; Update the original number ; Driver Code"
Solution in php:

< ? php function numberOfDigits ( $ n ) { $ cnt = 0 ; while ( $ n > 0 ) { $ cnt ++ ; $ n = floor ( $ n / 10 ) ; } return $ cnt ; } function cal ( $ num ) { $ digits = numberOfDigits ( $ num ) ; $ powTen = pow ( 10 , $ digits - 1 ) ; for ( $ i = 0 ; $ i < $ digits - 1 ; $ i ++ ) { $ firstDigit = floor ( $ num / $ powTen ) ; $ left = ( ( $ num * 10 ) + $ firstDigit ) - ( $ firstDigit * $ powTen * 10 ) ; echo $ left , " " ; $ num = $ left ; } } $ num = 1445 ; cal ( $ num ) ; ? >


"Generate all the binary strings of N bits | Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings"
Solution in php:

 < ? php function printTheArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ arr [ $ i ] , " " ; } echo "
 " ; } function generateAllBinaryStrings ( $ n , $ arr , $ i ) { if ( $ i == $ n ) { printTheArray ( $ arr , $ n ) ; return ; } $ arr [ $ i ] = 0 ; generateAllBinaryStrings ( $ n , $ arr , $ i + 1 ) ; $ arr [ $ i ] = 1 ; generateAllBinaryStrings ( $ n , $ arr , $ i + 1 ) ; } $ n = 4 ; $ arr = array_fill ( 0 , $ n , 0 ) ; generateAllBinaryStrings ( $ n , $ arr , 0 ) ; ? >


"Generate an Array in which count of even and odd sum sub | Function to generate and print the required array ; Find the number of odd prefix sums ; If no odd prefix sum found ; Calculating the number of even prefix sums ; Stores the current prefix sum ; If current prefix sum is even ; Print 0 until e = EvenPreSums - 1 ; Print 1 when e = EvenPreSums ; Print 0 for rest of the values ; Driver code"
Solution in php:

 < ? php function CreateArray ( $ N , $ even , $ odd ) { $ temp = -1 ; $ OddPreSums = 0 ; for ( $ i = 0 ; $ i <= $ N + 1 ; $ i ++ ) { if ( $ i * ( ( $ N + 1 ) - $ i ) == $ odd ) { $ temp = 0 ; $ OddPreSums = $ i ; break ; } } if ( $ temp == -1 ) { echo temp ; } else { $ EvenPreSums = ( $ N + 1 ) - $ OddPreSums ; $ e = 1 ; $ o = 0 ; $ CurrSum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ CurrSum % 2 == 0 ) { if ( $ e < $ EvenPreSums ) { $ e ++ ; echo "0 ▁ " ; } else { $ o ++ ; echo "1 ▁ " ; $ CurrSum ++ ; } } else { if ( $ e < $ EvenPreSums ) { $ e ++ ; echo "1 ▁ " ; $ CurrSum ++ ; } else { $ o ++ ; echo "0 ▁ " ; } } } echo "
 " ; } } $ N = 15 ; $ even = 60 ; $ odd = 60 ; CreateArray ( $ N , $ even , $ odd ) ; ? >


"Generate an array of K elements such that sum of elements is N and the condition a [ i ] < a [ i + 1 ] <= 2 * a [ i ] is met | Set 2 | Function that print the desired array which satisfies the given conditions ; If the lowest filling condition is void , then it is not possible to generate the required array ; Increase all the elements by cnt ; Start filling from the back till the number is a [ i + 1 ] <= 2 * a [ i ] ; Get the number to be filled ; If it is less than the remaining numbers to be filled ; less than remaining numbers to be filled ; Get the sum of the array ; If this condition is void at any stage during filling up , then print - 1 ; Else add it to the sum ; If the sum condition is not satisified , then print - 1 ; Print the generated array ; Driver code"
Solution in php:


"Generate array with minimum sum which can be deleted in P steps | Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code"
Solution in php:

 < ? php function findArray ( $ N , $ P ) { $ ans = ( $ P * ( $ P + 1 ) ) / 2 + ( $ N - $ P ) ; $ arr [ $ N + 1 ] = array ( ) ; for ( $ i = 1 ; $ i <= $ P ; $ i ++ ) $ arr [ $ i ] = $ i ; for ( $ i = $ P + 1 ; $ i <= $ N ; $ i ++ ) $ arr [ $ i ] = 1 ; echo " The ▁ Minimum ▁ Possible ▁ Sum ▁ is : ▁ " , $ ans , "
 " ; echo " The ▁ Array ▁ Elements ▁ are : ▁
 " ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo $ arr [ $ i ] , ' ▁ ' ; } $ N = 5 ; $ P = 3 ; findArray ( $ N , $ P ) ; ? >


"Generate k digit numbers with digits in strictly increasing order | number -- > Current value of number . x -- > Current digit to be considered k -- > Remaining number of digits ; Try all possible greater digits ; Generates all well ordered numbers of length k ; Driver code"
Solution in php:

< ? php function printWellOrdered ( $ number , $ x , $ k ) { if ( $ k == 0 ) { echo $ number , " " ; return ; } for ( $ i = ( $ x + 1 ) ; $ i < 10 ; $ i ++ ) printWellOrdered ( $ number * 10 + $ i , $ i , $ k - 1 ) ; } function generateWellOrdered ( $ k ) { printWellOrdered ( 0 , 0 , $ k ) ; } $ k = 3 ; generateWellOrdered ( $ k ) ; ? >


"Generate lexicographically smallest string of 0 , 1 and 2 with adjacent swaps allowed | Function to print the required string ; count number of 1 s ; To check if the all the 1 s have been used or not ; Print all the 1 s if any 2 is encountered ; If str [ i ] = 0 or str [ i ] = 2 ; If 1 s are not printed yet ; Driver code"
Solution in php:


"Generate minimum sum sequence of integers with even elements greater | Function to print the required sequence ; arr [ ] will hold the sequence sum variable will store the sum of the sequence ; If sum of the sequence is odd ; Print the sequence ; Driver Code"
Solution in php:


"Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers ; To store the permutation ; For sequence 1 2 3. . . ; For sequence N , N - 1 , N - 2. . . ; Flag is used to alternate between the above if else statements ; If last element added was r + 1 ; If last element added was l - 1 ; Print the permutation ; Driver code"
Solution in php:

< ? php function printPermutation ( $ N , $ K ) { $ res = array ( ) ; $ l = 1 ; $ r = $ N ; $ flag = 0 ; for ( $ i = 0 ; $ i < $ K ; $ i ++ ) { if ( ! $ flag ) { array_push ( $ res , $ l ) ; $ l ++ ; } else { array_push ( $ res , $ r ) ; $ r -- ; } $ flag ^= 1 ; } if ( ! $ flag ) { for ( $ i = $ r ; $ i >= $ l ; $ i -- ) array_push ( $ res , $ i ) ; } else { for ( $ i = l ; $ i <= $ r ; $ i ++ ) array_push ( $ res , $ i ) ; } for ( $ i = 0 ; $ i < sizeof ( $ res ) ; $ i ++ ) echo $ res [ $ i ] , " ▁ " ; } $ N = 10 ; $ K = 4 ; printPermutation ( $ N , $ K ) ; ? >


"Generate permutations with only adjacent swaps allowed | PHP program to generate permutations with only one swap allowed . ; don 't swap the current position ; Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character . ; Driver code"
Solution in php:

 < ? php function findPermutations ( $ str , $ index , $ n ) { if ( $ index >= $ n || ( $ index + 1 ) >= $ n ) { echo $ str , "
 " ; return ; } findPermutations ( $ str , $ index + 1 , $ n ) ; list ( $ str [ $ index ] , $ str [ $ index + 1 ] ) = array ( $ str [ $ index + 1 ] , $ str [ $ index ] ) ; findPermutations ( $ str , $ index + 2 , $ n ) ; list ( $ str [ $ index ] , $ str [ $ index + 1 ] ) = array ( $ str [ $ index + 1 ] , $ str [ $ index ] ) ; } $ str = "12345" ; $ n = strlen ( $ str ) ; findPermutations ( $ str , 0 , $ n ) ; ? >


"Generating numbers that are divisor of their right | Function to check if N is a divisor of its right - rotation ; Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code"
Solution in php:

 < ? php function rightRotationDivisor ( $ N ) { $ lastDigit = $ N % 10 ; $ rightRotation = ( $ lastDigit * pow ( 10 , ( int ) ( log10 ( $ N ) ) ) ) + floor ( $ N / 10 ) ; return ( $ rightRotation % $ N == 0 ) ; } function generateNumbers ( $ m ) { for ( $ i = pow ( 10 , ( $ m - 1 ) ) ; $ i < pow ( 10 , $ m ) ; $ i ++ ) if ( rightRotationDivisor ( $ i ) ) echo $ i . "
 " ; } $ m = 3 ; generateNumbers ( $ m ) ; ? >


"Generation of n numbers with given set of factors | Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers $output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; Driver code"
Solution in php:

< ? php function generateNumbers ( $ factor , $ n , $ k ) { $ next = array_fill ( 0 , $ k , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ) { $ toincrement = 0 ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) if ( $ next [ $ j ] < $ next [ $ toincrement ] ) $ toincrement = $ j ; if ( $ output != $ next [ $ toincrement ] ) { $ output = $ next [ $ toincrement ] ; echo $ next [ $ toincrement ] . " " ; $ i ++ ; } $ next [ $ toincrement ] += $ factor [ $ toincrement ] ; } } $ factor = array ( 3 , 5 , 7 ) ; $ n = 10 ; $ k = count ( $ factor ) ; generateNumbers ( $ factor , $ n , $ k ) ; ? >


"Geometric mean ( Two Methods ) | function to calculate geometric mean and return float value . ; declare product variable and initialize it to 1. ; Compute the product of all the elements in the array . ; compute geometric mean through formula pow ( product , 1 / n ) and return the value to main function . ; Driver Code"
Solution in php:

< ? php function geometricMean ( $ arr , $ n ) { $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ product = $ product * $ arr [ $ i ] ; $ gm = pow ( $ product , ( float ) ( 1 / $ n ) ) ; return $ gm ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ; $ n = sizeof ( $ arr ) ; echo ( geometricMean ( $ arr , $ n ) ) ; ? >


"Geometric mean ( Two Methods ) | function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value ; Driver Code ; function call"
Solution in php:

< ? php function geometricMean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + log ( $ arr [ $ i ] ) ; $ sum = $ sum / $ n ; return exp ( $ sum ) ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ; $ n = count ( $ arr ) ; echo geometricMean ( $ arr , $ n ) ; ? >


"Get maximum items when other items of total cost of an item are free | Function to count the total number of items ; Sort the prices ; Choose the last element ; Initial count of item ; Sum to keep track of the total price of free items ; If total is less than or equal to z then we will add 1 to the answer ; Driver code"
Solution in php:


"Given 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . . . k ' s print them in zig zag way . | function that prints given number of 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; two - dimensional array to store numbers . ; for even row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index ; for odd row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index . ; printing the stored elements . ; Driver Code"
Solution in php:

 < ? php function ZigZag ( $ rows , $ columns , $ numbers ) { $ k = 0 ; $ arr = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ rows ; $ i ++ ) { if ( $ i % 2 == 0 ) { for ( $ j = 0 ; $ j < $ columns and $ numbers [ $ k ] > 0 ; $ j ++ ) { $ arr [ $ i ] [ $ j ] = $ k + 1 ; $ numbers [ $ k ] -- ; if ( $ numbers [ $ k ] == 0 ) $ k ++ ; } } else { for ( $ j = $ columns - 1 ; $ j >= 0 and $ numbers [ $ k ] > 0 ; $ j -- ) { $ arr [ $ i ] [ $ j ] = $ k + 1 ; $ numbers [ $ k ] -- ; if ( $ numbers [ $ k ] == 0 ) $ k ++ ; } } } for ( $ i = 0 ; $ i < $ rows ; $ i ++ ) { for ( $ j = 0 ; $ j < $ columns ; $ j ++ ) echo $ arr [ $ i ] [ $ j ] , " ▁ " ; echo "
 " ; } } $ rows = 4 ; $ columns = 5 ; $ Numbers = array ( 3 , 4 , 2 , 2 , 3 , 1 , 5 ) ; ZigZag ( $ rows , $ columns , $ Numbers ) ; ? >


"Given N and Standard Deviation , find N elements | function to print series of n elements ; if S . D . is 0 then print all elements as 0. ; print n 0 's ; if S . D . is even ; print - SD , + SD , - SD , + SD ; if odd ; convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; Driver Code"
Solution in php:


"Given a Boolean Matrix , find k such that all elements in k ' th ▁ row ▁ are ▁ 0 ▁ and ▁ k ' th column are 1. | PHP program to find i such that all entries in i ' th ▁ row ▁ are ▁ 0 ▁ and ▁ all ▁ entries ▁ in ▁ i ' th column are 1 ; Start from top - most rightmost corner ( We could start from other corners also ) ; Initialize result ; Find the index ( This loop runs at most 2 n times , we either increment row number or decrement column number ) ; If current element is 0 , then this row may be a solution ; Check for all elements in this row ; If all values are 0 , then store this row as result ; We reach here if we found a 1 in current row , so this row cannot be a solution , increment row number ; If current element is 1 ; Check for all elements in this column ; If all elements are 1 ; We reach here if we found a 0 in current column , so this column cannot be a solution , increment column number ; If we could not find result in above loop , then result doesn 't exist ; Check if above computed res is valid ; Driver Code"
Solution in php:


"Given a binary string , count number of substrings that start and end with 1. | A simple PHP program to count number of substrings starting and ending with 1 ; Initialize result ; Pick a starting point ; Search for all possible ending point ; Driver Code"
Solution in php:

< ? php function countSubStr ( $ str ) { $ res = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == '1' ) { for ( $ j = $ i + 1 ; $ j < strlen ( $ str ) ; $ j ++ ) if ( $ str [ $ j ] == '1' ) $ res ++ ; } } return $ res ; } $ str = "00100101" ; echo countSubStr ( $ str ) ; ? >


"Given a binary string , count number of substrings that start and end with 1. | A simple PHP program to count number of substrings starting and ending with 1 ; Pick a starting point ; Return count of possible pairs among m 1 's ; Driver Code"
Solution in php:

< ? php function countSubStr ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == '1' ) { $ m ++ ; } } return $ m * ( $ m - 1 ) / 2 ; } $ str = "00100101" ; echo countSubStr ( $ str ) ; ? >


"Given a large number , check if a subsequence of digits is divisible by 8 | Function takes in an array of numbers , dynamically goes on the location and makes combination of numbers . ; Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 ) ; If we consider the number in our combination , we add it to the previous combination ; If we exclude the number from our combination ; If at dp [ i ] [ 0 ] , we find value 1 / true , it shows that the number exists at the value of ' i ' ; Driver Code"
Solution in php:


"Given a matrix of ‘ O ’ and ‘ X ’ , replace ' O ' with ' X ' if surrounded by ' X ' | Size of given matrix is M X N ; A recursive function to replace previous value ' prevV ' at ' ( x , ▁ y ) ' and all surrounding values of ( x , y ) with new value ' newV ' . ; Base cases ; Replace the color at ( x , y ) ; Recur for north , east , south and west ; Returns size of maximum size subsquare matrix surrounded by ' X ' ; Step 1 : Replace all ' O ' with ' - ' ; Call floodFill for all ' - ' lying on edges ; Right side ; Top side ; Bottom side ; Step 3 : Replace all ' - ' with ' X ' ; Driver Code"
Solution in php:

< ? php $ M = 6 ; $ N = 6 ; function floodFillUtil ( & $ mat , $ x , $ y , $ prevV , $ newV ) { if ( $ x < 0 $ x >= $ GLOBALS [ ' M ' ] $ y < 0 $ y >= $ GLOBALS [ ' N ' ] ) return ; if ( $ mat [ $ x ] [ $ y ] != $ prevV ) return ; $ mat [ $ x ] [ $ y ] = $ newV ; floodFillUtil ( $ mat , $ x + 1 , $ y , $ prevV , $ newV ) ; floodFillUtil ( $ mat , $ x - 1 , $ y , $ prevV , $ newV ) ; floodFillUtil ( $ mat , $ x , $ y + 1 , $ prevV , $ newV ) ; floodFillUtil ( $ mat , $ x , $ y - 1 , $ prevV , $ newV ) ; } function replaceSurrounded ( & $ mat ) { for ( $ i = 0 ; $ i < $ GLOBALS [ ' M ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] == ' O ' ) $ mat [ $ i ] [ $ j ] = ' - ' ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' M ' ] ; $ i ++ ) if ( $ mat [ $ i ] [ 0 ] == ' - ' ) floodFillUtil ( $ mat , $ i , 0 , ' - ' , ' O ' ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' M ' ] ; $ i ++ ) if ( $ mat [ $ i ] [ $ GLOBALS [ ' N ' ] - 1 ] == ' - ' ) floodFillUtil ( $ mat , $ i , $ GLOBALS [ ' N ' ] - 1 , ' - ' , ' O ' ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) if ( $ mat [ 0 ] [ $ i ] == ' - ' ) floodFillUtil ( $ mat , 0 , $ i , ' - ' , ' O ' ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) if ( $ mat [ $ GLOBALS [ ' M ' ] - 1 ] [ $ i ] == ' - ' ) floodFillUtil ( $ mat , $ GLOBALS [ ' M ' ] - 1 , $ i , ' - ' , ' O ' ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' M ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] == ' - ' ) $ mat [ $ i ] [ $ j ] = ' X ' ; } $ mat = array ( array ( ' X ' , ' O ' , ' X ' , ' O ' , ' X ' , ' X ' ) , array ( ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' ) , array ( ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' X ' ) , array ( ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' ) , array ( ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' ) , array ( ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' ) ) ; replaceSurrounded ( $ mat ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' M ' ] ; $ i ++ ) { for ( $ j = 0 ; $ j < $ GLOBALS [ '...


"Given a number N in decimal base , find number of its digits in any base ( base b ) | function to print number of digits ; Calculating log using base changing property and then taking it floor and then adding 1. ; printing output ; taking inputs ; calling the method"
Solution in php:

< ? php function findNumberOfDigits ( $ n , $ b ) { $ dig = ( int ) ( floor ( log ( $ n ) / log ( $ b ) ) + 1 ) ; echo ( " The ▁ Number ▁ of ▁ digits " . " ▁ of ▁ Number ▁ " . $ n . " ▁ in ▁ base ▁ " . $ b . " ▁ is ▁ " . $ dig ) ; } $ n = 1446 ; $ b = 7 ; findNumberOfDigits ( $ n , $ b ) ; ? >


"Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | PHP program to count substrings with recursive sum equal to 9 ; To store result ; Consider every character as beginning of substring ; sum of digits in current substring ; One by one choose every character as an ending character ; Add current digit to sum , if sum becomes multiple of 5 then increment count . Let us do modular arithmetic to avoid overflow for big strings ; Driver Code"
Solution in php:

 < ? php function count9s ( $ number ) { $ count = 0 ; $ n = strlen ( $ number ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ number [ $ i ] - '0' ; if ( $ number [ $ i ] == '9' ) $ count ++ ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ sum = ( $ sum + $ number [ $ j ] - '0' ) % 9 ; if ( $ sum == 0 ) $ count ++ ; } } return $ count ; } echo count9s ( " 4189 " ) , "
 " ; echo count9s ( " 1809 " ) ; ? >


"Given a number n , count all multiples of 3 and / or 5 in set { 1 , 2 , 3 , ... n } | PHP program to find count of multiples of 3 and 5 in { 1 , 2 , 3 , . . n } ; Add multiples of 3 and 5. Since common multiples are counted twice in n / 3 + n / 15 , subtract common multiples ; Driver Code"
Solution in php:

 < ? php function countOfMultiples ( $ n ) { return floor ( floor ( $ n / 3 ) + floor ( $ n / 5 ) - floor ( $ n / 15 ) ) ; } echo countOfMultiples ( 6 ) , "
 " ; echo countOfMultiples ( 16 ) ; ? >


"Given a number n , find the first k digits of n ^ n | function that manually calculates n ^ n and then removes digits until k digits remain ; loop will terminate when there are only k digits left ; Driver Code"
Solution in php:

< ? php function firstkdigits ( $ n , $ k ) { $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ product *= $ n ; while ( ( int ) ( $ product / pow ( 10 , $ k ) ) != 0 ) $ product = ( int ) $ product / 10 ; return floor ( $ product ) ; } $ n = 15 ; $ k = 4 ; echo firstkdigits ( $ n , $ k ) ; ? >


"Given a number n , find the first k digits of n ^ n | function to calculate first k digits of n ^ n ; take log10 of n ^ n . log10 ( n ^ n ) = n * log10 ( n ) ; We now try to separate the decimal and integral part of the / product . The floor function returns the smallest integer less than or equal to the argument . So in this case , product - floor ( product ) will give us the decimal part of product ; we now exponentiate this back by raising 10 to the power of decimal part ; We now try to find the power of 10 by which we will have to multiply the decimal part to obtain our final answer ; Driver Code"
Solution in php:

< ? php function firstkdigits ( $ n , $ k ) { $ product = $ n * log10 ( $ n ) ; $ decimal_part = $ product - floor ( $ product ) ; $ decimal_part = pow ( 10 , $ decimal_part ) ; $ digits = pow ( 10 , $ k - 1 ) ; $ i = 0 ; return floor ( $ decimal_part * $ digits ) ; } $ n = 1450 ; $ k = 6 ; echo firstkdigits ( $ n , $ k ) ; ? >


"Given a set , find XOR of the XOR 's of all subsets. | Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver Code"
Solution in php:

< ? php function findXOR ( $ Set , $ n ) { if ( $ n == 1 ) return $ Set [ 0 ] ; else return 0 ; } $ Set = array ( 1 , 2 , 3 ) ; $ n = count ( $ Set ) ; echo " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " , findXOR ( $ Set , $ n ) ; ? >


"Given a sorted and rotated array , find if there is a pair with a given sum | This function returns count of number of pairs with sum equals to x . ; Find the pivot element . Pivot element is largest element of array . ; l is index of smallest element . ; r is index of largest element . ; Variable to store count of number of pairs . ; Find sum of pair formed by arr [ l ] and arr [ r ] and update l , r and cnt accordingly . ; If we find a pair with sum x , then increment cnt , move l and r to next element . ; This condition is required to be checked , otherwise l and r will cross each other and loop will never terminate . ; If current pair sum is less , move to the higher sum side . ; If current pair sum is greater , move to the lower sum side . ; Driver Code"
Solution in php:


"Given a sorted and rotated array , find if there is a pair with a given sum | This function returns true if arr [ 0. . n - 1 ] has a pair with sum equals to x . ; Find the pivot element ; l is now index of smallest element ; r is now index of largest element ; Keep moving either l or r till they meet ; If we find a pair with sum x , we return true ; If current pair sum is less , move to the higher sum ; Move to the lower sum side ; Driver Code"
Solution in php:

< ? php function pairInSortedRotated ( $ arr , $ n , $ x ) { $ i ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] > $ arr [ $ i + 1 ] ) break ; $ l = ( $ i + 1 ) % $ n ; $ r = $ i ; while ( $ l != $ r ) { if ( $ arr [ $ l ] + $ arr [ $ r ] == $ x ) return true ; if ( $ arr [ $ l ] + $ arr [ $ r ] < $ x ) $ l = ( $ l + 1 ) % $ n ; else $ r = ( $ n + $ r - 1 ) % $ n ; } return false ; } $ arr = array ( 11 , 15 , 6 , 8 , 9 , 10 ) ; $ sum = 16 ; $ n = sizeof ( $ arr ) ; if ( pairInSortedRotated ( $ arr , $ n , $ sum ) ) echo " Array ▁ has ▁ two ▁ elements ▁ " . " with ▁ sum ▁ 16" ; else echo " Array ▁ doesn ' t ▁ have ▁ two ▁ " . " elements ▁ with ▁ sum ▁ 16 ▁ " ; ? >


"Given a sorted array and a number x , find the pair in array whose sum is closest to x | Prints the pair with sum closest to x ; To store indexes of result pair ; Initialize left and right indexes and difference between pair sum and x ; While there are elements between l and r ; Check if this pair is closer than the closest pair so far ; If this pair has more sum , move to smaller values . ; Move to larger values ; Driver Code"
Solution in php:

< ? php function printClosest ( $ arr , $ n , $ x ) { $ res_l ; $ res_r ; $ l = 0 ; $ r = $ n - 1 ; $ diff = PHP_INT_MAX ; while ( $ r > $ l ) { if ( abs ( $ arr [ $ l ] + $ arr [ $ r ] - $ x ) < $ diff ) { $ res_l = $ l ; $ res_r = $ r ; $ diff = abs ( $ arr [ $ l ] + $ arr [ $ r ] - $ x ) ; } if ( $ arr [ $ l ] + $ arr [ $ r ] > $ x ) $ r -- ; else $ l ++ ; } echo " ▁ The ▁ closest ▁ pair ▁ is ▁ " , $ arr [ $ res_l ] , " ▁ and ▁ " , $ arr [ $ res_r ] ; } $ arr = array ( 10 , 22 , 28 , 29 , 30 , 40 ) ; $ x = 54 ; $ n = count ( $ arr ) ; printClosest ( $ arr , $ n , $ x ) ; ? >


"Given a string , find its first non | PHP program to find first non - repeating character ; calculate count of characters in the passed string ; The method returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; Driver code"
Solution in php:

< ? php $ NO_OF_CHARS = 256 ; $ count = array_fill ( 0 , 200 , 0 ) ; function getCharCountArray ( $ str ) { global $ count ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) ] ++ ; } function firstNonRepeating ( $ str ) { global $ count ; getCharCountArray ( $ str ) ; $ index = -1 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ count [ ord ( $ str [ $ i ] ) ] == 1 ) { $ index = $ i ; break ; } } return $ index ; } $ str = " geeksforgeeks " ; $ index = firstNonRepeating ( $ str ) ; if ( $ index == -1 ) echo " Either ▁ all ▁ characters ▁ are " . " ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ; else echo " First ▁ non - repeating ▁ " . " character ▁ is ▁ " . $ str [ $ index ] ; ? >


"Given an array and two integers l and r , find the kth largest element in the range [ l , r ] | PHP implementation of the approach ; Function to calculate the prefix ; Creating one based indexing ; Initializing and creating prefix array ; Creating a prefix array for every possible value in a given range ; Function to return the kth largest element in the index range [ l , r ] ; Binary searching through the 2d array and only checking the range in which the sub array is a part ; Driver code ; Creating the prefix array for the given array ; Queries ; Perform queries"
Solution in php:

 < ? php $ MAX = 101 ; $ prefix = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , 0 ) ) ; $ ar = array_fill ( 0 , $ MAX , 0 ) ; function cal_prefix ( $ n , $ arr ) { global $ prefix , $ ar , $ MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ ar [ $ i + 1 ] = $ arr [ $ i ] ; for ( $ i = 1 ; $ i < $ MAX ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ prefix [ $ i ] [ $ j ] = 0 ; for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { $ prefix [ $ i ] [ $ j ] = $ prefix [ $ i ] [ $ j - 1 ] + ( int ) ( $ ar [ $ j ] <= $ i ? 1 : 0 ) ; } } } function ksub ( $ l , $ r , $ n , $ k ) { global $ prefix , $ ar , $ MAX ; $ lo = 1 ; $ hi = $ MAX - 1 ; while ( $ lo + 1 < $ hi ) { $ mid = ( int ) ( ( $ lo + $ hi ) / 2 ) ; if ( $ prefix [ $ mid ] [ $ r ] - $ prefix [ $ mid ] [ $ l - 1 ] >= $ k ) $ hi = $ mid ; else $ lo = $ mid + 1 ; } if ( $ prefix [ $ lo ] [ $ r ] - $ prefix [ $ lo ] [ $ l - 1 ] >= $ k ) $ hi = $ lo ; return $ hi ; } $ arr = array ( 1 , 4 , 2 , 3 , 5 , 7 , 6 ) ; $ n = count ( $ arr ) ; $ k = 4 ; cal_prefix ( $ n , $ arr ) ; $ queries = array ( array ( 1 , $ n , 1 ) , array ( 2 , $ n - 2 , 2 ) , array ( 3 , $ n - 1 , 3 ) ) ; $ q = count ( $ queries ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo ksub ( $ queries [ $ i ] [ 0 ] , $ queries [ $ i ] [ 1 ] , $ n , $ queries [ $ i ] [ 2 ] ) . "
 " ; ? >


"Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible | Function to find the maximum possible sum ; To store required sum ; Number of 234 's can be formed ; Sum obtained with 234 s ; Remaining 2 's ; Sum obtained with 12 s ; Return the required sum ; Driver code"
Solution in php:


"Given level order traversal of a Binary Tree , check if the Tree is a Min | Returns true if given level order traversal is Min Heap . ; First non leaf node is at index ( n / 2 - 1 ) . Check whether each parent is greater than child ; Left child will be at index 2 * i + 1 Right child will be at index 2 * i + 2 ; If parent is greater than right child ; Driver code"
Solution in php:

< ? php function isMinHeap ( $ level , $ n ) { for ( $ i = ( $ n / 2 - 1 ) ; $ i >= 0 ; $ i -- ) { if ( $ level [ $ i ] > $ level [ 2 * $ i + 1 ] ) return false ; if ( 2 * $ i + 2 < $ n ) { if ( $ level [ $ i ] > $ level [ 2 * $ i + 2 ] ) return false ; } } return true ; } $ level = array ( 10 , 15 , 14 , 25 , 30 ) ; $ n = sizeof ( $ level ) ; if ( isMinHeap ( $ level , $ n ) ) echo " True " ; else echo " False " ;


"Given number of matches played , find number of teams in tournament | Function to return the number of teams ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code"
Solution in php:

< ? php function number_of_teams ( $ M ) { $ sqr = sqrt ( 1 + ( 8 * $ M ) ) ; $ N1 = ( 1 + $ sqr ) / 2 ; $ N2 = ( 1 - $ sqr ) / 2 ; if ( $ N1 > 0 ) return $ N1 ; return $ N2 ; } $ M = 45 ; echo number_of_teams ( $ M ) ; ? >


"Given two arrays count all pairs whose sum is an odd number | Function that returns the number of pairs ; Count of odd and even numbers ; Traverse in the first array and count the number of odd and evene numbers in them ; Traverse in the second array and count the number of odd and evene numbers in them ; Count the number of pairs ; Return the number of pairs ; Driver code"
Solution in php:


"Given two binary strings perform operation until B > 0 and print the result | PHP implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code"
Solution in php:


"Given two numbers a and b find all x such that a % x = b | PHP program to find x such that a % x is equal to b . ; if a is less than b then no solution ; if a is equal to b then every number greater than a will be the solution so its infinity ; count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver Code"
Solution in php:

< ? php function modularEquation ( $ a , $ b ) { if ( $ a < $ b ) { echo " No ▁ solution ▁ possible ▁ " ; return ; } if ( $ a == $ b ) { echo " Infinite ▁ Solution ▁ possible ▁ " ; return ; } $ count = 0 ; $ n = $ a - $ b ; $ y = sqrt ( $ a - $ b ) ; for ( $ i = 1 ; $ i <= $ y ; ++ $ i ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i > $ b ) $ count ++ ; if ( $ i > $ b ) $ count ++ ; } } if ( $ y * $ y == $ n && $ y > $ b ) $ count -- ; echo $ count ; } $ a = 21 ; $ b = 5 ; modularEquation ( $ a , $ b ) ; ? >


"Given two strings , find if first string is a subsequence of second | Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Base Cases ; If last characters of two strings are matching ; If last characters are not matching ; Driver Code"
Solution in php:

< ? php function isSubSequence ( $ str1 , $ str2 , $ m , $ n ) { if ( $ m == 0 ) return true ; if ( $ n == 0 ) return false ; if ( $ str1 [ $ m - 1 ] == $ str2 [ $ n - 1 ] ) return isSubSequence ( $ str1 , $ str2 , $ m - 1 , $ n - 1 ) ; return isSubSequence ( $ str1 , $ str2 , $ m , $ n - 1 ) ; } $ str1 = " gksrek " ; $ str2 = " geeksforgeeks " ; $ m = strlen ( $ str1 ) ; $ n = strlen ( $ str2 ) ; $ t = isSubSequence ( $ str1 , $ str2 , $ m , $ n ) ? " Yes ▁ " : " No " ; if ( $ t = true ) echo " Yes " ; else echo " No " ; ? >


"Given two strings check which string makes a palindrome first | Given two strings , check which string makes palindrome first . ; returns winner of two strings ; Count frequencies of characters in both given strings ; Check if there is a character that appears more than once in A and does not appear in B ; Driver Code"
Solution in php:

< ? php $ MAX_CHAR = 26 ; function stringPalindrome ( $ A , $ B ) { global $ MAX_CHAR ; $ countA = array_fill ( 0 , $ MAX_CHAR , 0 ) ; $ countB = array_fill ( 0 , $ MAX_CHAR , 0 ) ; $ l1 = strlen ( $ A ) ; $ l2 = strlen ( $ B ) ; for ( $ i = 0 ; $ i < $ l1 ; $ i ++ ) $ countA [ ord ( $ A [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ l2 ; $ i ++ ) $ countB [ ord ( $ B [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) if ( ( $ countA [ $ i ] > 1 && $ countB [ $ i ] == 0 ) ) return ' A ' ; return ' B ' ; } $ a = " abcdea " ; $ b = " bcdesg " ; echo stringPalindrome ( $ a , $ b ) ; ? >


"Given two unsorted arrays , find all pairs whose sum is x | Function to print all pairs in both arrays whose sum is equal to given value x ; Driver code"
Solution in php:


"Gnome Sort | A function to sort the algorithm using gnome sort ; Driver Code"
Solution in php:

 < ? php function gnomeSort ( $ arr , $ n ) { $ index = 0 ; while ( $ index < $ n ) { if ( $ index == 0 ) $ index ++ ; if ( $ arr [ $ index ] >= $ arr [ $ index - 1 ] ) $ index ++ ; else { $ temp = 0 ; $ temp = $ arr [ $ index ] ; $ arr [ $ index ] = $ arr [ $ index - 1 ] ; $ arr [ $ index - 1 ] = $ temp ; $ index -- ; } } echo " Sorted ▁ sequence ▁ " , " after ▁ Gnome ▁ sort : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ arr = array ( 34 , 2 , 10 , -9 ) ; $ n = count ( $ arr ) ; gnomeSort ( $ arr , $ n ) ; ? >


"Golomb sequence | Print the first n term of Golomb Sequence ; base cases ; Finding and printing first n terms of Golomb Sequence . ; Driver Code"
Solution in php:


"Golomb sequence | Return the nth element of Golomb sequence ; base case ; Recursive Step ; Print the first n term of Golomb Sequence ; Finding first n terms of Golomb Sequence . ; Driver Code"
Solution in php:

< ? php function findGolomb ( $ n ) { if ( $ n == 1 ) return 1 ; return 1 + findGolomb ( $ n - findGolomb ( findGolomb ( $ n - 1 ) ) ) ; } function printGolomb ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo findGolomb ( $ i ) , " ▁ " ; } $ n = 9 ; printGolomb ( $ n ) ; ? >


"Gould 's Sequence | 32768 = 2 ^ 15 ; Array to store Sequence up to 2 ^ 16 = 65536 ; Utility function to pre - compute odd numbers in ith row of Pascals 's triangle ; First term of the Sequence is 1 ; Initialize i to 1 ; Initialize p to 1 ( i . e 2 ^ i ) in each iteration i will be pth power of 2 ; loop to generate gould 's Sequence ; i is pth power of 2 traverse the array from j = 0 to i i . e ( 2 ^ p ) ; double the value of arr [ j ] and store to arr [ i + j ] ; update i to next power of 2 ; increment p ; Function to print gould 's Sequence ; loop to generate gould 's  Sequence up to n ; Driver code ; Get n ; Function call"
Solution in php:

< ? php $ MAX = 32768 ; $ arr = array_fill ( 0 , 2 * $ MAX , 0 ) ; function gouldSequence ( ) { global $ MAX , $ arr ; $ arr [ 0 ] = 1 ; $ i = 1 ; $ p = 1 ; while ( $ i <= $ MAX ) { $ j = 0 ; while ( $ j < $ i ) { $ arr [ $ i + $ j ] = 2 * $ arr [ $ j ] ; $ j ++ ; } $ i = ( 1 << $ p ) ; $ p ++ ; } } function printSequence ( $ n ) { global $ MAX , $ arr ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ arr [ $ i ] . " " ; } } gouldSequence ( ) ; $ n = 16 ; printSequence ( $ n ) ; ? >


"Gould 's Sequence | Function to generate gould 's Sequence ; loop to generate each row of pascal 's Triangle up to nth row ; Loop to generate each element of ith row ; if c is odd increment count ; print count of odd elements ; Get n ; Function call"
Solution in php:

< ? php function gouldSequence ( $ n ) { for ( $ row_num = 1 ; $ row_num <= $ n ; $ row_num ++ ) { $ count = 1 ; $ c = 1 ; for ( $ i = 1 ; $ i <= $ row_num ; $ i ++ ) { $ c = $ c * ( $ row_num - $ i ) / $ i ; if ( $ c % 2 == 1 ) $ count ++ ; } echo $ count , " " ; } } $ n = 16 ; gouldSequence ( $ n ) ; ? >


"Gould 's Sequence | Utility function to count odd numbers in ith row of Pascals 's triangle ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's Sequence up to n ; Get n ; Function call"
Solution in php:

< ? php function countOddNumber ( $ row_num ) { $ count = 0 ; while ( $ row_num ) { $ count += $ row_num & 1 ; $ row_num >>= 1 ; } return ( 1 << $ count ) ; } function gouldSequence ( $ n ) { for ( $ row_num = 0 ; $ row_num < $ n ; $ row_num ++ ) { echo countOddNumber ( $ row_num ) , " " ; } } $ n = 16 ; gouldSequence ( $ n ) ; ? >


"Gray to Binary and Binary to Gray conversion | Helper function to xor two characters ; Helper function to flip the bit ; function to convert binary string to gray string ; MSB of gray code is same as binary code ; Compute remaining bits , next bit is computed by doing XOR of previous and current in Binary ; Concatenate XOR of previous bit with current bit ; function to convert gray code string to binary string ; MSB of binary code is same as gray code ; Compute remaining bits ; If current bit is 0 , concatenate previous bit ; Else , concatenate invert of previous bit ; Driver Code"
Solution in php:

 < ? php function xor_c ( $ a , $ b ) { return ( $ a == $ b ) ? '0' : '1' ; } function flip ( $ c ) { return ( $ c == '0' ) ? '1' : '0' ; } function binarytoGray ( $ binary ) { $ gray = " " ; $ gray . = $ binary [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ binary ) ; $ i ++ ) { $ gray . = xor_c ( $ binary [ $ i - 1 ] , $ binary [ $ i ] ) ; } return $ gray ; } function graytoBinary ( $ gray ) { $ binary = " " ; $ binary . = $ gray [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ gray ) ; $ i ++ ) { if ( $ gray [ $ i ] == '0' ) $ binary . = $ binary [ $ i - 1 ] ; else $ binary . = flip ( $ binary [ $ i - 1 ] ) ; } return $ binary ; } $ binary = "01001" ; print ( " Gray ▁ code ▁ of ▁ " . $ binary . " ▁ is ▁ " . binarytoGray ( $ binary ) . "
 " ) ; $ gray = "01101" ; print ( " Binary ▁ code ▁ of ▁ " . $ gray . " ▁ is ▁ " . graytoBinary ( $ gray ) ) ; ? >


"Greatest Integer Function | Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code"
Solution in php:

< ? php function GIF ( $ n ) { return floor ( $ n ) ; } $ n = 2.3 ; echo GIF ( $ n ) ; ? >


"Greatest divisor which divides all natural number in range [ L , R ] | Function to return the greatest divisor that divides all the natural numbers in the range [ l , r ] ; Driver Code"
Solution in php:

< ? php function find_greatest_divisor ( $ l , $ r ) { if ( $ l == $ r ) return $ l ; return 1 ; } $ l = 2 ; $ r = 12 ; echo find_greatest_divisor ( $ l , $ r ) ; ? >


"Group consecutive characters of same type in a string | Function to return the modified string ; Store original string ; Remove all white spaces ; To store the resultant string ; Traverse the string ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant string ; Driver code"
Solution in php:

< ? php function groupCharacters ( $ s , $ len ) { $ temp = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) if ( $ s [ $ i ] != ' ▁ ' ) $ temp = $ temp . $ s [ $ i ] ; $ len = strlen ( $ temp ) ; $ ans = " " ; $ i = 0 ; while ( $ i < $ len ) { if ( ord ( $ temp [ $ i ] ) >= ord ( ' A ' ) && ord ( $ temp [ $ i ] ) <= ord ( ' Z ' ) ) { while ( $ i < $ len && ord ( $ temp [ $ i ] ) >= ord ( ' A ' ) && ord ( $ temp [ $ i ] ) <= ord ( ' Z ' ) ) { $ ans = $ ans . $ temp [ $ i ] ; $ i ++ ; } $ ans = $ ans . " ▁ " ; } else if ( ord ( $ temp [ $ i ] ) >= ord ( '0' ) && ord ( $ temp [ $ i ] ) <= ord ( '9' ) ) { while ( $ i < $ len && ord ( $ temp [ $ i ] ) >= ord ( '0' ) && ord ( $ temp [ $ i ] ) <= ord ( '9' ) ) { $ ans = $ ans . $ temp [ $ i ] ; $ i ++ ; } $ ans = $ ans . " ▁ " ; } else { while ( $ i < $ len && ord ( $ temp [ $ i ] ) >= ord ( ' * ' ) && ord ( $ temp [ $ i ] ) <= ord ( ' / ' ) ) { $ ans = $ ans . $ temp [ $ i ] ; $ i ++ ; } $ ans = $ ans . " ▁ " ; } } return $ ans ; } $ s = "34FTG234 + ▁ + - ▁ * " ; $ len = strlen ( $ s ) ; print ( groupCharacters ( $ s , $ len ) ) ; ? >


"Grouping Countries | PHP program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; someone gives different answer ; check next person ; one valid country group has been found ; Driver Code"
Solution in php:

 < ? php function countCountries ( $ ans , $ N ) { $ total_countries = 0 ; $ i = 0 ; $ invalid = false ; while ( $ i < $ N ) { $ curr_size = $ ans [ $ i ] ; $ num = $ ans [ $ i ] ; while ( $ num > 0 ) { if ( $ ans [ $ i ] != $ curr_size ) { echo " Invalid ▁ Answer
 " ; return ; } else $ num -- ; $ i ++ ; } $ total_countries ++ ; } echo " There ▁ are ▁ " , $ total_countries , " ▁ distinct ▁ companies ▁ in ▁ the ▁ group .
 " ; } $ ans = array ( 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ) ; $ n = sizeof ( $ ans ) ; countCountries ( $ ans , $ n ) ; ? >


"Hailstone Numbers | function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver Code ; Function to generate Hailstone Numbers ; Output : Number of Steps"
Solution in php:

< ? php function HailstoneNumbers ( $ N ) { static $ c ; echo $ N . " " ; if ( $ N == 1 && $ c == 0 ) { return $ c ; } else if ( $ N == 1 && $ c != 0 ) { $ c ++ ; return $ c ; } else if ( $ N % 2 == 0 ) { $ c ++ ; HailstoneNumbers ( ( int ) ( $ N / 2 ) ) ; } else if ( $ N % 2 != 0 ) { $ c ++ ; HailstoneNumbers ( 3 * $ N + 1 ) ; } return $ c ; } $ N = 7 ; $ x = HailstoneNumbers ( $ N ) ; echo " Number of Steps : " ? >


"Hamming Distance between two strings | function to calculate Hamming distance ; Driver Code ; function call"
Solution in php:

< ? php function hammingDist ( $ str1 , $ str2 ) { $ i = 0 ; $ count = 0 ; while ( isset ( $ str1 [ $ i ] ) != ' ' ) { if ( $ str1 [ $ i ] != $ str2 [ $ i ] ) $ count ++ ; $ i ++ ; } return $ count ; } $ str1 = " geekspractice " ; $ str2 = " nerdspractise " ; echo hammingDist ( $ str1 , $ str2 ) ; ? >


"Happy Number | Utility method to return sum of square of digit of n ; method return true if n is Happy number ; initialize slow and fast by n ; move slow number by one iteration ; move fast number by two iteration ; if both number meet at 1 , then return true ; Driver Code"
Solution in php:

 < ? php function numSquareSum ( $ n ) { $ squareSum = 0 ; while ( $ n ) { $ squareSum += ( $ n % 10 ) * ( $ n % 10 ) ; $ n /= 10 ; } return $ squareSum ; } function isHappynumber ( $ n ) { $ slow ; $ fast ; $ slow = $ n ; $ fast = $ n ; do { $ slow = numSquareSum ( $ slow ) ; $ fast = numSquareSum ( numSquareSum ( $ fast ) ) ; } while ( $ slow != $ fast ) ; return ( $ slow == 1 ) ; } $ n = 13 ; if ( isHappynumber ( $ n ) ) echo $ n , " ▁ is ▁ a ▁ Happy ▁ number
 " ; else echo n , " ▁ is ▁ not ▁ a ▁ Happy ▁ number
 " ; ? >


"Happy Numbers | Returns sum of squares of digits of a number n . For example for n = 12 it returns 1 + 4 = 5 ; Returns true if n is Happy number else returns false . ; Keep replacing n with sum of squares of digits until we either reach 1 or we end up in a cycle ; Number is Happy if we reach 1 ; Replace n with sum of squares of digits ; Number is not Happy if we reach 4 ; Driver code"
Solution in php:

< ? php function sumDigitSquare ( $ n ) { $ sq = 0 ; while ( $ n ) { $ digit = $ n % 10 ; $ sq += $ digit * $ digit ; $ n = $ n / 10 ; } return $ sq ; } function isHappy ( $ n ) { while ( 1 ) { if ( $ n == 1 ) return true ; $ n = sumDigitSquare ( $ n ) ; if ( $ n == 4 ) return false ; } return false ; } $ n = 23 ; if ( isHappy ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Hardy | A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code"
Solution in php:

 < ? php function exactPrimeFactorCount ( $ n ) { $ count = 0 ; if ( $ n % 2 == 0 ) { $ count ++ ; while ( $ n % 2 == 0 ) $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ count ++ ; while ( $ n % $ i == 0 ) $ n = $ n / $ i ; } } if ( $ n > 2 ) $ count ++ ; return $ count ; } $ n = 51242183 ; echo " The ▁ number ▁ of ▁ distinct ▁ prime " . " ▁ factors ▁ is / are ▁ " , exactPrimeFactorCount ( $ n ) , "
 " ; echo " The ▁ value ▁ of ▁ log ( log ( n ) ) ▁ " . " is ▁ " , log ( log ( $ n ) ) , "
 " ; ? >


"Harmonic Progression | PHP program to check if a given array can form harmonic progression ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . , first Sort the reciprocal array , then check difference between consecutive elements ; series to check whether it is in H . P ; Checking a series is in H . P or not"
Solution in php:

< ? php function checkIsHP ( $ arr ) { $ n = count ( $ arr ) ; if ( $ n == 1 ) return true ; $ rec = array ( ) ; for ( $ i = 0 ; $ i < count ( $ arr ) ; $ i ++ ) { $ a = 1 / $ arr [ $ i ] ; array_push ( $ rec , $ a ) ; } return ( $ rec ) ; sort ( $ rec ) ; $ d = $ rec [ 1 ] - $ rec [ 0 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ rec [ $ i ] - $ rec [ $ i - 1 ] != $ d ) return false ; return true ; } $ arr = array ( 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 ) ; if ( checkIsHP ( $ arr ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >


"Haversine formula to find distance between two points on a sphere | PHP program for the haversine formula ; distance between latitudes and longitudes ; convert to radians ; apply formulae ; Driver code"
Solution in php:


"HeapSort | To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize largest as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is larger than root ; If right child is larger than largest so far ; If largest is not root ; Recursively heapify the affected sub - tree ; main function to do heap sort ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver program"
Solution in php:

 < ? php function heapify ( & $ arr , $ n , $ i ) { $ largest = $ i ; $ l = 2 * $ i + 1 ; $ r = 2 * $ i + 2 ; if ( $ l < $ n && $ arr [ $ l ] > $ arr [ $ largest ] ) $ largest = $ l ; if ( $ r < $ n && $ arr [ $ r ] > $ arr [ $ largest ] ) $ largest = $ r ; if ( $ largest != $ i ) { $ swap = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ largest ] ; $ arr [ $ largest ] = $ swap ; heapify ( $ arr , $ n , $ largest ) ; } } function heapSort ( & $ arr , $ n ) { for ( $ i = $ n / 2 - 1 ; $ i >= 0 ; $ i -- ) heapify ( $ arr , $ n , $ i ) ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { $ temp = $ arr [ 0 ] ; $ arr [ 0 ] = $ arr [ $ i ] ; $ arr [ $ i ] = $ temp ; heapify ( $ arr , $ i , 0 ) ; } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) echo ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 12 , 11 , 13 , 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; heapSort ( $ arr , $ n ) ; echo ' Sorted array is ' . "
 " ; printArray ( $ arr , $ n ) ; ? >


"Hendecagonal number | Function to find Hendecagonal number ; Formula to calculate nth Hendecagonal number ; Driver Code"
Solution in php:

 < ? php function hendecagonal_num ( $ n ) { return ( 9 * $ n * $ n - 7 * $ n ) / 2 ; } $ n = 3 ; echo $ n , " th ▁ Hendecagonal ▁ number : ▁ " ; echo hendecagonal_num ( $ n ) ; echo "
 " ; $ n = 10 ; echo $ n , " th ▁ Hendecagonal ▁ number : ▁ " ; echo hendecagonal_num ( $ n ) ; ? >


"Heptagonal number | Function to return Nth Heptagonal number ; Driver Code"
Solution in php:

 < ? php function heptagonalNumber ( $ n ) { return ( ( 5 * $ n * $ n ) - ( 3 * $ n ) ) / 2 ; } $ n = 2 ; echo heptagonalNumber ( $ n ) , "
 " ; $ n = 15 ; echo heptagonalNumber ( $ n ) ; ? >


"Hexadecagonal number | Function to calculate hexadecagonal number ; Driver Code"
Solution in php:

 < ? php function hexadecagonalNum ( $ n ) { return ( ( 14 * $ n * $ n ) - 12 * $ n ) / 2 ; } $ n = 5 ; echo $ n , " th ▁ Hexadecagonal ▁ number ▁ : ▁ " ; echo hexadecagonalNum ( $ n ) ; echo "
 " ; $ n = 9 ; echo $ n , " th ▁ Hexadecagonal ▁ number ▁ : ▁ " ; echo hexadecagonalNum ( $ n ) ;


"Highest power of 2 that divides a number represented in binary | Function to return the highest power of 2 which divides the given binary number ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code"
Solution in php:

< ? php function highestPower ( $ str , $ len ) { $ ans = 0 ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == '0' ) $ ans ++ ; else break ; } return $ ans ; } $ str = "100100" ; $ len = strlen ( $ str ) ; echo highestPower ( $ str , $ len ) ; ? >


"Highest power of a number that divides other number | Function to get the prime factors and its count of times it divides ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to return the highest power ;  ; Get the prime factors of n and m ; Iterate and find the maximum power ; If i not a prime factor of n and m ; If i is a prime factor of n and m If count of i dividing m is more than i dividing n , then power will be 0 ; If i is a prime factor of M ; get the maximum power ; Drivers code"
Solution in php:

< ? php function primeFactors ( $ n , $ freq ) { $ cnt = 0 ; while ( $ n % 2 == 0 ) { $ cnt ++ ; $ n = floor ( $ n / 2 ) ; } $ freq [ 2 ] = $ cnt ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { $ cnt = 0 ; while ( $ n % $ i == 0 ) { $ cnt ++ ; $ n = floor ( $ n / $ i ) ; } $ freq [ $ i ] = $ cnt ; } if ( $ n > 2 ) $ freq [ $ n ] = 1 ; return $ freq ; } function getMaximumPower ( $ n , $ m ) { ' ' ' Initialize two arrays ' ' ' $ freq1 = array_fill ( 0 , $ n + 1 , 0 ) ; $ freq2 = array_fill ( 0 , $ m + 1 , 0 ) ; $ freq1 = primeFactors ( $ n , $ freq1 ) ; $ freq2 = primeFactors ( $ m , $ freq2 ) ; $ maxi = 0 ; for ( $ i = 2 ; $ i <= $ m ; $ i ++ ) { if ( $ freq1 [ $ i ] == 0 && $ freq2 [ $ i ] == 0 ) continue ; if ( $ freq2 [ $ i ] > $ freq1 [ $ i ] ) return 0 ; if ( $ freq2 [ $ i ] ) { $ maxi = max ( $ maxi , floor ( $ freq1 [ $ i ] / $ freq2 [ $ i ] ) ) ; } } return $ maxi ; } $ n = 48 ; $ m = 4 ; echo getMaximumPower ( $ n , $ m ) ; ? >


"Highest power of two that divides a given number | PHP program to find highest power of 2 that divides n . ; Driver Code"
Solution in php:

< ? php function highestPowerOf2 ( $ n ) { return ( $ n & ( ~ ( $ n - 1 ) ) ) ; } $ n = 48 ; echo highestPowerOf2 ( $ n ) ; ? >


"Highway Billboard Problem | PHP program to find maximum revenue by placing billboard on the highway with given constraints . ; Array to store maximum revenue at each miles . ; actual minimum distance between 2 billboards . ; check if all billboards are already placed . ; check if we have billboard for that particular mile . If not , copy the previous maximum revenue . ; we do have billboard for this mile . ; If current position is less than or equal to t , then we can have only one billboard . ; Else we may have to remove previously placed billboard ; Driver Code"
Solution in php:

< ? php function maxRevenue ( $ m , $ x , $ revenue , $ n , $ t ) { $ maxRev = array_fill ( 0 , $ m + 1 , false ) ; $ nxtbb = 0 ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { if ( $ nxtbb < $ n ) { if ( $ x [ $ nxtbb ] != $ i ) $ maxRev [ $ i ] = $ maxRev [ $ i - 1 ] ; else { if ( $ i <= $ t ) $ maxRev [ $ i ] = max ( $ maxRev [ $ i - 1 ] , $ revenue [ $ nxtbb ] ) ; else $ maxRev [ $ i ] = max ( $ maxRev [ $ i - $ t - 1 ] + $ revenue [ $ nxtbb ] , $ maxRev [ $ i - 1 ] ) ; $ nxtbb ++ ; } } else $ maxRev [ $ i ] = $ maxRev [ $ i - 1 ] ; } return $ maxRev [ $ m ] ; } $ m = 20 ; $ x = array ( 6 , 7 , 12 , 13 , 14 ) ; $ revenue = array ( 5 , 6 , 5 , 3 , 1 ) ; $ n = sizeof ( $ x ) ; $ t = 5 ; echo maxRevenue ( $ m , $ x , $ revenue , $ n , $ t ) ; ? >


"Hilbert Number | Utility function to return Nth Hilbert Number ; Driver code"
Solution in php:

< ? php function nthHilbertNumber ( $ n ) { return 4 * ( $ n - 1 ) + 1 ; } $ n = 5 ; echo nthHilbertNumber ( $ n ) ; ? >


"Hosoya 's Triangle | PHP Program to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driven Code"
Solution in php:


"Hosoya 's Triangle | PHP Program to print Hosoya 's triangle of height n. ; Print the Hosoya triangle of height n . ; base case . ; For each row . ; for each column ; ; recursive steps . ; printing the solution ; Driven Program"
Solution in php:


"How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver Code"
Solution in php:

< ? php function isHeap ( $ arr , $ i , $ n ) { if ( $ i >= ( $ n - 2 ) / 2 ) return true ; if ( $ arr [ $ i ] >= $ arr [ 2 * $ i + 1 ] && $ arr [ $ i ] >= $ arr [ 2 * $ i + 2 ] && isHeap ( $ arr , 2 * $ i + 1 , $ n ) && isHeap ( $ arr , 2 * $ i + 2 , $ n ) ) return true ; return false ; } $ arr = array ( 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( isHeap ( $ arr , 0 , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; Driver Code"
Solution in php:

< ? php function isHeap ( $ arr , $ i , $ n ) { for ( $ i = 0 ; $ i < ( ( $ n - 2 ) / 2 ) + 1 ; $ i ++ ) { if ( $ arr [ 2 * $ i + 1 ] > $ arr [ $ i ] ) return False ; if ( 2 * $ i + 2 < $ n && $ arr [ 2 * $ i + 2 ] > $ arr [ $ i ] ) return False ; return True ; } } $ arr = array ( 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( isHeap ( $ arr , 0 , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"How to check if a given number is Fibonacci number ? | A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; n is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct square ; Driver Code"
Solution in php:

 < ? php function isPerfectSquare ( $ x ) { $ s = ( int ) ( sqrt ( $ x ) ) ; return ( $ s * $ s == $ x ) ; } function isFibonacci ( $ n ) { return isPerfectSquare ( 5 * $ n * $ n + 4 ) || isPerfectSquare ( 5 * $ n * $ n - 4 ) ; } for ( $ i = 1 ; $ i <= 10 ; $ i ++ ) if ( isFibonacci ( $ i ) ) echo " $ i ▁ is ▁ a ▁ Fibonacci ▁ Number ▁
 " ; else echo " $ i ▁ is ▁ a ▁ not ▁ Fibonacci ▁ Number ▁
 " ; ? >


"How to check if an instance of 8 puzzle is solvable ? | A utility function to count inversions in given array ' arr [ ] ' ; Value 0 is used for empty space ; This function returns true if given 8 puzzle is solvable . ; Count inversions in given 8 puzzle ; return true if inversion count is even . ; Driver Code"
Solution in php:

< ? php function getInvCount ( $ arr ) { $ inv_count = 0 ; for ( $ i = 0 ; $ i < 9 - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < 9 ; $ j ++ ) $ inv_count ++ ; return $ inv_count ; } function isSolvable ( $ puzzle ) { $ invCount = getInvCount ( $ puzzle ) ; return ( $ invCount % 2 == 0 ) ; } $ puzzle = array ( array ( 1 , 8 , 2 ) , array ( 0 , 4 , 3 ) , array ( 7 , 6 , 5 ) ) ; if ( isSolvable ( $ puzzle ) == true ) echo " Solvable " ; else echo " Not ▁ Solvable " ; ? >


"How to check if two given sets are disjoint ? | Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver Code"
Solution in php:

< ? php function areDisjoint ( $ set1 , $ set2 , $ m , $ n ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ set1 [ $ i ] == $ set2 [ $ j ] ) return false ; return true ; } $ set1 = array ( 12 , 34 , 11 , 9 , 3 ) ; $ set2 = array ( 7 , 2 , 1 , 5 ) ; $ m = sizeof ( $ set1 ) ; $ n = sizeof ( $ set2 ) ; if ( areDisjoint ( $ set1 , $ set2 , $ m , $ n ) == true ) echo " Yes " ; else echo " ▁ No " ; ? >


"How to compute mod of a big number ? | Function to compute num ( mod a ) ; Initialize result ; One by one process all digits of ' num ' ; Driver Code"
Solution in php:


"How to swap two numbers without using a temporary variable ? | Driver code ; Code to swap ' x ' and ' y ' x now becomes 50 ; y becomes 10 ; x becomes 5"
Solution in php:


"How to swap two numbers without using a temporary variable ? | PHP code to swap using XOR ; Code to swap ' x ' ( 1010 ) and ' y ' ( 0101 ) x now becomes 15 ( 1111 ) ; y becomes 10 ( 1010 ) ; x becomes 5 ( 0101 )"
Solution in php:


"How to swap two numbers without using a temporary variable ? | Swap function ; Driver Code"
Solution in php:

< ? php function swap ( & $ xp , & $ yp ) { $ xp = $ xp ^ $ yp ; $ yp = $ xp ^ $ yp ; $ xp = $ xp ^ $ yp ; } $ x = 10 ; swap ( $ x , $ x ) ; print ( " After ▁ swap ( & x , ▁ & x ) : ▁ x ▁ = ▁ " . $ x ) ; ? >


"How to turn on a particular bit in a number ? | Returns a number that has all bits same as n except the k 'th bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver Code"
Solution in php:

< ? php function turnOnK ( $ n , $ k ) { if ( $ k <= 0 ) return $ n ; return ( $ n | ( 1 << ( $ k - 1 ) ) ) ; } $ n = 4 ; $ k = 2 ; echo turnOnK ( $ n , $ k ) ; ? >


"Hypercube Graph | PHP program to find vertices in a hypercube graph of order n ; Function to find power of 2 ; Driver Code"
Solution in php:

< ? php { function power ( $ n ) { if ( $ n == 1 ) return 2 ; return 2 * power ( $ n - 1 ) ; } { $ n = 4 ; echo ( power ( $ n ) ) ; } } ? >


"Icosidigonal number | Function to calculate Icosidigonal number ; Formula for finding nth Icosidigonal number ; Driver Code"
Solution in php:

 < ? php function icosidigonal_num ( $ n ) { return ( 20 * $ n * $ n - 18 * $ n ) / 2 ; } $ n = 4 ; echo $ n , " th ▁ Icosidigonal ▁ number ▁ : ▁ " , icosidigonal_num ( $ n ) ; echo "
 " ; $ n = 8 ; echo $ n , " th ▁ Icosidigonal ▁ number ▁ : ▁ " , icosidigonal_num ( $ n ) ; ? >


"Implement * , | Function to flip the sign using only " + " operator ( It is simple with ' * ' allowed . We need to do a = ( - 1 ) * a ; If sign is + ve turn it - ve and vice - versa ; Check if a and b are of different signs ; Function to subtract two numbers by negating b and adding them ; Negating b ; Function to multiply a by b by adding a to itself b times ; because algo is faster if b < a ; Adding a to itself b times ; Check if final sign must be - ve or + ve ; Function to divide a by b by counting how many times ' b ' can be subtracted from ' a ' before getting 0 ; Negating b to subtract from a ; Subtracting divisor from dividend ; Check if a and b are of similar symbols or not ; Driver code"
Solution in php:

 < ? php function flipSign ( $ a ) { $ neg = 0 ; $ tmp = $ a < 0 ? 1 : -1 ; while ( $ a != 0 ) { $ neg += $ tmp ; $ a += $ tmp ; } return $ neg ; } function areDifferentSign ( $ a , $ b ) { return ( ( $ a < 0 && $ b > 0 ) || ( $ a > 0 && $ b < 0 ) ) ; } function sub ( $ a , $ b ) { return $ a + flipSign ( $ b ) ; } function mul ( $ a , $ b ) { if ( $ a < $ b ) return mul ( $ b , $ a ) ; $ sum = 0 ; for ( $ i = abs ( $ b ) ; $ i > 0 ; $ i -- ) $ sum += $ a ; if ( $ b < 0 ) $ sum = flipSign ( $ sum ) ; return $ sum ; } function division ( $ a , $ b ) { $ quotient = 0 ; $ divisor = flipSign ( abs ( $ b ) ) ; for ( $ dividend = abs ( $ a ) ; $ dividend >= abs ( $ divisor ) ; $ dividend += $ divisor ) $ quotient ++ ; if ( areDifferentSign ( $ a , $ b ) ) $ quotient = flipSign ( $ quotient ) ; return $ quotient ; } print ( " Subtraction ▁ is ▁ " . sub ( 4 , -2 ) . "
 " ) ; print ( " Product ▁ is ▁ " . mul ( -9 , 6 ) . "
 " ) ; list ( $ a , $ b ) = array ( 8 , 2 ) ; if ( $ b ) print ( " Division ▁ is ▁ " . division ( $ a , $ b ) ) ; else print ( " Exception ▁ : - ▁ Divide ▁ by ▁ 0" ) ; ? >


"Implement rand3 ( ) using rand2 ( ) | Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 0 , 1 or 2 with equal probability 1 with 75 % ; returns 0 , 1 , 2 or 3 with 25 % probability ; Driver Code"
Solution in php:

< ? php function rand2 ( ) { return rand ( ) & 1 ; } function rand3 ( ) { $ r = 2 * rand2 ( ) + rand2 ( ) ; if ( $ r < 3 ) return $ r ; return rand3 ( ) ; } srand ( time ( NULL ) ) ; for ( $ i = 0 ; $ i < 100 ; $ i ++ ) echo rand3 ( ) ; ? >


"Increasing sequence with given GCD | Function to print the required sequence ; Driver Code"
Solution in php:

< ? php function generateSequence ( $ n , $ g ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo $ i * $ g . " ▁ " ; } $ n = 6 ; $ g = 5 ; generateSequence ( $ n , $ g ) ; ? >


"Increment a number by one by manipulating the bits | function to find the position of rightmost set bit ; function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set ; toggle the last m bits and return the number ; function to increment a number by one by manipulating the bits ; get position of rightmost unset bit if all bits of ' n ' are set , then the bit left to the MSB is the rightmost unset bit ; kth bit of n is being set by this operation ; from the right toggle all the bits before the k - th ; required number ; Driver code"
Solution in php:

< ? php function getPosOfRightmostSetBit ( $ n ) { $ t = $ n & - $ n ; return log ( $ t , 2 ) ; } function toggleLastKBits ( $ n , $ k ) { $ num = ( 1 << $ k ) - 1 ; return ( $ n ^ $ num ) ; } function incrementByOne ( $ n ) { $ k = getPosOfRightmostSetBit ( ~ $ n ) ; $ n = ( ( 1 << $ k ) $ n ) ; if ( $ k != 0 ) $ n = toggleLastKBits ( $ n , $ k ) ; return $ n ; } $ n = 15 ; echo incrementByOne ( $ n ) ; ? >


"Increment a number without using ++ or + | function that increment the value . ; Invert bits and apply negative sign ; Driver code"
Solution in php:

< ? php function increment ( $ i ) { $ i = - ( ~ ord ( $ i ) ) ; return chr ( $ i ) ; } $ n = ' a ' ; echo increment ( $ n ) ; ? >


< ? php function increment ( $ i ) { $ i = - ( ~ $ i ) ; return $ i ; } $ n = 3 ; echo increment ( $ n ) ; ? >


"Indexed Sequential Search | PHP program for Indexed Sequential Search ; Storing element ; Storing the index ; Driver code ; Element to search ; Function call"
Solution in php:

< ? php function indexedSequentialSearch ( $ arr , $ n , $ k ) { $ elements = array ( ) ; $ indices = array ( ) ; $ temp = array ( ) ; $ j = 0 ; $ ind = 0 ; $ start = 0 ; $ end = 0 ; $ set = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 3 ) { $ elements [ $ ind ] = $ arr [ $ i ] ; $ indices [ $ ind ] = $ i ; $ ind ++ ; } if ( $ k < $ elements [ 0 ] ) { echo " Not ▁ found " ; } else { for ( $ i = 1 ; $ i <= $ ind ; $ i ++ ) if ( $ k < $ elements [ $ i ] ) { $ start = $ indices [ $ i - 1 ] ; $ set = 1 ; $ end = $ indices [ $ i ] ; break ; } } if ( $ set == 1 ) { $ start = $ indices [ $ i - 1 ] ; $ end = $ n ; } for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) { if ( $ k == $ arr [ $ i ] ) { $ j = 1 ; break ; } } if ( $ j == 1 ) echo " Found ▁ at ▁ index ▁ " , $ i ; else echo " Not ▁ found " ; } $ arr = array ( 6 , 7 , 8 , 9 , 10 ) ; $ n = count ( $ arr ) ; $ k = 8 ; indexedSequentialSearch ( $ arr , $ n , $ k ) ; ? >


"Integers from the range that are composed of a single distinct digit | Boolean function to check distinct digits of a number ; Take last digit ; Check if all other digits are same as last digit ; Remove last digit ; Function to return the count of integers that are composed of a single distinct digit only ; If i has single distinct digit ; Driver code"
Solution in php:

< ? php function checkDistinct ( $ x ) { $ last = $ x % 10 ; while ( $ x ) { if ( $ x % 10 != $ last ) return false ; $ x = floor ( $ x / 10 ) ; } return true ; } function findCount ( $ L , $ R ) { $ count = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( checkDistinct ( $ i ) ) $ count += 1 ; } return $ count ; } $ L = 10 ; $ R = 50 ; echo findCount ( $ L , $ R ) ; ? >


"Interchange elements of first and last rows in matrix | PHP code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; input in the array ; printing the interchanged matrix"
Solution in php:


"Interchanging first and second halves of strings | Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . ; Driver Code ; Calling function"
Solution in php:

< ? php function swapTwoHalves ( $ a , $ b ) { $ la = strlen ( $ a ) ; $ lb = strlen ( $ b ) ; $ c = substr ( $ a , 0 , intval ( $ la / 2 ) ) . substr ( $ b , intval ( $ lb / 2 ) , $ lb ) ; $ d = substr ( $ b , 0 , intval ( $ lb / 2 ) ) . substr ( $ a , intval ( $ la / 2 ) , $ la ) ; echo ( $ c . " " ▁ . ▁ $ d ▁ . ▁ " " } $ a = " remuneration " ; $ b = " day " ; swapTwoHalves ( $ a , $ b ) ; ? >


"Interprime | 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 ; Function to check if the given number is interprime or not ; Smallest Interprime is 4 So the number less than 4 can not be a Interprime ; Calculate first prime number < n ; Calculate first prime number > n ; Check if prev_prime and next_prime have the same average ; Driver Code"
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 ; } function isInterprime ( $ n ) { if ( $ n < 4 ) return false ; $ prev_prime = $ n ; $ next_prime = $ n ; while ( ! isPrime ( $ prev_prime ) ) { $ prev_prime -- ; } while ( ! isPrime ( $ next_prime ) ) { $ next_prime ++ ; } if ( ( $ prev_prime + $ next_prime ) == 2 * $ n ) return true ; else return false ; } $ n = 9 ; if ( isInterprime ( $ n ) ) echo " YES " ; else echo " NO " ; ? >