"Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | Code to print the sum of the series ; Driver code"
How can the above be solved in PHP?

< ? php function sum ( $ x , $ n ) { $ i ; $ total = 1.0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ total = $ total + ( pow ( $ x , $ i ) / $ i ) ; return $ total ; } $ x = 2 ; $ n = 5 ; echo ( sum ( $ x , $ n ) ) ; ? >


"Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | code to print the sum of the series ; Driver code"
How can the above be solved in PHP?

< ? php function sum ( $ x , $ n ) { $ i ; $ total = 1.0 ; $ multi = $ x ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ total = $ total + $ multi / $ i ; $ multi = $ multi * $ x ; } return $ total ; } $ x = 2 ; $ n = 5 ; echo ( sum ( $ x , $ n ) ) ; ? >


"Sum of the Series 1 / ( 1 * 2 ) + 1 / ( 2 * 3 ) + 1 / ( 3 * 4 ) + 1 / ( 4 * 5 ) + . . . . . | function to find the sum of given series ; Computing sum term by term ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOfTheSeries ( $ n ) { $ sum = 0.0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += 1.0 / ( $ i * ( $ i + 1 ) ) ; return $ sum ; } $ n = 10 ; echo sumOfTheSeries ( $ n ) ; ? >


"Sum of the alphabetical values of the characters of a string | Function to find string score ; Driver code"
How can the above be solved in PHP?

 < ? php function strScore ( $ str , $ s , $ n ) { $ score = 0 ; $ index ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == $ s ) { for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) $ score += ( ord ( $ s [ $ j ] ) - ord ( ' a ' ) ) + 1 ; $ index = ( $ i + 1 ) ; break ; } } $ score = $ score * $ index ; return $ score ; } $ str = array ( " sahil " , " shashanak " , " sanjit " , " abhinav " , " mohit " ) ; $ s = " abhinav " ; $ n = sizeof ( $ str ) ; $ score = strScore ( $ str , $ s , $ n ) ; echo $ score , "
 " ; ? >


"Sum of the digits of a number N written in all bases from 2 to N / 2 | Function to calculate the sum of the digits of n in the given base ; Sum of digits ; Digit of n in the given base ; Add the digit ; Function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all bases ; function call for multiple bases ; Driver Code"
How can the above be solved in PHP?

< ? php function solve ( $ n , $ base ) { $ sum = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ sum += $ remainder ; $ n = $ n / $ base ; } return $ sum ; } function SumsOfDigits ( $ n ) { $ sum = 0 ; for ( $ base = 2 ; $ base <= $ n / 2 ; ++ $ base ) $ sum += solve ( $ n , $ base ) ; echo $ sum ; } $ n = 8 ; SumsOfDigits ( $ n ) ; ? >


"Sum of the first N terms of the series 2 , 6 , 12 , 20 , 30. ... | Function to calculate the sum ; number of terms to be included in the sum ; find the Sn"
How can the above be solved in PHP?


"Sum of the first N terms of the series 5 , 12 , 23 , 38. ... | Function to calculate the sum ; number of terms to be included in sum ; find the Sn"
How can the above be solved in PHP?


"Sum of the natural numbers ( up to N ) whose modulo with K yield R | Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code"
How can the above be solved in PHP?


"Sum of the numbers upto N that are divisible by 2 or 5 | Function to find the sum ; sum2 is sum of numbers divisible by 2 ; sum5 is sum of number divisible by 5 ; sum10 of numbers divisible by 2 and 5 ; Driver Code"
How can the above be solved in PHP?


"Sum of the sequence 2 , 22 , 222 , ... ... ... | function which return the the sum of series ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { return 0.0246 * ( pow ( 10 , $ n ) - 1 - ( 9 * $ n ) ) ; } $ n = 3 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | Function to return sum ; Driver code"
How can the above be solved in PHP?

< ? php function sum ( $ n ) { return $ n * ( $ n + 1 ) * ( $ n + 2 ) / 3 ; } $ n = 2 ; echo sum ( $ n ) ; ? >


"Sum of the series 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms | function which return the the sum of series ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { return ( 0.666 ) * ( 1 - 1 / pow ( 10 , $ n ) ) ; } $ n = 2 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | Function to find sum of given series ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) $ sum += $ j ; return $ sum ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + Ã ¢ â ‚¬¦ + ( 2 n | functionn to find the sum of the given series ; required sum ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOfTheSeries ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; } $ n = 5 ; echo " Sum ▁ = ▁ " . sumOfTheSeries ( $ n ) ; ? >


"Sum of the series 1 , 2 , 4 , 3 , 5 , 7 , 9 , 6 , 8 , 10 , 11 , 13. . till N | Function to find the sum of first N odd numbers ; Function to find the sum of first N even numbers ; Function to overall find the sum of series ; Initial odd numbers ; Initial even numbers ; First power of 2 ; Check for parity for odd / even ; Counts the sum ; Get the minimum out of remaining num or power of 2 ; Decrease that much numbers from num ; If the segment has odd numbers ; Summate the odd numbers By exclusion ; Increase number of odd numbers ; If the segment has even numbers ; Summate the even numbers By exclusion ; Increase number of even numbers ; Next set of numbers ; Change parity for odd / even ; Driver code"
How can the above be solved in PHP?

< ? php function sumodd ( $ n ) { return ( $ n * $ n ) ; } function sumeven ( $ n ) { return ( $ n * ( $ n + 1 ) ) ; } function findSum ( $ num ) { $ sumo = 0 ; $ sume = 0 ; $ x = 1 ; $ cur = 0 ; $ ans = 0 ; while ( $ num > 0 ) { $ inc = min ( $ x , $ num ) ; $ num -= $ inc ; if ( $ cur == 0 ) { $ ans = $ ans + sumodd ( $ sumo + $ inc ) - sumodd ( $ sumo ) ; $ sumo += $ inc ; } else { $ ans = $ ans + sumeven ( $ sume + $ inc ) - sumeven ( $ sume ) ; $ sume += $ inc ; } $ x *= 2 ; $ cur ^= 1 ; } return $ ans ; } $ n = 4 ; echo findSum ( $ n ) ; ? >


"Sum of the series 1 , 3 , 6 , 10. . . ( Triangular Numbers ) | Function to find the sum of series ; Driver code"
How can the above be solved in PHP?

< ? php function seriesSum ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >


"Sum of the series 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ... . . + n ^ n using recursion | Recursive function to return the sum of the given series ; 1 ^ 1 = 1 ; Recursive call ; Driver code"
How can the above be solved in PHP?

< ? php function sum ( $ n ) { if ( $ n == 1 ) return 1 ; else return ( pow ( $ n , $ n ) + sum ( $ n - 1 ) ) ; } $ n = 2 ; echo ( sum ( $ n ) ) ; ? >


"Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | PHP program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code"
How can the above be solved in PHP?

< ? php function sumofseries ( $ n ) { $ res = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ res += ( $ i ) * ( $ i + 1 ) * ( $ i + 2 ) ; return $ res ; } echo sumofseries ( 3 ) ; ? >


"Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | function to calculate sum of series ; Driver Code"
How can the above be solved in PHP?

< ? php function sumofseries ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) * ( $ n + 3 ) / 4 ) ; } echo sumofseries ( 3 ) ; ? >


"Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + â €¦ â €¦ + ( 2 + 4 + 6 + 8 + â €¦ . + 2 n ) | function to find the sum of the given series ; sum of 1 st n natural numbers ; sum of squares of 1 st n natural numbers ; required sum ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOfTheSeries ( $ n ) { $ sum_n = ( $ n * ( $ n + 1 ) / 2 ) ; $ sum_sq_n = ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; return ( $ sum_n + $ sum_sq_n ) ; } $ n = 5 ; echo ( " Sum ▁ = ▁ " . sumOfTheSeries ( $ n ) ) ; ? >


"Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above"
How can the above be solved in PHP?


"Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | function to calculate sum of series ; initialize sum as 0 ; loop to calculate sum of series ; calculate 2 ^ i and add it to sum ; Driver code"
How can the above be solved in PHP?

< ? php function calculateSum ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( 1 << $ i ) ; } return $ sum ; } $ n = 10 ; echo " Sum ▁ of ▁ the ▁ series ▁ of ▁ " . " power ▁ 2 ▁ is ▁ : ▁ " , calculateSum ( $ n ) ; ? >


"Sum of the series 5 + 55 + 555 + . . up to n terms | function which return the the sum of series ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { return ( int ) ( 0.6172 * ( pow ( 10 , $ n ) - 1 ) - 0.55 * $ n ) ; } $ n = 2 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series Kn + ( K ( n | Function to return sum ; Driver code"
How can the above be solved in PHP?

< ? php function sum ( $ k , $ n ) { $ sum = pow ( $ k , $ n + 1 ) - pow ( $ k - 1 , $ n + 1 ) ; return $ sum ; } $ n = 3 ; $ K = 3 ; echo sum ( $ K , $ n ) ;


"Sum of the series Kn + ( K ( n | Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code"
How can the above be solved in PHP?

< ? php function exponent ( $ A , $ B ) { if ( $ A == 0 ) return 0 ; if ( $ B == 0 ) return 1 ; if ( $ B % 2 == 0 ) { $ y = exponent ( $ A , $ B / 2 ) ; $ y = ( $ y * $ y ) ; } else { $ y = $ A ; $ y = ( $ y * exponent ( $ A , $ B - 1 ) ) ; } return $ y ; } function sum ( $ k , $ n ) { $ sum = exponent ( $ k , $ n + 1 ) - exponent ( $ k - 1 , $ n + 1 ) ; return $ sum ; } $ n = 3 ; $ K = 3 ; echo sum ( $ K , $ n ) ; ? >


"Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute sum of current digits and carry ; Add remaining digits of str2 [ ] ; Add remaining carry ; reverse resultant string ; Driver code"
How can the above be solved in PHP?


"Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Reverse both of strings ; Do school mathematics , compute sum of current digits and carry ; Calculate carry for next step ; Add remaining digits of larger number ; Add remaining carry ; reverse resultant string ; Driver code"
How can the above be solved in PHP?


"Sum of width ( max and min diff ) of all Subsequences | PHP implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver Code"
How can the above be solved in PHP?


"Summation of GCD of all the pairs up to N | PHP approach of finding sum of GCD of all pairs ; phi [ i ] stores euler totient function for i result [ j ] stores result for value j ; Precomputation of phi [ ] numbers . Refer link for details : https : goo . gl / LUqdtY ; Refer https : goo . gl / LUqdtY ; Precomputes result for all numbers till MAX ; Precompute all phi value ; Iterate throght all the divisors of i . ; Add summation of previous calculated sum ; Function to calculate sum of all the GCD pairs"
How can the above be solved in PHP?

 < ? php $ MAX = 100001 ; $ phi = array_fill ( 0 , $ MAX , 0 ) ; $ result = array_fill ( 0 , $ MAX , 0 ) ; function computeTotient ( ) { global $ MAX , $ phi ; $ phi [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( ! $ phi [ $ i ] ) { $ phi [ $ i ] = $ i - 1 ; for ( $ j = ( $ i << 1 ) ; $ j < $ MAX ; $ j += $ i ) { if ( ! $ phi [ $ j ] ) $ phi [ $ j ] = $ j ; $ phi [ $ j ] = ( $ phi [ $ j ] / $ i ) * ( $ i - 1 ) ; } } } } function sumOfGcdPairs ( ) { global $ MAX , $ phi , $ result ; computeTotient ( ) ; for ( $ i = 1 ; $ i < $ MAX ; ++ $ i ) { for ( $ j = 2 ; $ i * $ j < $ MAX ; ++ $ j ) $ result [ $ i * $ j ] += $ i * $ phi [ $ j ] ; } for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ result [ $ i ] += $ result [ $ i - 1 ] ; } sumOfGcdPairs ( ) ; $ N = 4 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . "
 " ; $ N = 12 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . "
 " ; $ N = 5000 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . "
 " ; ? >


"Summing the sum series | Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ; Driver Code"
How can the above be solved in PHP?


"Superperfect Number | Function to calculate sum of all divisors ; Final result of summation of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Returns true if n is Super Perfect else false . ; Find the sum of all divisors of number n ; Again find the sum of all divisors of n1 and check if sum is equal to n1 ; Driver code"
How can the above be solved in PHP?

 < ? php function divSum ( $ num ) { $ result = 0 ; for ( $ i = 1 ; $ i * $ i <= $ num ; ++ $ i ) { if ( $ num % $ i == 0 ) { if ( $ i == ( $ num / $ i ) ) $ result += $ i ; else $ result += ( $ i + $ num / $ i ) ; } } return $ result ; } function isSuperPerfect ( $ n ) { $ n1 = divSum ( $ n ) ; return ( 2 * $ n == divSum ( $ n1 ) ) ; } $ n = 16 ; $ hh = ( isSuperPerfect ( $ n ) ? " Yes
 " : " No
 " ) ; echo ( $ hh ) ; $ n = 6 ; $ hh = ( isSuperPerfect ( $ n ) ? " Yes
 " : " No
 " ) ; echo ( $ hh ) ; ? >


"Surd number | Returns true if x is Surd number ; Try all powers of i ; Driver code"
How can the above be solved in PHP?

< ? php function isSurd ( $ n ) { for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { $ j = $ i ; while ( $ j < $ n ) $ j = $ j * $ i ; if ( $ j == $ n ) return false ; } return true ; } $ n = 15 ; if ( isSurd ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Surface Area and Volume of Hexagonal Prism | Function to calculate Surface area ; Formula to calculate surface area ; Display surface area ; Function to calculate Volume ; formula to calculate Volume ; Display Volume ; Driver Code ; surface area function call ; volume function call"
How can the above be solved in PHP?

 < ? php function findSurfaceArea ( $ a , $ h ) { $ Area ; $ Area = 6 * $ a * $ h + 3 * sqrt ( 3 ) * $ a * $ a ; echo " Surface ▁ Area : ▁ " , $ Area , "
 " ; } function findVolume ( $ a , $ h ) { $ Volume ; $ Volume = 3 * sqrt ( 3 ) * $ a * $ a * $ h / 2 ; echo " Volume : ▁ " , $ Volume ; } $ a = 5 ; $ h = 10 ; findSurfaceArea ( $ a , $ h ) ; findVolume ( $ a , $ h ) ; ? >


"Swap all odd and even bits | Function to swap even and odd bits ; Get all even bits of x ; Get all odd bits of x ; Right shift even bits ; Left shift odd bits ; Combine even and odd bits ; 00010111 ; Output is 43 ( 00101011 )"
How can the above be solved in PHP?

< ? php function swapBits ( $ x ) { $ even_bits = $ x & 0xAAAAAAAA ; $ odd_bits = $ x & 0x55555555 ; $ even_bits >>= 1 ; $ odd_bits <<= 1 ; return ( $ even_bits $ odd_bits ) ; } $ x = 23 ; echo swapBits ( $ x ) ; ? >


"Swap bits in a given number | PHP Program to swap bits in a given number function returns the swapped bits ; Move all bits of first set to rightmost side ; Move all bits of second set to rightmost side ; XOR the two sets ; Put the xor bits back to their original positions ; XOR the ' xor ' with the original number so that the two sets are swapped ; Driver Code"
How can the above be solved in PHP?


"Swap every two bits in bytes | PHP program to swap every two bits in a byte . ; Extracting the high bit shift it to lowbit Extracting the low bit shift it to highbit ; Driver Code"
How can the above be solved in PHP?

< ? php function swapBitsInPair ( $ x ) { return ( ( $ x & 0b10101010 ) >> 1 ) | ( ( $ x & 0b01010101 ) << 1 ) ; } $ x = 4 ; echo swapBitsInPair ( $ x ) ; ? >


"Swap three variables without using temporary variable | Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver code ; Calling Function"
How can the above be solved in PHP?

 < ? php function swapThree ( & $ a , & $ b , & $ c ) { $ a = $ a ^ $ b ^ $ c ; $ b = $ a ^ $ b ^ $ c ; $ c = $ a ^ $ b ^ $ c ; $ a = $ a ^ $ b ^ $ c ; } $ a = 10 ; $ b = 20 ; $ c = 30 ; echo " Before ▁ swapping ▁ a ▁ = ▁ " , $ a , " , ▁ b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c , "
 " ; swapThree ( $ a , $ b , $ c ) ; echo " After ▁ swapping ▁ a ▁ = ▁ " , $ a , " , ▁ b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c , "
 " ; ? >


"Swapping four variables without temporary variable | PHP program to swap 4 variables without using temporary variable ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; initialising variables ; Function call"
How can the above be solved in PHP?

 < ? php function swap ( $ a , $ b , $ c , $ d ) { $ a = $ a + $ b ; $ b = $ a - $ b ; $ a = $ a - $ b ; $ b = $ b + $ c ; $ c = $ b - $ c ; $ b = $ b - $ c ; $ c = $ c + $ d ; $ d = $ c - $ d ; $ c = $ c - $ d ; echo " values ▁ after ▁ swapping ▁ are ▁ : ▁ " , "
 " ; echo " a = " ▁ , ▁ $ a ▁ , " " ;
 	 echo ▁ " b = " ▁ , ▁ $ b ▁ , " " ;
 	 echo ▁ " c = " ▁ , $ c ▁ , " " ;
 	 echo ▁ " d = " ▁ , ▁ $ d ▁ , " " } $ a = 1 ; $ b = 2 ; $ c = 3 ; $ d = 4 ; echo " Values ▁ before ▁ swapping ▁ are ▁ : " , "
 " ; echo " a = " ▁ , ▁ $ a ▁ , " " ;
 	 echo ▁ " b = " ▁ , $ b , " " ;
 	 echo ▁ " c = " ▁ , ▁ $ c ▁ , " " ;
 	 echo ▁ " d = " ▁ , ▁ $ d ▁ , " " , " " swap ( $ a , $ b , $ c , $ d ) ; ? >


"Sylvester 's sequence | PHP program to print terms of Sylvester 's sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver Code"
How can the above be solved in PHP?


"Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver Code"
How can the above be solved in PHP?

< ? php function fact ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * fact ( $ n - 1 ) ; } echo fact ( 5 ) ; ? >


"Tail Recursion | A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver program to test above function"
How can the above be solved in PHP?

< ? php function factTR ( $ n , $ a ) { if ( $ n == 0 ) return $ a ; return factTR ( $ n - 1 , $ n * $ a ) ; } function fact ( $ n ) { return factTR ( $ n , 1 ) ; } echo fact ( 5 ) ; ? >


"Temple Offerings | Returns minimum offerings required ; Go through all templs one by one ; Go to left while height keeps increasing ; Go to right while height keeps increasing ; This temple should offer maximum of two values to follow the rule . ; Driver code"
How can the above be solved in PHP?

 < ? php function offeringNumber ( $ n , $ templeHeight ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ left = 0 ; $ right = 0 ; for ( $ j = $ i - 1 ; $ j >= 0 ; -- $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j + 1 ] ) ++ $ left ; else break ; } for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j - 1 ] ) ++ $ right ; else break ; } $ sum += max ( $ right , $ left ) + 1 ; } return $ sum ; } $ arr1 = array ( 1 , 2 , 2 ) ; echo offeringNumber ( 3 , $ arr1 ) , "
 " ; $ arr2 = array ( 1 , 4 , 3 , 6 , 2 , 1 ) ; echo offeringNumber ( 6 , $ arr2 ) , "
 " ; ? >


"Tetrahedral Numbers | Function to find Tetrahedral Number ; Driver Code"
How can the above be solved in PHP?

< ? php function tetrahedralNumber ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 5 ; echo tetrahedralNumber ( $ n ) ; ? >


"Thabit number | Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Code ; Check if number is thabit number"
How can the above be solved in PHP?

< ? php function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; } function isThabitNumber ( $ n ) { $ n = $ n + 1 ; if ( $ n % 3 == 0 ) $ n = $ n / 3 ; else return false ; if ( isPowerOfTwo ( $ n ) ) return true ; else return false ; } $ n = 47 ; if ( isThabitNumber ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"The Lazy Caterer 's Problem | This function receives an integer n and returns the maximum number of pieces that can be made form pancake using n cuts ; Use the formula ; Driver Code"
How can the above be solved in PHP?

 < ? php function findPieces ( $ n ) { return ( $ n * ( $ n + 1 ) ) / 2 + 1 ; } echo findPieces ( 1 ) , "
 " ; echo findPieces ( 2 ) , "
 " ; echo findPieces ( 3 ) , "
 " ; echo findPieces ( 50 ) , "
 " ; ? >


"The Stock Span Problem | Fills array S [ ] with span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; print the calculated span values ; Driver Code ; Fill the span values in array S [ ]"
How can the above be solved in PHP?


"The biggest possible circle that can be inscribed in a rectangle | Function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; Driver code"
How can the above be solved in PHP?

 < ? php function circlearea ( $ l , $ b ) { if ( $ l < 0 $ b < 0 ) return -1 ; if ( $ l < $ b ) return 3.14 * pow ( $ l / 2 , 2 ) ; else return 3.14 * pow ( $ b / 2 , 2 ) ; } $ l = 4 ; $ b = 8 ; echo circlearea ( $ l , $ b ) . "
 " ; ? >


"The painter 's partition problem | function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver Code ; Calculate size of array ."
How can the above be solved in PHP?


"Third largest element in an array of distinct elements | PHP program to find third Largest element in an array ; There should be atleast three elements ; Initialize first , second and third Largest element ; Traverse array elements to find the third Largest ; If current element is greater than first , then update first , second and third ; If arr [ i ] is in between first and second ; If arr [ i ] is in between second and third ; Driver Code"
How can the above be solved in PHP?

< ? php function thirdLargest ( $ arr , $ arr_size ) { if ( $ arr_size < 3 ) { echo " ▁ Invalid ▁ Input ▁ " ; return ; } $ first = $ arr [ 0 ] ; $ second = PHP_INT_MIN ; $ third = PHP_INT_MIN ; for ( $ i = 1 ; $ i < $ arr_size ; $ i ++ ) { if ( $ arr [ $ i ] > $ first ) { $ third = $ second ; $ second = $ first ; $ first = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ second ) { $ third = $ second ; $ second = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ third ) $ third = $ arr [ $ i ] ; } echo " The ▁ third ▁ Largest ▁ element ▁ is ▁ " , $ third ; } $ arr = array ( 12 , 13 , 1 , 10 , 34 , 16 ) ; $ n = sizeof ( $ arr ) ; thirdLargest ( $ arr , $ n ) ; ? >


"Third largest element in an array of distinct elements | PHP program to find third Largest element in an array of distinct elements ; There should be atleast three elements ; Find first largest element ; Find second largest element ; Find third largest element ; Driver Code"
How can the above be solved in PHP?

 < ? php function thirdLargest ( $ arr , $ arr_size ) { if ( $ arr_size < 3 ) { echo " ▁ Invalid ▁ Input ▁ " ; return ; } $ first = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ first ) $ first = $ arr [ $ i ] ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ second && $ arr [ $ i ] < $ first ) $ second = $ arr [ $ i ] ; $ third = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ third && $ arr [ $ i ] < $ second ) $ third = $ arr [ $ i ] ; echo " The ▁ third ▁ Largest ▁ element ▁ is ▁ " , $ third , "
 " ; } $ arr = array ( 12 , 13 , 1 , 10 , 34 , 16 ) ; $ n = sizeof ( $ arr ) ; thirdLargest ( $ arr , $ n ) ; ? >


"Tidy Number ( Digits in non | Returns true if num is Tidy ; To store previous digit ( Assigning initial value which is more than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver code"
How can the above be solved in PHP?

< ? php function isTidy ( $ num ) { $ prev = 10 ; while ( $ num ) { $ rem = $ num % 10 ; $ num = ( int ) $ num / 10 ; if ( $ rem > $ prev ) return false ; $ prev = $ rem ; } return true ; } $ num = 1556 ; if ( isTidy ( $ num ) == true ) echo " Yes " ; else echo " No " ; ? >


"Tiling with Dominoes | PHP program to find no . of ways to fill a 3 xn board with 2 x1 dominoes . ; Driver Code"
How can the above be solved in PHP?


"Time required to meet in equilateral triangle | function to calculate time to meet ; Driver Code"
How can the above be solved in PHP?

< ? php function timeToMeet ( $ s , $ v ) { $ V = 3 * $ v / 2 ; $ time = $ s / $ V ; echo $ time ; } $ s = 25 ; $ v = 56 ; timeToMeet ( $ s , $ v ) ; ? >


"Time taken by two persons to meet on a circular track | PHP implementation of above approach ; Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { return $ b ? gcd ( $ b , $ a % $ b ) : $ a ; } function startingPoint ( $ Length , $ Speed1 , $ Speed2 ) { $ result1 = 0 ; $ result2 = 0 ; $ time1 = $ Length / $ Speed1 ; $ time2 = $ Length / $ Speed2 ; $ result1 = gcd ( $ time1 , $ time2 ) ; $ result2 = $ time1 * $ time2 / ( $ result1 ) ; return $ result2 ; } function firstTime ( $ Length , $ Speed1 , $ Speed2 ) { $ result = 0 ; $ relativeSpeed = abs ( $ Speed1 - $ Speed2 ) ; $ result = ( ( float ) $ Length / $ relativeSpeed ) ; return $ result ; } $ L = 30 ; $ S1 = 5 ; $ S2 = 2 ; $ first_Time = firstTime ( $ L , $ S1 , $ S2 ) ; $ starting_Point = startingPoint ( $ L , $ S1 , $ S2 ) ; echo " Met ▁ first ▁ time ▁ after ▁ " . $ first_Time . " ▁ hrs " . "
 " ; echo " Met ▁ at ▁ starting ▁ point ▁ after ▁ " . $ starting_Point . " ▁ hrs " . "
 " ; ? >


"Time until distance gets equal to X between two objects moving in opposite direction | Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code"
How can the above be solved in PHP?

< ? php function getTime ( $ u , $ v , $ x ) { $ speed = $ u + $ v ; $ time = $ x / $ speed ; return $ time ; } $ u = 3 ; $ v = 3 ; $ x = 3 ; echo getTime ( $ u , $ v , $ x ) ; ? >


"Time when minute hand and hour hand coincide | function to find the minute ; finding the angle between minute hand and the first hour hand ; Driver code"
How can the above be solved in PHP?

< ? php function find_time ( $ h1 ) { $ theta = 30 * $ h1 ; echo ( " ( " . ( $ theta * 2 ) . " / " ▁ . ▁ " 11 " ▁ . ▁ " ) " ▁ . ▁ " minutes " } $ h1 = 3 ; find_time ( $ h1 ) ; ? >


"Times required by Simple interest for the Principal to become Y times itself | Function to return the no . of years ; Driver code"
How can the above be solved in PHP?


"To check a number is palindrome or not without using any extra space | Function to check if given number is palindrome or not without using the extra space ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Driver code"
How can the above be solved in PHP?

< ? php function isPalindrome ( $ n ) { $ divisor = 1 ; while ( $ n / $ divisor >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = floor ( $ n / $ divisor ) ; $ trailing = $ n % 10 ; if ( $ leading != $ trailing ) return false ; $ n = ( $ n % $ divisor ) / 10 ; $ divisor = $ divisor / 100 ; } return true ; } if ( isPalindrome ( 1001 ) == true ) echo " Yes , ▁ it ▁ is ▁ Palindrome " ; else echo " No , ▁ not ▁ Palindrome " ; ? >


"To check divisibility of any large number by 999 | function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; Driver Code"
How can the above be solved in PHP?

< ? php function isDivisible999 ( $ num ) { $ n = strlen ( $ num ) ; if ( $ n == 0 && $ num [ 0 ] == '0' ) return true ; if ( $ n % 3 == 1 ) $ num = "00" . $ num ; if ( $ n % 3 == 2 ) $ num = "0" . $ num ; $ gSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 3 ) { $ group = 0 ; $ group += ( ord ( $ num [ $ i ] ) - 48 ) * 100 ; $ group += ( ord ( $ num [ $ i + 1 ] ) - 48 ) * 10 ; $ group += ( ord ( $ num [ $ i + 2 ] ) - 48 ) ; $ gSum += $ group ; } if ( $ gSum > 1000 ) { $ num = strval ( $ gSum ) ; $ n = strlen ( $ num ) ; $ gSum = isDivisible999 ( $ num ) ; } return ( $ gSum == 999 ) ; } $ num = "1998" ; if ( isDivisible999 ( $ num ) ) echo " Divisible " ; else echo " Not ▁ divisible " ; ? >


"Toggle all bits after most significant bit | Function to toggle bits starting from MSB ; temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code"
How can the above be solved in PHP?

< ? php function toggle ( & $ n ) { $ temp = 1 ; while ( $ temp <= $ n ) { $ n = $ n ^ $ temp ; $ temp = $ temp << 1 ; } } $ n = 10 ; toggle ( $ n ) ; echo $ n ; ? >


"Toggle all bits after most significant bit | Returns a number which has all set bits starting from MSB of n ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Driver Code"
How can the above be solved in PHP?

< ? php function setAllBitsAfterMSB ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return $ n ; } function toggle ( & $ n ) { $ n = $ n ^ setAllBitsAfterMSB ( $ n ) ; } $ n = 10 ; toggle ( $ n ) ; echo $ n ; ? >


"Toggle all even bits of a number | Returns a number which has all even bits of n toggled . ; Generate number form of 101010 . . till of same order as n ; if bit is even then generate number and or with res ; return toggled number ; Driver code"
How can the above be solved in PHP?

< ? php function evenbittogglenumber ( $ n ) { $ res = 0 ; $ count = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 1 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return $ n ^ $ res ; } $ n = 11 ; echo evenbittogglenumber ( $ n ) ; ? >


"Toggle all odd bits of a number | Returns a number which has all odd bits of n toggled . ; Generate number form of 101010. . . . . till of same order as n ; if bit is odd , then generate number and or with res ; return toggled number ; Driver code"
How can the above be solved in PHP?

< ? php function evenbittogglenumber ( $ n ) { $ res = 0 ; $ count = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 0 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return $ n ^ $ res ; } $ n = 11 ; echo evenbittogglenumber ( $ n ) ; ? >


"Toggle bits in the given range | function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' Besides this , we can calculate num as : $num = ( 1 << $r ) - $l . and return the number ; Driver Code"
How can the above be solved in PHP?

< ? php function toggleBitsFromLToR ( $ n , $ l , $ r ) { $ num = ( ( 1 << $ r ) - 1 ) ^ ( ( 1 << ( $ l - 1 ) ) - 1 ) ; return ( $ n ^ $ num ) ; } $ n = 50 ; $ l = 2 ; $ r = 5 ; echo toggleBitsFromLToR ( $ n , $ l , $ r ) ; ? >


"Toggle first and last bits of a number | Returns a number which has same bit count as n and has only first and last bits as set . ; set all the bit of the number ; Adding one to n now unsets all bits and moves MSB to one place . Now we shift the number by 1 and add 1. ; if number is 1 ; take XOR with first and last set bit number ; Driver code"
How can the above be solved in PHP?

< ? php function takeLandFsetbits ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( ( $ n + 1 ) >> 1 ) + 1 ; } function toggleFandLbits ( int $ n ) { if ( $ n == 1 ) return 0 ; return $ n ^ takeLandFsetbits ( $ n ) ; } $ n = 10 ; echo toggleFandLbits ( $ n ) ; ? >


"Toggle the last m bits | 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 ; Driver code"
How can the above be solved in PHP?

< ? php function toggleLastMBits ( $ n , $ m ) { $ num = ( 1 << $ m ) - 1 ; return ( $ n ^ $ num ) ; } { $ n = 107 ; $ m = 4 ; echo toggleLastMBits ( $ n , $ m ) ; return 0 ; } ? >


"Tomohiko Sakamoto 's Algorithm | function to implement tomohiko sakamoto algorithm ; array with leading number of days values ; if month is less than 3 reduce year by 1 ; Driver Code"
How can the above be solved in PHP?


"Total money to be paid after traveling the given number of hours | PHP implementation of the above approach ; calculating hours travelled"
How can the above be solved in PHP?


"Total no of 1 's in numbers | PHP code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code"
How can the above be solved in PHP?

 < ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ str = strval ( $ i ) ; $ countr += substr_count ( $ str , '1' ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) . "
 " ; $ n = 131 ; echo countDigitOne ( $ n ) . "
 " ; $ n = 159 ; echo countDigitOne ( $ n ) . "
 " ; ? >


"Total no of 1 's in numbers | function to count the frequency of 1. ; Driver Code"
How can the above be solved in PHP?

 < ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i *= 10 ) { $ divider = $ i * 10 ; $ countr += ( int ) ( $ n / $ divider ) * $ i + min ( max ( $ n % $ divider - $ i + 1 , 0 ) , $ i ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) , "
 " ; $ n = 113 ; echo countDigitOne ( $ n ) , "
 " ; $ n = 205 ; echo countDigitOne ( $ n ) , "
 " ; ? >


"Total number of different staircase that can made from N boxes | Function to find the total number of different staircase that can made from N boxes ; Initialize all the elements of the table to zero ; Base case ; When step is equal to 2 ; When step is greater than 2 ; Count the total staircase from all the steps ; Driver Code"
How can the above be solved in PHP?


"Total number of divisors for a given number | program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Driver Code"
How can the above be solved in PHP?


"Total number of non | PHP program to count non - decreasing number with n digits ; dp [ i ] [ j ] contains total count of non decreasing numbers ending with digit i and of length j ; Fill table for non decreasing numbers of length 1 Base cases 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ; Fill the table in bottom - up manner ; Compute total numbers of non decreasing numbers of length ' len ' ; sum of all numbers of length of len - 1 in which last digit x is <= ' digit ' ; There total nondecreasing numbers of length n wiint be dp [ 0 ] [ n ] + dp [ 1 ] [ n ] . . + dp [ 9 ] [ n ] ; Driver program"
How can the above be solved in PHP?

< ? php function countNonDecreasing ( $ n ) { $ dp = array_fill ( 0 , 10 , array_fill ( 0 , $ n + 1 , NULL ) ) ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ dp [ $ i ] [ 1 ] = 1 ; for ( $ digit = 0 ; $ digit <= 9 ; $ digit ++ ) { for ( $ len = 2 ; $ len <= $ n ; $ len ++ ) { for ( $ x = 0 ; $ x <= $ digit ; $ x ++ ) $ dp [ $ digit ] [ $ len ] += $ dp [ $ x ] [ $ len - 1 ] ; } } $ count = 0 ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ count += $ dp [ $ i ] [ $ n ] ; return $ count ; } $ n = 3 ; echo countNonDecreasing ( $ n ) ; return 0 ; ? >


"Total number of non | PHP program to count non - decreasing numner with n digits ; Compute value of N * ( N + 1 ) / 2 * ( N + 2 ) / 3 * ... ... . * ( N + n - 1 ) / n ; Driver Code"
How can the above be solved in PHP?

< ? php function countNonDecreasing ( $ n ) { $ N = 10 ; $ count = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ count *= ( $ N + $ i - 1 ) ; $ count /= $ i ; } return $ count ; } $ n = 3 ; echo countNonDecreasing ( $ n ) ; ? >


"Total number of possible Binary Search Trees and Binary Trees with n keys | A function to find factorial of a given number ; Calculate value of [ 1 * ( 2 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; A function to count number of BST with n nodes using catalan ; find nth catalan number ; return nth catalan number ; A function to count number of binary trees with n nodes ; find count of BST with n numbers ; return count * n ! ; Driver Code ; find count of BST and binary trees with n nodes ; print count of BST and binary trees with n nodes"
How can the above be solved in PHP?

 < ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ res *= $ i ; } return $ res ; } function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res = ( int ) $ res / ( $ i + 1 ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * $ n , $ n ) ; return ( int ) $ c / ( $ n + 1 ) ; } function countBST ( $ n ) { $ count = catalan ( $ n ) ; return $ count ; } function countBT ( $ n ) { $ count = catalan ( $ n ) ; return $ count * factorial ( $ n ) ; } $ count1 ; $ count2 ; $ n = 5 ; $ count1 = countBST ( $ n ) ; $ count2 = countBT ( $ n ) ; echo " Count ▁ of ▁ BST ▁ with ▁ " , $ n , " ▁ nodes ▁ is ▁ " , $ count1 , "
 " ; echo " Count ▁ of ▁ binary ▁ trees ▁ with ▁ " , $ n , " ▁ nodes ▁ is ▁ " , $ count2 ; ? >


"Total number of triangles formed when there are H horizontal and V vertical lines | Function to return total triangles ; Only possible triangle is the given triangle ; If only vertical lines are present ; If only horizontal lines are present ; Return total triangles ; Driver code"
How can the above be solved in PHP?

< ? php function totalTriangles ( $ h , $ v ) { if ( $ h == 0 && $ v == 0 ) return 1 ; if ( $ h == 0 ) return ( ( $ v + 1 ) * ( $ v + 2 ) / 2 ) ; if ( $ v == 0 ) return ( $ h + 1 ) ; $ Total = ( $ h + 1 ) * ( ( $ v + 1 ) * ( $ v + 2 ) / 2 ) ; return $ Total ; } $ h = 2 ; $ v = 2 ; echo totalTriangles ( $ h , $ v ) ; ? >


"Total number of ways to place X and Y at n places such that no two X are together | Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; total number of places"
How can the above be solved in PHP?

< ? php function ways ( $ n ) { $ first = 2 ; $ second = 3 ; $ res = 0 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ res = $ first + $ second ; $ first = $ second ; $ second = $ res ; } return $ res ; } $ n = 7 ; echo " Total ▁ ways ▁ are : ▁ " , ways ( $ n ) ; ? >


"Total numbers with no repeated digits in a range | Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to find total number in the given range which has no repeated digit ; Traversing through the range ; Add 1 to the answer if i has no repeated digit else 0 ; Driver Code ; Calling the calculate"
How can the above be solved in PHP?

< ? php function repeated_digit ( $ n ) { $ c = 10 ; $ a = array_fill ( 0 , $ c , 0 ) ; while ( $ n > 0 ) { $ d = $ n % 10 ; if ( $ a [ $ d ] > 0 ) { return 0 ; } $ a [ $ d ] ++ ; $ n = ( int ) ( $ n / 10 ) ; } return 1 ; } function calculate ( $ L , $ R ) { $ answer = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { $ answer += repeated_digit ( $ i ) ; } return $ answer ; } $ L = 1 ; $ R = 100 ; echo calculate ( $ L , $ R ) ; ? >


"Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | Function to find the count of required pairs ; To store the count of elements which give remainder 0 i . e . even values ; To store the count of elements which give remainder 1 i . e . odd values ; Driver code"
How can the above be solved in PHP?

 < ? php function CalculatePairs ( $ a , $ n ) { $ cnt_zero = 0 ; $ cnt_one = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 == 0 ) $ cnt_zero += 1 ; else $ cnt_one += 1 ; } $ total_XOR_pairs = $ cnt_zero * $ cnt_one ; $ total_AND_pairs = ( $ cnt_one ) * ( $ cnt_one - 1 ) / 2 ; $ total_OR_pairs = $ cnt_zero * $ cnt_one + ( $ cnt_one ) * ( $ cnt_one - 1 ) / 2 ; echo ( " cntXOR ▁ = ▁ $ total _ XOR _ pairs
 " ) ; echo ( " cntAND ▁ = ▁ $ total _ AND _ pairs
 " ) ; echo ( " cntOR ▁ = ▁ $ total _ OR _ pairs
 " ) ; } $ a = array ( 1 , 3 , 4 , 2 ) ; $ n = count ( $ a ) ; CalculatePairs ( $ a , $ n ) ; ? >


"Total position where king can reach on a chessboard in exactly M moves | Function to return the number of squares that the king can reach in the given number of moves ; Calculate initial and final coordinates ; Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8. ; Calculate total positions ; Driver code"
How can the above be solved in PHP?


"Trailing number of 0 s in product of two factorials | Returns number of zeros in factorial n ; dividing x by powers of 5 and update count ; Returns count of trailing zeros in M ! x N ! ; Driver Code"
How can the above be solved in PHP?

< ? php function trailingZero ( $ x ) { $ i = 5 ; $ count = 0 ; while ( $ x > $ i ) { $ count = $ count + ( int ) ( $ x / $ i ) ; $ i = $ i * 5 ; } return $ count ; } function countProductTrailing ( $ M , $ N ) { return trailingZero ( $ N ) + trailingZero ( $ M ) ; } $ N = 67 ; $ M = 98 ; echo ( countProductTrailing ( $ N , $ M ) ) ; ? >


"Transform a string such that it has abcd . . z as a subsequence | function to transform string with string passed as reference ; initializing the variable ch to ' a ' ; if the length of string is less than 26 , we can 't obtain the required  subsequence ; if ch has reached ' z ' , it means we have transformed our string such that required subsequence can be obtained ; current character is not greater than ch , then replace it with ch and increment ch ; Driver Code"
How can the above be solved in PHP?

< ? php function transformString ( & $ s ) { $ ch = " a " ; if ( strlen ( $ s ) < 26 ) return false ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ord ( $ ch ) > ord ( " z " ) ) break ; if ( $ s [ $ i ] <= $ ch ) { $ s [ $ i ] = $ ch ; $ ch = chr ( ord ( $ ch ) + 1 ) ; } } if ( $ ch <= " z " ) return false ; return true ; } $ str = " aaaaaaaaaaaaaaaaaaaaaaaaaa " ; if ( transformString ( $ str ) ) echo $ str ; else echo " Not ▁ Possible " ; ? >


"Trapezoidal Rule for Approximate Value of Definite Integral | A sample function whose definite integral 's approximate value is computed using Trapezoidal rule ; Declaring the function f ( x ) = 1 / ( 1 + x * x ) ; Function to evaluate the value of integral ; Grid spacing ; Computing sum of first and last terms in above formula ; Adding middle terms in above formula ; h / 2 indicates ( b - a ) / 2 n . Multiplying h / 2 with s . ; Range of definite integral ; Number of grids . Higher value means more accuracy"
How can the above be solved in PHP?

< ? php function y ( $ x ) { return 1 / ( 1 + $ x * $ x ) ; } function trapezoidal ( $ a , $ b , $ n ) { $ h = ( $ b - $ a ) / $ n ; $ s = y ( $ a ) + y ( $ b ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ s += 2 * Y ( $ a + $ i * $ h ) ; return ( $ h / 2 ) * $ s ; } $ x0 = 0 ; $ xn = 1 ; $ n = 6 ; echo ( " Value ▁ of ▁ integral ▁ is ▁ " ) ; echo ( trapezoidal ( $ x0 , $ xn , $ n ) ) ; ? >


"Trapping Rain Water | Method to find maximum amount of water that can be trapped within given set of bars . ; initialize output ; maximum element on left and right ; indices to traverse the array ; update max in left ; water on curr element = max - curr ; update right maximum ; Driver Code"
How can the above be solved in PHP?

< ? php function findWater ( $ arr , $ n ) { $ result = 0 ; $ left_max = 0 ; $ right_max = 0 ; $ lo = 0 ; $ hi = $ n - 1 ; while ( $ lo <= $ hi ) { if ( $ arr [ $ lo ] < $ arr [ $ hi ] ) { if ( $ arr [ $ lo ] > $ left_max ) $ left_max = $ arr [ $ lo ] ; else $ result += $ left_max - $ arr [ $ lo ] ; $ lo ++ ; } else { if ( $ arr [ $ hi ] > $ right_max ) $ right_max = $ arr [ $ hi ] ; else $ result += $ right_max - $ arr [ $ hi ] ; $ hi -- ; } } return $ result ; } $ arr = array ( 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " , findWater ( $ arr , $ n ) ; ? >


"Trapping Rain Water | PHP program to find maximum amount of water that can be trapped within given set of bars . ; Initialize result ; Fill left array ; Fill right array ; Calculate the accumulated water element by element consider the amount of water on i 'th bar, the  amount of water accumulated  on this particular  bar will be equal to min(left[i], right[i]) - arr[i] . ; Driver program"
How can the above be solved in PHP?

< ? php function findWater ( $ arr , $ n ) { $ water = 0 ; $ left [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ left [ $ i ] = max ( $ left [ $ i - 1 ] , $ arr [ $ i ] ) ; $ right [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) $ right [ $ i ] = max ( $ right [ $ i + 1 ] , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ water += min ( $ left [ $ i ] , $ right [ $ i ] ) - $ arr [ $ i ] ; return $ water ; } $ arr = array ( 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " , findWater ( $ arr , $ n ) ; ? >


"Triangular Matchstick Number | PHP program to find X - th triangular matchstick number ; Driver code"
How can the above be solved in PHP?

< ? php function numberOfSticks ( $ x ) { return ( 3 * $ x * ( $ x + 1 ) ) / 2 ; } echo ( numberOfSticks ( 7 ) ) ; ? >


"Triangular Numbers | Returns true if ' num ' is triangular , else false ; Base case ; A Triangular number must be sum of first n natural numbers ; Driver code"
How can the above be solved in PHP?

< ? php function isTriangular ( $ num ) { if ( $ num < 0 ) return false ; $ sum = 0 ; for ( $ n = 1 ; $ sum <= $ num ; $ n ++ ) { $ sum = $ sum + $ n ; if ( $ sum == $ num ) return true ; } return false ; } $ n = 55 ; if ( isTriangular ( $ n ) ) echo " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ; else echo " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ; ? >


"Triangular Numbers | Returns true if num is triangular ; Considering the equation n * ( n + 1 ) / 2 = num The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Driver code"
How can the above be solved in PHP?

< ? php function isTriangular ( $ num ) { if ( $ num < 0 ) return false ; $ c = ( -2 * $ num ) ; $ b = 1 ; $ a = 1 ; $ d = ( $ b * $ b ) - ( 4 * $ a * $ c ) ; if ( $ d < 0 ) return false ; $ root1 = ( - $ b + ( float ) sqrt ( $ d ) ) / ( 2 * $ a ) ; $ root2 = ( - $ b - ( float ) sqrt ( $ d ) ) / ( 2 * $ a ) ; if ( $ root1 > 0 && floor ( $ root1 ) == $ root1 ) return true ; if ( $ root2 > 0 && floor ( $ root2 ) == $ root2 ) return true ; return false ; } $ num = 55 ; if ( isTriangular ( $ num ) ) echo ( " The ▁ number ▁ is " . " ▁ a ▁ triangular ▁ number " ) ; else echo ( " The ▁ number ▁ " . " is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; ? >


"Tribonacci Numbers | A DP based PHP program to print first n Tribonacci numbers . ; Driver code"
How can the above be solved in PHP?


"Tribonacci Numbers | A space optimized based PHP program to print first n Tribinocci numbers . | ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code"
How can the above be solved in PHP?

< ? php function printTrib ( $ n ) { if ( $ n < 1 ) return ; $ first = 0 ; $ second = 0 ; $ third = 1 ; echo $ first , " " ; if ( $ n > 1 ) echo $ second , " ▁ " ; if ( $ n > 2 ) echo $ second , " ▁ " ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) { $ curr = $ first + $ second + $ third ; $ first = $ second ; $ second = $ third ; $ third = $ curr ; echo $ curr , " " ; } } $ n = 10 ; printTrib ( $ n ) ; ? >


"Tribonacci Numbers | Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; $T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code"
How can the above be solved in PHP?


"Trimorphic Number | Function to check Trimorphic number ; Store the cube ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce N and cube ; Driver code"
How can the above be solved in PHP?

< ? php function isTrimorphic ( $ N ) { $ cube = $ N * $ N * $ N ; while ( $ N > 0 ) { if ( $ N % 10 != $ cube % 10 ) return -1 ; $ N /= 10 ; $ cube /= 10 ; } return 1 ; } $ N = 24 ; $ r = isTrimorphic ( $ N ) ? " trimorphic " : " not ▁ trimporphic " ; echo $ r ; ? >


"Trimorphic Number | Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max int size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver code"
How can the above be solved in PHP?

< ? php function checkTrimorphic ( $ num ) { $ cube = $ num * $ num * $ num ; while ( $ num > 0 ) { if ( $ num % 10 != $ cube % 10 ) return false ; $ num = ( int ) ( $ num / 10 ) ; $ cube = ( int ) ( $ cube / 10 ) ; } return true ; } function nthTrimorphic ( $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < PHP_INT_MAX ; $ i ++ ) { if ( checkTrimorphic ( $ i ) ) $ count ++ ; if ( $ count == $ n ) return $ i ; } } $ n = 9 ; echo nthTrimorphic ( $ n ) ; ? >


"Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; check if n - 2 is divisible by 3 or not ; Driver code"
How can the above be solved in PHP?

< ? php function printCombination ( $ n ) { echo "1 ▁ " ; if ( ( $ n - 2 ) % 3 == 0 ) echo "2 ▁ " . ( $ n - 3 ) ; else echo "1 ▁ " . ( $ n - 2 ) ; } $ n = 233 ; printCombination ( $ n ) ; ? >


"Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; Check for 3 rd number ; Driver Code"
How can the above be solved in PHP?


"Tug of War | solution by calling itself recursively ; checks whether the it is going out of bound ; checks that the numbers of elements left are not less than the number of elements required to form the solution ; consider the cases when current element is not included in the solution ; add the current element to the solution ; checks if a solution is formed ; checks if the solution formed is better than the best solution so far ; consider the cases where current element is included in the solution ; removes current element before returning to the caller of this function ; main function that generate an arr ; the boolean array that contains the inclusion and exclusion of an element in current set . The number excluded automatically form the other set ; The inclusion / exclusion array for final solution ; Find the solution using recursive function TOWUtil ( ) ; Print the solution ; Driver Code"
How can the above be solved in PHP?

< ? php function TOWUtil ( & $ arr , $ n , & $ curr_elements , $ no_of_selected_elements , & $ soln , & $ min_diff , $ sum , $ curr_sum , $ curr_position ) { if ( $ curr_position == $ n ) return ; if ( ( intval ( $ n / 2 ) - $ no_of_selected_elements ) > ( $ n - $ curr_position ) ) return ; TOWUtil ( $ arr , $ n , $ curr_elements , $ no_of_selected_elements , $ soln , $ min_diff , $ sum , $ curr_sum , $ curr_position + 1 ) ; $ no_of_selected_elements ++ ; $ curr_sum = ( $ curr_sum + $ arr [ $ curr_position ] ) ; $ curr_elements [ $ curr_position ] = true ; if ( $ no_of_selected_elements == intval ( $ n / 2 ) ) { if ( abs ( intval ( $ sum / 2 ) - $ curr_sum ) < $ min_diff ) { $ min_diff = abs ( intval ( $ sum / 2 ) - $ curr_sum ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ soln [ $ i ] = $ curr_elements [ $ i ] ; } } else { TOWUtil ( $ arr , $ n , $ curr_elements , $ no_of_selected_elements , $ soln , $ min_diff , $ sum , $ curr_sum , $ curr_position + 1 ) ; } $ curr_elements [ $ curr_position ] = false ; } function tugOfWar ( & $ arr , $ n ) { $ curr_elements = array_fill ( 0 , $ n , 0 ) ; $ soln = array_fill ( 0 , $ n , 0 ) ; $ min_diff = PHP_INT_MAX ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ curr_elements [ $ i ] = $ soln [ $ i ] = false ; } TOWUtil ( $ arr , $ n , $ curr_elements , 0 , $ soln , $ min_diff , $ sum , 0 , 0 ) ; echo " The ▁ first ▁ subset ▁ is : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ soln [ $ i ] == true ) echo $ arr [ $ i ] . " ▁ " ; } echo " The second subset is : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ soln [ $ i ] == false ) echo $ arr [ $ i ] . " ▁ " ; } } $ arr = array ( 23 , 45 , -34 , 12 , 0 , 98 , -99 , 4 , 189 , -1 , 4 ) ; $ n = count ( $ arr ) ; tugOfWar ( $ arr , $ n ) ; ? >


"Turn off the rightmost set bit | unsets the rightmost set bit of n and returns the result ; Driver Code"
How can the above be solved in PHP?

< ? php function fun ( $ n ) { return $ n & ( $ n - 1 ) ; } $ n = 7 ; echo " The ▁ number ▁ after ▁ unsetting ▁ the " . " ▁ rightmost ▁ set ▁ bit ▁ " , fun ( $ n ) ; ? >


"Twin Prime Numbers between 1 and n | PHP program to print all twin primes using Sieve of Eratosthenes . ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; to check for twin prime numbers display the twin primes ; Driver Code ; Calling the function"
How can the above be solved in PHP?


"Twin Prime Numbers | Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver code"
How can the above be solved in PHP?

 < ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function twinPrime ( $ n1 , $ n2 ) { return ( isPrime ( $ n1 ) && isPrime ( $ n2 ) && abs ( $ n1 - $ n2 ) == 2 ) ; } $ n1 = 11 ; $ n2 = 13 ; if ( twinPrime ( $ n1 , $ n2 ) ) echo " Twin ▁ Prime " , "
 " ; else echo "
 " , " Not ▁ Twin ▁ Prime " , "
 " ; ? >


"Two elements whose sum is closest to zero | PHP program to find the Two elements whose sum is closest to zero ; Array should have at least two elements ; Initialization of values ; Driver Code"
How can the above be solved in PHP?

< ? php function minAbsSumPair ( $ arr , $ arr_size ) { $ inv_count = 0 ; if ( $ arr_size < 2 ) { echo " Invalid ▁ Input " ; return ; } $ min_l = 0 ; $ min_r = 1 ; $ min_sum = $ arr [ 0 ] + $ arr [ 1 ] ; for ( $ l = 0 ; $ l < $ arr_size - 1 ; $ l ++ ) { for ( $ r = $ l + 1 ; $ r < $ arr_size ; $ r ++ ) { $ sum = $ arr [ $ l ] + $ arr [ $ r ] ; if ( abs ( $ min_sum ) > abs ( $ sum ) ) { $ min_sum = $ sum ; $ min_l = $ l ; $ min_r = $ r ; } } } echo " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are ▁ " . $ arr [ $ min_l ] . " ▁ and ▁ " . $ arr [ $ min_r ] ; } $ arr = array ( 1 , 60 , -10 , 70 , -80 , 85 ) ; minAbsSumPair ( $ arr , 6 ) ; ? >


"Two odd occurring elements in an array where all other occur even times | PHP code to find two odd occurring elements in an array where all other elements appear even number of times . ; Find XOR of all numbers ; Find a set bit in the XOR ( We find rightmost set bit here ) ; Traverse through all numbers and divide them in two groups ( i ) Having set bit set at same position as the only set bit in set_bit ( ii ) Having 0 bit at same position as the only set bit in set_bit ; XOR of two different sets are our required numbers . ; Driver code"
How can the above be solved in PHP?


"Ugly Numbers | This function divides a by greatest divisible power of b ; Function to check if a number is ugly or not ; Function to get the nth ugly number ; ugly number count ; Check for all integers untill ugly count becomes n ; Driver Code"
How can the above be solved in PHP?

< ? php function maxDivide ( $ a , $ b ) { while ( $ a % $ b == 0 ) $ a = $ a / $ b ; return $ a ; } function isUgly ( $ no ) { $ no = maxDivide ( $ no , 2 ) ; $ no = maxDivide ( $ no , 3 ) ; $ no = maxDivide ( $ no , 5 ) ; return ( $ no == 1 ) ? 1 : 0 ; } function getNthUglyNo ( $ n ) { $ i = 1 ; $ count = 1 ; while ( $ n > $ count ) { $ i ++ ; if ( isUgly ( $ i ) ) $ count ++ ; } return $ i ; } $ no = getNthUglyNo ( 150 ) ; echo "150th ▁ ugly ▁ no . ▁ is ▁ " . $ no ; ? >


"Unbounded Knapsack ( Repetition of items allowed ) | Returns the maximum value with knapsack of W capacity ; dp [ i ] is going to store maximum value with knapsack capacity i . ; Fill dp [ ] using above recursive formula ; Driver Code"
How can the above be solved in PHP?

< ? php function unboundedKnapsack ( $ W , $ n , $ val , $ wt ) { for ( $ i = 0 ; $ i <= $ W ; $ i ++ ) $ dp [ $ i ] = 0 ; $ ans = 0 ; for ( $ i = 0 ; $ i <= $ W ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ wt [ $ j ] <= $ i ) $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ i - $ wt [ $ j ] ] + $ val [ $ j ] ) ; return $ dp [ $ W ] ; } $ W = 100 ; $ val = array ( 10 , 30 , 20 ) ; $ wt = array ( 5 , 10 , 15 ) ; $ n = count ( $ val ) ; sizeof ( $ val ) / sizeof ( $ val [ 0 ] ) ; echo unboundedKnapsack ( $ W , $ n , $ val , $ wt ) ; ? >


"Union and Intersection of two sorted arrays | Function prints Intersection of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Driver Code ; Function calling"
How can the above be solved in PHP?

< ? php function printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) { $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ arr1 [ $ i ] < $ arr2 [ $ j ] ) $ i ++ ; else if ( $ arr2 [ $ j ] < $ arr1 [ $ i ] ) $ j ++ ; else { echo $ arr2 [ $ j ] , " " ; $ i ++ ; $ j ++ ; } } } $ arr1 = array ( 1 , 2 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 5 , 7 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >


"Union and Intersection of two sorted arrays | Function prints union of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Print remaining elements of the larger array ; Driver Code"
How can the above be solved in PHP?


"Unique cells in a binary matrix | PHP program to count unique cells in a matrix ; Returns true if mat [ i ] [ j ] is unique ; checking in row calculating sumrow will be moving column wise ; checking in column calculating sumcol will be moving row wise ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function isUnique ( $ mat , $ i , $ j , $ n , $ m ) { global $ MAX ; $ sumrow = 0 ; for ( $ k = 0 ; $ k < $ m ; $ k ++ ) { $ sumrow += $ mat [ $ i ] [ $ k ] ; if ( $ sumrow > 1 ) return false ; } $ sumcol = 0 ; for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { $ sumcol += $ mat [ $ k ] [ $ j ] ; if ( $ sumcol > 1 ) return false ; } return true ; } function countUnique ( $ mat , $ n , $ m ) { $ uniquecount = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] && isUnique ( $ mat , $ i , $ j , $ n , $ m ) ) $ uniquecount ++ ; return $ uniquecount ; } $ mat = array ( array ( 0 , 1 , 0 , 0 ) , array ( 0 , 0 , 1 , 0 ) , array ( 1 , 0 , 0 , 1 ) ) ; echo countUnique ( $ mat , 3 , 4 ) ; ? >


"Unique element in an array where all elements occur k times except one | PHP program to find unique element where every element appears k times except one ; Create a count array to store count of numbers that have a particular bit set . count [ i ] stores count of array elements with i - th bit set . ; AND ( bitwise ) each element of the array with each set digit ( one at a time ) to get the count of set bits at each position ; Now consider all bits whose count is not multiple of k to form the required number . ; Driver Code"
How can the above be solved in PHP?


"Value of k | PHP program to fin k - th element after append and insert middle operations ; Middle element of the sequence ; length of the resulting sequence . ; Updating the middle element of next sequence ; Moving to the left side of the middle element . ; Moving to the right side of the middle element . ; Driver code"
How can the above be solved in PHP?

 < ? php function findElement ( $ n , $ k ) { $ ans = $ n ; $ left = 1 ; $ right = pow ( 2 , $ n ) - 1 ; while ( 1 ) { $ mid = ( $ left + $ right ) / 2 ; if ( $ k == $ mid ) { echo $ ans , "
 " ; break ; } $ ans -- ; if ( $ k < $ id ) $ right = $ mid - 1 ; else $ left = $ mid + 1 ; } } $ n = 4 ; $ k = 8 ; findElement ( $ n , $ k ) ; ? >


"Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code"
How can the above be solved in PHP?

< ? php function fnMod ( $ n ) { $ rem = $ n % 4 ; if ( $ rem == 0 or $ rem == 3 ) return 0 ; else if ( $ rem == 1 or $ rem == 2 ) return 1 ; } $ n = 6 ; echo fnMod ( $ n ) ; ? >


"Variance and standard | variance function declaration Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation"
How can the above be solved in PHP?


"Variation in Nim Game | Function to return final grundy Number ( G ) of game ; if pile size is odd ; We XOR pile size + 1 ; We XOR pile size - 1 ; Game with 3 piles ; pile with different sizes ; Function to return result of game ; if ( $res == 0 ) if G is zero ; else if G is non zero"
How can the above be solved in PHP?


"Volume of biggest sphere within a right circular cylinder | Function to find the biggest sphere ; radius and height cannot be negative ; radius of sphere ; Driver code"
How can the above be solved in PHP?

< ? php function sph ( $ r , $ h ) { if ( $ r < 0 && $ h < 0 ) return -1 ; $ R = $ r ; return $ R ; } $ r = 4 ; $ h = 8 ; echo sph ( $ r , $ h ) ; ? >


"Volume of cube using its space diagonal | Function to calculate Volume ; Formula to find Volume ; space diagonal of Cube"
How can the above be solved in PHP?

< ? php function CubeVolume ( $ d ) { $ Volume ; $ Volume = ( sqrt ( 3 ) * pow ( $ d , 3 ) ) / 9 ; return $ Volume ; } $ d = 5 ; echo " Volume ▁ of ▁ Cube : ▁ " , CubeVolume ( $ d ) ; ? >


"Volume of largest right circular cylinder within a Sphere | Function to find the biggest right circular cylinder ; radius cannot be negative ; volume of cylinder ; Driver code"
How can the above be solved in PHP?


"Ways of dividing a group into two halves such that two elements are in different groups | This function will return the factorial of a given number ; This function will calculate nCr of given n and r ; This function will Calculate number of ways ; Driver Code"
How can the above be solved in PHP?

 < ? php function factorial ( $ n ) { $ result = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ result = $ result * $ i ; return $ result ; } function nCr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ r ) * factorial ( $ n - $ r ) ) ; } function calculate_result ( $ n ) { $ result = 2 * nCr ( ( $ n - 2 ) , ( $ n / 2 - 1 ) ) ; return $ result ; } $ a = 2 ; $ b = 4 ; echo calculate_result ( 2 * $ a ) . "
 " ; echo calculate_result ( 2 * $ b ) . "
 " ; ? >


"Ways of filling matrix such that product of all rows and all columns are equal to unity | PHP program to find number of ways to fill a matrix under given constraints ; Returns a raised power t under modulo mod ; Counting number of ways of filling the matrix ; Function calculating the answer ; if sum of numbers of rows and columns is odd i . e ( n + m ) % 2 == 1 and k = - 1 then there are 0 ways of filiing the matrix . ; If there is one row or one column then there is only one way of filling the matrix ; If the above cases are not followed then we find ways to fill the n - 1 rows and m - 1 columns which is 2 ^ ( ( m - 1 ) * ( n - 1 ) ) . ; Driver Code"
How can the above be solved in PHP?

< ? php $ mod = 100000007 ; function modPower ( $ a , $ t ) { global $ mod ; $ now = $ a ; $ ret = 1 ; while ( $ t ) { if ( $ t & 1 ) $ ret = $ now * ( $ ret % $ mod ) ; $ now = $ now * ( $ now % $ mod ) ; $ t >>= 1 ; } return $ ret ; } function countWays ( $ n , $ m , $ k ) { global $ mod ; if ( $ k == -1 and ( $ n + $ m ) % 2 == 1 ) return 0 ; if ( $ n == 1 or $ m == 1 ) return 1 ; return ( modPower ( modPower ( 2 , $ n - 1 ) , $ m - 1 ) % $ mod ) ; } $ n = 2 ; $ m = 7 ; $ k = 1 ; echo countWays ( $ n , $ m , $ k ) ; ? >


"Ways of selecting men and women from a group to make a team | Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code"
How can the above be solved in PHP?

< ? php function fact ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact *= $ i ; return $ fact ; } function ncr ( $ n , $ r ) { $ ncr = ( int ) ( fact ( $ n ) / ( fact ( $ r ) * fact ( $ n - $ r ) ) ) ; return $ ncr ; } function ways ( $ m , $ w , $ n , $ k ) { $ ans = 0 ; while ( $ m >= $ k ) { $ ans += ncr ( $ m , $ k ) * ncr ( $ w , $ n - $ k ) ; $ k += 1 ; } return $ ans ; } $ m = 7 ; $ w = 6 ; $ n = 5 ; $ k = 3 ; echo ways ( $ m , $ w , $ n , $ k ) ;


"Ways to arrange Balls such that adjacent balls are of different types | PHP program to count number of ways to arrange three types of balls such that no two balls of same color are adjacent to each other ; table to store to store results of subproblems ; Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; If this subproblem is already evaluated ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q Balls and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; else ( last == 2 ) ; Returns count of required arrangements ; Three cases arise : return countWays ( $p , $q , $r , 0 ) + Last required balls is type P countWays ( $p , $q , $r , 1 ) + Last required balls is type Q countWays ( $p , $q , $r , 2 ) ; Last required balls is type R ; Driver code"
How can the above be solved in PHP?


"Ways to arrange Balls such that adjacent balls are of different types | Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q Balls and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; Returns count of required arrangements ; Three cases arise : Last required balls is type P Last required balls is type Q Last required balls is type R ; Driver Code"
How can the above be solved in PHP?

< ? php function countWays ( $ p , $ q , $ r , $ last ) { if ( $ p < 0 $ q < 0 $ r < 0 ) return 0 ; if ( $ p == 1 && $ q == 0 && $ r == 0 && $ last == 0 ) return 1 ; if ( $ p == 0 && $ q == 1 && $ r == 0 && $ last == 1 ) return 1 ; if ( $ p == 0 && $ q == 0 && $ r == 1 && $ last == 2 ) return 1 ; if ( $ last == 0 ) return countWays ( $ p - 1 , $ q , $ r , 1 ) + countWays ( $ p - 1 , $ q , $ r , 2 ) ; if ( $ last == 1 ) return countWays ( $ p , $ q - 1 , $ r , 0 ) + countWays ( $ p , $ q - 1 , $ r , 2 ) ; if ( $ last == 2 ) return countWays ( $ p , $ q , $ r - 1 , 0 ) + countWays ( $ p , $ q , $ r - 1 , 1 ) ; } function countUtil ( $ p , $ q , $ r ) { return countWays ( $ p , $ q , $ r , 0 ) + countWays ( $ p , $ q , $ r , 1 ) + countWays ( $ p , $ q , $ r , 2 ) ; } $ p = 1 ; $ q = 1 ; $ r = 1 ; echo ( countUtil ( $ p , $ q , $ r ) ) ; ? >


"Ways to choose three points with distance between the most distant points <= L | Returns the number of triplets with distance between farthest points <= L ; sort to get ordered triplets so that we can find the distance between farthest points belonging to a triplet ; generate and check for all possible triplets : { arr [ i ] , arr [ j ] , arr [ k ] } ; Since the array is sorted the farthest points will be a [ i ] and a [ k ] ; ; set of n points on the X axis"
How can the above be solved in PHP?

 < ? php function countTripletsLessThanL ( $ n , $ L , $ arr ) { sort ( $ arr ) ; $ ways = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) { $ mostDistantDistance = $ arr [ $ k ] - $ arr [ $ i ] ; if ( $ mostDistantDistance <= $ L ) { $ ways ++ ; } } } } return $ ways ; } $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; $ L = 3 ; $ ans = countTripletsLessThanL ( $ n , $ L , $ arr ) ; echo " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " , $ ans , "
 " ; ? >


"Ways to color a skewed tree such that parent and child have different colors | fast_way is recursive method to calculate power ; Driver Code"
How can the above be solved in PHP?

< ? php function fastPow ( $ N , $ K ) { if ( $ K == 0 ) return 1 ; $ temp = fastPow ( $ N , $ K / 2 ) ; if ( $ K % 2 == 0 ) return $ temp * $ temp ; else return $ N * $ temp * $ temp ; } function countWays ( $ N , $ K ) { return $ K * fastPow ( $ K - 1 , $ N - 1 ) ; } $ N = 3 ; $ K = 3 ; echo countWays ( $ N , $ K ) ; ? >


"Ways to express a number as product of two different factors | To count number of ways in which number expressed as product of two different numbers ; To store count of such pairs ; Counting number of pairs upto sqrt ( n ) - 1 ; To return count of pairs ; Driver Code"
How can the above be solved in PHP?

 < ? php function countWays ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i * $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) $ count ++ ; return $ count ; } $ n = 12 ; echo countWays ( $ n ) , "
 " ; ? >


"Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors | PHP implementation of the approach ; Recursive function to find the required number of ways ; When all positions are filled ; If adjacent pairs are exactly K ; If already calculated ; Next position filled with same color ; Next position filled with different color So there can be m - 1 different colors ; Driver Code"
How can the above be solved in PHP?

< ? php $ GLOBALS [ ' max ' ] = 4 ; function countWays ( $ index , $ cnt , $ dp , $ n , $ m , $ k ) { if ( $ index == $ n ) { if ( $ cnt == $ k ) return 1 ; else return 0 ; } if ( $ dp [ $ index ] [ $ cnt ] != -1 ) return $ dp [ $ index ] [ $ cnt ] ; $ ans = 0 ; $ ans += countWays ( $ index + 1 , $ cnt , $ dp , $ n , $ m , $ k ) ; $ ans += ( $ m - 1 ) * countWays ( $ index + 1 , $ cnt + 1 , $ dp , $ n , $ m , $ k ) ; $ dp [ $ index ] [ $ cnt ] = $ ans ; return $ dp [ $ index ] [ $ cnt ] ; } $ n = 3 ; $ m = 3 ; $ k = 2 ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' max ' ] ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo $ m * countWays ( 1 , 0 , $ dp , $ n , $ m , $ k ) ; ? >


"Ways to form an array having integers in given range such that total sum is divisible by 2 | Function to return the number of ways to form an array of size n such that sum of all elements is divisible by 2 ; Represents first and last numbers of each type ( modulo 0 and 1 ) ; Count of numbers of each type between range ; Base Cases ; Ways to form array whose sum upto i numbers modulo 2 is 0 ; Ways to form array whose sum upto i numbers modulo 2 is 1 ; Return the required count of ways ; Driver Code"
How can the above be solved in PHP?


"Ways to place 4 items in n ^ 2 positions such that no row / column contains more than one | Function to return the number of ways to place 4 items in n ^ 2 positions ; Driver code"
How can the above be solved in PHP?


"Ways to place K bishops on an NÃ — N chessboard so that no two attack | returns the number of squares in diagonal i ; returns the number of ways to fill a n * n chessboard with k bishops so that no two bishops attack each other . ; return 0 if the number of valid places to be filled is less than the number of bishops ; dp table to store the values ; Setting the base conditions ; calculate the required number of ways ; stores the answer ; Driver code"
How can the above be solved in PHP?


"Ways to remove one element from a binary string so that XOR becomes zero | Return number of ways in which XOR become ZERO by remove 1 element ; Counting number of 0 and 1 ; If count of ones is even then return count of zero else count of one ; Driver Code"
How can the above be solved in PHP?

 < ? php function xorZero ( $ str ) { $ one_count = 0 ; $ zero_count = 0 ; $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ str [ $ i ] == '1' ) $ one_count ++ ; else $ zero_count ++ ; if ( $ one_count % 2 == 0 ) return $ zero_count ; return $ one_count ; } $ str = "11111" ; echo xorZero ( $ str ) , "
 " ; ? >


"Ways to sum to N using array elements with repetition allowed | function to count the total number of ways to sum up to ' N ' ; base case ; count ways for all values up to ' N ' and store the result ; if i >= arr [ j ] then accumulate count for value ' i ' as ways to form value ' i - arr [ j ] ' ; required number of ways ; Driver code"
How can the above be solved in PHP?

< ? php function countWays ( $ arr , $ m , $ N ) { $ count = array_fill ( 0 , $ N + 1 , 0 ) ; $ count [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ i >= $ arr [ $ j ] ) $ count [ $ i ] += $ count [ $ i - $ arr [ $ j ] ] ; return $ count [ $ N ] ; } $ arr = array ( 1 , 5 , 6 ) ; $ m = count ( $ arr ) ; $ N = 7 ; echo " Total ▁ number ▁ of ▁ ways ▁ = ▁ " , countWays ( $ arr , $ m , $ N ) ; ? >


"Woodall Number | PHP program to check if a number is Woodball or not . ; If number is even , return false . ; If x is 1 , return true . ; Add 1 to make x even ; While x is divisible by 2 ; Divide x by 2 ; Count the power ; If at any point power and x became equal , return true . ; Driver Code"
How can the above be solved in PHP?

< ? php function isWoodall ( $ x ) { if ( $ x % 2 == 0 ) return false ; if ( $ x == 1 ) return true ; $ x ++ ; $ p = 0 ; while ( $ x % 2 == 0 ) { $ x = $ x / 2 ; $ p ++ ; if ( $ p == $ x ) return true ; } return false ; } $ x = 383 ; if ( isWoodall ( $ x ) ) echo " Yes " ; else echo " No " ; ? >


"Write you own Power without using multiplication ( * ) and division ( / ) operators | A recursive function to get x * y ; A recursive function to get a ^ b Works only if a >= 0 and b >= 0 ; Driver Code"
How can the above be solved in PHP?

< ? php function multiply ( $ x , $ y ) { if ( $ y ) return ( $ x + multiply ( $ x , $ y - 1 ) ) ; else return 0 ; } function p_ow ( $ a , $ b ) { if ( $ b ) return multiply ( $ a , p_ow ( $ a , $ b - 1 ) ) ; else return 1 ; } echo pow ( 5 , 3 ) ; ? >


"Write you own Power without using multiplication ( * ) and division ( / ) operators | Works only if a >= 0 and b >= 0 ; Driver Code"
How can the above be solved in PHP?

< ? php function poww ( $ a , $ b ) { if ( $ b == 0 ) return 1 ; $ answer = $ a ; $ increment = $ a ; $ i ; $ j ; for ( $ i = 1 ; $ i < $ b ; $ i ++ ) { for ( $ j = 1 ; $ j < $ a ; $ j ++ ) { $ answer += $ increment ; } $ increment = $ answer ; } return $ answer ; } echo ( poww ( 5 , 3 ) ) ; ? >


"Writing power function for large numbers | This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver Code"
How can the above be solved in PHP?

< ? php function multiply ( $ x , $ res ) { $ carry = 0 ; $ res_size = count ( $ res ) ; for ( $ i = 0 ; $ i < $ res_size ; $ i ++ ) { $ prod = $ res [ $ i ] * $ x + $ carry ; $ res [ $ i ] = $ prod % 10 ; $ carry = ( int ) ( $ prod / 10 ) ; } while ( $ carry ) { if ( $ carry % 10 ) $ res [ $ res_size ++ ] = $ carry % 10 ; $ carry = ( int ) ( $ carry / 10 ) ; } return $ res ; } function power ( $ x , $ n ) { if ( $ n == 0 ) { echo "1" ; return ; } $ res_size = 0 ; $ res = array ( ) ; $ temp = $ x ; while ( $ temp != 0 ) { $ res [ $ res_size ++ ] = $ temp % 10 ; $ temp = $ temp / 10 ; } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = multiply ( $ x , $ res ) ; echo $ x . " ^ " . $ n . " ▁ = ▁ " ; $ O = 0 ; for ( $ i = count ( $ res ) - 1 ; $ i >= 0 ; $ i -- , $ O ++ ) if ( $ res [ $ i ] ) break ; for ( $ i = count ( $ res ) - $ O - 1 ; $ i >= 0 ; $ i -- ) echo $ res [ $ i ] ; } $ exponent = 100 ; $ base = 2 ; power ( $ base , $ exponent ) ; ? >


"XNOR of two numbers | Please refer below post for details of this function https : www . geeksforgeeks . org / toggle - bits - significant - bit / ; Make a copy of n as we are going to change it . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Returns XNOR of num1 and num2 ; if num2 is greater then we swap this number in num1 ; Driver code"
How can the above be solved in PHP?

< ? php function togglebit ( $ n ) { if ( $ n == 0 ) return 1 ; $ i = $ n ; $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return $ i ^ $ n ; } function XNOR ( $ num1 , $ num2 ) { if ( $ num1 < $ num2 ) list ( $ num1 , $ num2 ) = array ( $ num2 , $ num1 ) ; $ num1 = togglebit ( $ num1 ) ; return $ num1 ^ $ num2 ; } $ num1 = 10 ; $ num2 = 20 ; echo XNOR ( $ num1 , $ num2 ) ; ? >


"XNOR of two numbers | log ( n ) solution ; Make sure a is larger ; for last bit of a ; for last bit of b ; counter for count bit and set bit in xnornum ; to make new xnor number ; for set bits in new xnor number ; get last bit of a ; get last bit of b ; Check if current two bits are same ; counter for count bit ; Driver code"
How can the above be solved in PHP?

< ? php function xnor ( $ a , $ b ) { if ( $ a < $ b ) list ( $ a , $ b ) = array ( $ b , $ a ) ; if ( $ a == 0 && $ b == 0 ) return 1 ; $ a_rem = 0 ; $ b_rem = 0 ; $ count = 0 ; $ xnornum = 0 ; while ( $ a ) { $ a_rem = $ a & 1 ; $ b_rem = $ b & 1 ; if ( $ a_rem == $ b_rem ) $ xnornum |= ( 1 << $ count ) ; $ count ++ ; $ a = $ a >> 1 ; $ b = $ b >> 1 ; } return $ xnornum ; } $ a = 10 ; $ b = 50 ; echo xnor ( $ a , $ b ) ; ? >


"XOR counts of 0 s and 1 s in binary representation | Returns XOR of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; Driver Code"
How can the above be solved in PHP?

< ? php function countXOR ( $ n ) { $ count0 = 0 ; $ count1 = 0 ; while ( $ n ) { ( $ n % 2 == 0 ) ? $ count0 ++ : $ count1 ++ ; $ n = intval ( $ n / 2 ) ; } return ( $ count0 ^ $ count1 ) ; } $ n = 31 ; echo countXOR ( $ n ) ; ? >


"XOR of Sum of every possible pair of an array | Function to find XOR of sum of all pairs ; Calculate xor of all the elements ; Return twice of xor value ; Driver code"
How can the above be solved in PHP?

< ? php function findXor ( $ arr , $ n ) { $ xoR = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ xoR = $ xoR ^ $ arr [ $ i ] ; } return $ xoR * 2 ; } $ arr = array ( 1 , 5 , 6 ) ; $ n = count ( $ arr ) ; echo findXor ( $ arr , $ n ) ; ? >


"XOR of XORs of all sub | Function to find to required XOR value ; Nested loop to find the number of sub - matrix each index belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code"
How can the above be solved in PHP?

 < ? php function submatrixXor ( $ arr ) { $ ans = 0 ; $ n = 3 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ top_left = ( $ i + 1 ) * ( $ j + 1 ) ; $ bottom_right = ( $ n - $ i ) * ( $ n - $ j ) ; if ( ( $ top_left % 2 == 1 ) && ( $ bottom_right % 2 == 1 ) ) $ ans = ( $ ans ^ $ arr [ $ i ] [ $ j ] ) ; } } return $ ans ; } $ arr = array ( array ( 6 , 7 , 13 ) , array ( 8 , 3 , 4 ) , array ( 9 , 7 , 6 ) ) ; echo submatrixXor ( $ arr ) ; # This  code is contributed by Ryuga
? >


"XOR of a submatrix queries | PHP implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries"
How can the above be solved in PHP?

< ? php $ n = 3 ; function preComputeXor ( $ arr , & $ prefix_xor ) { global $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ j == 0 ) $ prefix_xor [ $ i ] [ $ j ] = $ arr [ $ i ] [ $ j ] ; else $ prefix_xor [ $ i ] [ $ j ] = ( $ prefix_xor [ $ i ] [ $ j - 1 ] ^ $ arr [ $ i ] [ $ j ] ) ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 1 ; $ j < $ n ; $ j ++ ) $ prefix_xor [ $ j ] [ $ i ] = ( $ prefix_xor [ $ j - 1 ] [ $ i ] ^ $ prefix_xor [ $ j ] [ $ i ] ) ; } function ansQuerie ( $ prefix_xor , $ x1 , $ y1 , $ x2 , $ y2 ) { $ xor_1 = $ xor_2 = $ xor_3 = 0 ; if ( $ x1 != 0 ) $ xor_1 = $ prefix_xor [ $ x1 - 1 ] [ $ y2 ] ; if ( $ y1 != 0 ) $ xor_2 = $ prefix_xor [ $ x2 ] [ $ y1 - 1 ] ; if ( $ x1 != 0 and $ y1 != 0 ) $ xor_3 = $ prefix_xor [ $ x1 - 1 ] [ $ y1 - 1 ] ; return ( ( $ prefix_xor [ $ x2 ] [ $ y2 ] ^ $ xor_1 ) ^ ( $ xor_2 ^ $ xor_3 ) ) ; } $ arr = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; $ prefix_xor = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; preComputeXor ( $ arr , $ prefix_xor ) ; echo ansQuerie ( $ prefix_xor , 1 , 1 , 2 , 2 ) . " " ; echo ansQuerie ( $ prefix_xor , 1 , 2 , 2 , 2 ) ; ? >


"XOR of all subarray XORs | Set 1 | Returns XOR of all subarray xors ; initialize result by 0 as ( a XOR 0 = a ) ; loop over all elements once ; get the frequency of current element ; if frequency is odd , then include it in the result ; return the result ; Driver Code"
How can the above be solved in PHP?

< ? php function getTotalXorOfSubarrayXors ( $ arr , $ N ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ freq = ( $ i + 1 ) * ( $ N - $ i ) ; if ( $ freq % 2 == 1 ) $ res = $ res ^ $ arr [ $ i ] ; } return $ res ; } $ arr = array ( 3 , 5 , 2 , 4 , 6 ) ; $ N = count ( $ arr ) ; echo getTotalXorOfSubarrayXors ( $ arr , $ N ) ; ? >


"XOR of all subarray XORs | Set 1 | Returns XOR of all subarray xors ; initialize result by 0 as ( a xor 0 = a ) ; select the starting element ; select the eNding element ; Do XOR of elements in current subarray ; Driver code"
How can the above be solved in PHP?

< ? php function getTotalXorOfSubarrayXors ( $ arr , $ N ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i ; $ j < $ N ; $ j ++ ) for ( $ k = $ i ; $ k <= $ j ; $ k ++ ) $ res = $ res ^ $ arr [ $ k ] ; return $ res ; } $ arr = array ( 3 , 5 , 2 , 4 , 6 ) ; $ N = sizeof ( $ arr ) ; echo getTotalXorOfSubarrayXors ( $ arr , $ N ) ; ? >


"XOR of all subarray XORs | Set 2 | Returns XOR of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . So result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; Driver Code"
How can the above be solved in PHP?

< ? php function getTotalXorOfSubarrayXors ( $ arr , $ N ) { if ( $ N % 2 == 0 ) return 0 ; $ res = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i += 2 ) $ res ^= $ arr [ $ i ] ; return $ res ; } $ arr = array ( 3 , 5 , 2 , 4 , 6 ) ; $ N = count ( $ arr ) ; echo getTotalXorOfSubarrayXors ( $ arr , $ N ) ; ? >


"XOR of all the elements in the given range [ L , R ] | Function to return the most significant bit ; Function to return the required XOR ; Finding the MSB ; Value of the current bit to be added ; To store the final answer ; Loop for case 1 ; Edge case when both the integers lie in the same segment of continuous 1 s ; To store whether parity of count is odd ; Updating the answer if parity is odd ; Updating the number to be added ; Case 2 ; Driver code ; Final answer"
How can the above be solved in PHP?


"XOR of all the elements in the given range [ L , R ] | Function to return the required XOR ; Modulus operator are expensive on most of the computers . n & 3 will be equivalent to n % 4 n % 4 ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Driver code"
How can the above be solved in PHP?

< ? php function computeXOR ( $ n ) { $ x = $ n & 3 ; switch ( $ x ) { case 0 : return $ n ; case 1 : return 1 ; case 2 : return $ n + 1 ; case 3 : return 0 ; } return 0 ; } $ l = 1 ; $ r = 4 ; echo ( computeXOR ( $ r ) ^ computeXOR ( $ l - 1 ) ) ; ? >


"XOR of two numbers after making length of their binary representations equal | function to count the number of bits in binary representation of an integer ; initialize count ; count till n is non zero ; right shift by 1 i . e , divide by 2 ; function to calculate the xor of two numbers by adding trailing zeros to the number having less number of bits in its binary representation . ; stores the minimum and maximum ; left shift if the number of bits are less in binary representation ; Driver Code"
How can the above be solved in PHP?

< ? php function count1 ( $ n ) { $ c = 0 ; while ( $ n ) { $ c ++ ; $ n = $ n >> 1 ; } return $ c ; } function XOR1 ( $ a , $ b ) { $ c = min ( $ a , $ b ) ; $ d = max ( $ a , $ b ) ; if ( count1 ( $ c ) < count1 ( $ d ) ) $ c = $ c << ( count1 ( $ d ) - count1 ( $ c ) ) ; return ( $ c ^ $ d ) ; } $ a = 13 ; $ b = 5 ; echo XOR1 ( $ a , $ b ) ; ? >


"Zeckendorf 's Theorem (Non | Returns the greatest Fibonacci Number smaller than or equal to n . ; Corner cases ; Find the greatest Fibonacci Number smaller than n . ; Prints Fibonacci Representation of n using greedy algorithm ; Find the greates Fibonacci Number smaller than or equal to n ; Print the found fibonacci number ; Reduce n ; Driver Code"
How can the above be solved in PHP?

 < ? php function nearestSmallerEqFib ( $ n ) { if ( $ n == 0 $ n == 1 ) return $ n ; $ f1 = 0 ; $ f2 = 1 ; $ f3 = 1 ; while ( $ f3 <= $ n ) { $ f1 = $ f2 ; $ f2 = $ f3 ; $ f3 = $ f1 + $ f2 ; } return $ f2 ; } function printFibRepresntation ( $ n ) { while ( $ n > 0 ) { $ f = nearestSmallerEqFib ( $ n ) ; echo $ f , " " ; $ n = $ n - $ f ; } } $ n = 30 ; echo " Non - neighbouring ▁ Fibonacci ▁ Representation ▁ of ▁ " , $ n , " ▁ is ▁
 " ; printFibRepresntation ( $ n ) ; ? >


"k smallest elements in same order using O ( 1 ) extra space | Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; find largest from first k - elements ; if largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver Code"
How can the above be solved in PHP?


"kth smallest / largest in a small range unsorted array | PHP program of kth smallest / largest in a small range unsorted array ; Storing counts of elements ; Traverse hash array build above until we reach k - th smallest element . ; Driver code"
How can the above be solved in PHP?

 < ? php $ maxs = 1000001 ; function kthSmallestLargest ( & $ arr , $ n , $ k ) { global $ maxs ; $ max_val = max ( $ arr ) ; $ hash = array_fill ( 0 , $ max_val + 1 , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash [ $ arr [ $ i ] ] ++ ; $ count = 0 ; for ( $ i = 0 ; $ i <= $ max_val ; $ i ++ ) { while ( $ hash [ $ i ] > 0 ) { $ count ++ ; if ( $ count == $ k ) return $ i ; $ hash [ $ i ] -- ; } } return -1 ; } $ arr = array ( 11 , 6 , 2 , 9 , 4 , 3 , 16 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ k = 3 ; echo " kth ▁ smallest ▁ number ▁ is : ▁ " . kthSmallestLargest ( $ arr , $ n , $ k ) . "
 " ; return 0 ; ? >


"n | Function to find nth term ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { return $ n * ( $ n + 1 ) * ( 6 * $ n * $ n * $ n + 9 * $ n * $ n + $ n - 1 ) / 30 ; } $ n = 4 ; echo sumOfSeries ( $ n ) ; ? >


"n | Function to find the nth term of series ; Loop to add 4 th powers ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans += $ i * $ i * $ i * $ i ; return $ ans ; } $ n = 4 ; echo sumOfSeries ( $ n ) ; ? >


"n | Returns n - th term of the series 2 , 12 , 36 , 80 , 150 ; driver code"
How can the above be solved in PHP?

< ? php function nthTerm ( $ n ) { return ( $ n * $ n ) + ( $ n * $ n * $ n ) ; } $ n = 4 ; echo ( nthTerm ( $ n ) ) ; ? >


"n | Simple PHP program to find n - th number with sum of digits as 10. ; Find sum of digits in current no . ; If sum is 10 , we increment count ; If count becomes n , we return current number . ; Driver Code"
How can the above be solved in PHP?

< ? php function findNth ( $ n ) { $ count = 0 ; for ( $ curr = 19 ; ; $ curr += 9 ) { $ sum = 0 ; for ( $ x = $ curr ; $ x > 0 ; $ x = ( int ) $ x / 10 ) $ sum = $ sum + $ x % 10 ; if ( $ sum == 10 ) $ count ++ ; if ( $ count == $ n ) return $ curr ; } return -1 ; } echo findNth ( 5 ) ; ? >