"Exponential Search | Returns position of first occurrence of x in array ; If x is present at first location itself ; Find range for binary search by repeated doubling ; Call binary search for the found range . ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present n left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code"
How can the above be solved in PHP?

< ? php function exponentialSearch ( $ arr , $ n , $ x ) { if ( $ arr [ 0 ] == $ x ) return 0 ; $ i = 1 ; while ( $ i < $ n and $ arr [ $ i ] <= $ x ) $ i = $ i * 2 ; return binarySearch ( $ arr , $ i / 2 , min ( $ i , $ n - 1 ) , $ x ) ; } function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = $ l + ( $ r - $ l ) / 2 ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } $ arr = array ( 2 , 3 , 4 , 10 , 40 ) ; $ n = count ( $ arr ) ; $ x = 10 ; $ result = exponentialSearch ( $ arr , $ n , $ x ) ; if ( $ result == -1 ) echo " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ; else echo " Element ▁ is ▁ present ▁ at ▁ index ▁ " , $ result ; ? >


"Exponential Squaring ( Fast Modulo Multiplication ) | prime modulo value ; for cases where exponent is not an even value ; Driver Code"
How can the above be solved in PHP?

< ? php $ N = 1000000007 ; function exponentiation ( $ bas , $ exp ) { global $ N ; $ t = 1 ; while ( $ exp > 0 ) { if ( $ exp % 2 != 0 ) $ t = ( $ t * $ bas ) % $ N ; $ bas = ( $ bas * $ bas ) % $ N ; $ exp = ( int ) $ exp / 2 ; } return $ t % $ N ; } $ bas = 5 ; $ exp = 100000 ; $ modulo = exponentiation ( $ bas , $ exp ) ; echo ( $ modulo ) ; ? >


"Express a number as sum of consecutive numbers | Print consecutive numbers from last to first ; Driver Code"
How can the above be solved in PHP?

< ? php function printConsecutive ( $ last , $ first ) { echo $ first ++ ; for ( $ x = $ first ; $ x <= $ last ; $ x ++ ) echo " + " } function findConsecutive ( $ N ) { for ( $ last = 1 ; $ last < $ N ; $ last ++ ) { for ( $ first = 0 ; $ first < $ last ; $ first ++ ) { if ( 2 * $ N == ( $ last - $ first ) * ( $ last + $ first + 1 ) ) { echo $ N , " ▁ = ▁ " ; printConsecutive ( $ last , $ first + 1 ) ; return ; } } } echo " - 1" ; } $ n = 12 ; findConsecutive ( $ n ) ; ? >


"Express an odd number as sum of prime numbers | Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; CASE - I ; CASE - II ; CASE - III ; Driver code"
How can the above be solved in PHP?

< ? php function isPrime ( $ x ) { if ( $ x == 0 $ x == 1 ) return false ; for ( $ i = 2 ; $ i * $ i <= $ x ; ++ $ i ) if ( $ x % $ i == 0 ) return false ; return true ; } function findPrimes ( $ n ) { if ( isPrime ( $ n ) ) echo ( $ n ) ; else if ( isPrime ( $ n - 2 ) ) echo ( 2 . " ▁ " . ( $ n - 2 ) ) ; else { echo ( 3 . " ▁ " ) ; $ n = $ n - 3 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPrime ( $ i ) && isPrime ( $ n - $ i ) ) { echo ( $ i . " " . ( $ n - $ i ) ) ; break ; } } } } $ n = 27 ; findPrimes ( $ n ) ; ? >


"Expressing a fraction as a natural number under modulo ' m ' | Function to return the GCD of given numbers ; Recursive function to return ( x ^ n ) % m ; Function to return the fraction modulo mod ; ( b ^ m - 2 ) % m ; Final answer ; Driver code"
How can the above be solved in PHP?

< ? php function abc ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return abc ( $ b % $ a , $ a ) ; } function modexp ( $ x , $ n ) { $ m = 1000000007 ; if ( $ n == 0 ) { return 1 ; } else if ( $ n % 2 == 0 ) { return modexp ( ( $ x * $ x ) % $ m , $ n / 2 ) ; } else { return ( $ x * modexp ( ( $ x * $ x ) % $ m , ( $ n - 1 ) / 2 ) % $ m ) ; } } function getFractionModulo ( $ a , $ b ) { $ m = 1000000007 ; $ c = abc ( $ a , $ b ) ; $ a = $ a / $ c ; $ b = $ b / $ c ; $ d = modexp ( $ b , $ m - 2 ) ; $ ans = ( ( $ a % $ m ) * ( $ d % $ m ) ) % $ m ; return $ ans ; } $ a = 2 ; $ b = 6 ; echo ( getFractionModulo ( $ a , $ b ) ) ; ? >


"Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; N as sum of consecutive numbers"
How can the above be solved in PHP?

< ? php function countOddFactors ( $ n ) { $ odd_factors = 0 ; for ( $ i = 1 ; 1 * $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( 1 * $ i * $ i == $ n ) { if ( $ i & 1 ) $ odd_factors ++ ; } else { if ( $ i & 1 ) $ odd_factors ++ ; $ factor = $ n / $ i ; if ( $ factor & 1 ) $ odd_factors ++ ; } } } return $ odd_factors - 1 ; } $ N = 15 ; echo ( countOddFactors ( $ N ) . ( " " ) ) ; $ N = 10 ; echo ( countOddFactors ( $ N ) ) ; ? >


"Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility function to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Function to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Drivers program"
How can the above be solved in PHP?

< ? php function maximumNum ( $ curr_num , $ res ) { $ len1 = strlen ( $ curr_num ) ; $ len2 = strlen ( $ res ) ; if ( $ len1 == $ len2 ) { $ i = 0 ; while ( $ curr_num [ $ i ] == $ res [ $ i ] ) $ i ++ ; if ( $ curr_num [ $ i ] < $ res [ $ i ] ) return $ res ; else return $ curr_num ; } return $ len1 < $ len2 ? $ res : $ curr_num ; } function extractMaximum ( $ str ) { $ n = strlen ( $ str ) ; $ curr_num = " " ; $ res = " " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ i < $ n && $ str [ $ i ] == '0' ) $ i ++ ; while ( $ i < $ n && $ str [ $ i ] >= '0' && $ str [ $ i ] <= '9' ) { $ curr_num . = $ str [ $ i ] ; $ i ++ ; } if ( $ i == $ n ) break ; if ( strlen ( $ curr_num ) > 0 ) $ i -- ; $ res = maximumNum ( $ curr_num , $ res ) ; $ curr_num = " " ; } if ( strlen ( $ curr_num ) == 0 && strlen ( $ res ) == 0 ) $ res . = '0' ; return maximumNum ( $ curr_num , $ res ) ; } $ str = "100klh564abc365bg " ; echo extractMaximum ( $ str ) ; ? >


"Farey Sequence | Optimized function to print Farey sequence of order n ; We know first two terms are 0 / 1 and 1 / n ; For next terms to be evaluated ; Using recurrence relation to find the next term ; Print next term ; Update x1 , y1 , x2 and y2 for next iteration ; Driver Code"
How can the above be solved in PHP?


"Fascinating Number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not"
How can the above be solved in PHP?

< ? php function isFascinating ( $ num ) { $ freq = array_fill ( 0 , 10 , NULL ) ; $ val = " " . $ num . ( $ num * 2 ) . ( $ num * 3 ) ; for ( $ i = 0 ; $ i < strlen ( $ val ) ; $ i ++ ) { $ digit = $ val [ $ i ] - '0' ; if ( $ freq [ $ digit ] > 0 && $ digit != 0 ) return false ; else $ freq [ $ digit ] ++ ; } for ( $ i = 1 ; $ i < 10 ; $ i ++ ) { if ( $ freq [ $ i ] == 0 ) return false ; } return true ; } $ num = 192 ; if ( $ num < 100 ) echo " No " ; else { $ ans = isFascinating ( $ num ) ; if ( $ ans ) echo " Yes " ; else echo " No " ; } ? >


"Fermat 's Last Theorem | PHP program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver Code"
How can the above be solved in PHP?

< ? php function testSomeNumbers ( $ limit , $ n ) { if ( $ n < 3 ) for ( $ a = 1 ; $ a <= $ limit ; $ a ++ ) for ( $ b = $ a ; $ b <= $ limit ; $ b ++ ) { $ pow_sum = pow ( $ a , $ n ) + pow ( $ b , $ n ) ; $ c = pow ( $ pow_sum , 1.0 / $ n ) ; $ c_pow = pow ( $ c , $ n ) ; if ( $ c_pow != $ pow_sum ) { echo " Count ▁ example ▁ found " ; return ; } } echo " No ▁ counter ▁ example ▁ within ▁ " . " given ▁ range ▁ and ▁ data " ; } testSomeNumbers ( 10 , 3 ) ; ? >


"Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code"
How can the above be solved in PHP?

< ? php function checkFibinnary ( $ n ) { $ prev_last = 0 ; while ( $ n ) { if ( ( $ n & 1 ) && $ prev_last ) return false ; $ prev_last = $ n & 1 ; $ n >>= 1 ; } return true ; } $ n = 10 ; if ( checkFibinnary ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check whether a number is fibbinary or not ; if the number does not contain adjacent ones then ( n & ( n >> 1 ) ) operation results to 0 ; not a fibbinary number ; Driver code"
How can the above be solved in PHP?

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


"Fibonacci modulo p | Returns position of first Fibonacci number whose modulo p is 0. ; add previous two remainders and then take its modulo p . ; Driver code"
How can the above be solved in PHP?

 < ? php function findMinZero ( $ p ) { $ first = 1 ; $ second = 1 ; $ number = 2 ; $ next = 1 ; while ( $ next ) { $ next = ( $ first + $ second ) % $ p ; $ first = $ second ; $ second = $ next ; $ number ++ ; } return $ number ; } $ p = 7 ; echo " Minimal ▁ zero ▁ is : ▁ " , findMinZero ( $ p ) , "
 " ; ? >


"Fibonacci problem ( Value of Fib ( N ) * Fib ( N ) | PHP implementation of the approach ; Driver code"
How can the above be solved in PHP?

< ? php function getResult ( $ n ) { if ( $ n & 1 ) return 1 ; return -1 ; } $ n = 3 ; echo getResult ( $ n ) ; ? >


"Fibonomial coefficient and Fibonomial triangle | PHP Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driven Program"
How can the above be solved in PHP?


"Fill array with 1 's using minimum iterations of filling neighbors | Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver code"
How can the above be solved in PHP?

< ? php function countIterations ( $ arr , $ n ) { $ oneFound = false ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; ) { if ( $ arr [ $ i ] == 1 ) $ oneFound = true ; while ( $ i < $ n && $ arr [ $ i ] == 1 ) $ i ++ ; $ count_zero = 0 ; while ( $ i < $ n && $ arr [ $ i ] == 0 ) { $ count_zero ++ ; $ i ++ ; } if ( $ oneFound == false && $ i == $ n ) return -1 ; $ curr_count ; if ( $ i < $ n && $ oneFound == true ) { if ( $ count_zero & 1 == 0 ) $ curr_count = $ count_zero / 2 ; else $ curr_count = ( $ count_zero + 1 ) / 2 ; $ count_zero = 0 ; } else { $ curr_count = $ count_zero ; $ count_zero = 0 ; } $ res = max ( $ res , $ curr_count ) ; } return $ res ; } $ arr = array ( 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo countIterations ( $ arr , $ n ) ; ? >


"Final cell position in the matrix | function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code"
How can the above be solved in PHP?

< ? php function finalPos ( $ command , $ n , $ x , $ y ) { $ cup ; $ cdown ; $ cleft ; $ cright ; $ final_x ; $ final_y ; $ cup = $ cdown = $ cleft = $ cright = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ command [ $ i ] == ' U ' ) $ cup ++ ; else if ( $ command [ $ i ] == ' D ' ) $ cdown ++ ; else if ( $ command [ $ i ] == ' L ' ) $ cleft ++ ; else if ( $ command [ $ i ] == ' R ' ) $ cright ++ ; } $ final_x = $ x + ( $ cright - $ cleft ) ; $ final_y = $ y + ( $ cdown - $ cup ) ; echo " Final ▁ Position : ▁ " . " ( " . $ final_x . " , ▁ " . $ final_y . " ) " ; } $ command = " DDLRULL " ; $ n = strlen ( $ command ) ; $ x = 3 ; $ y = 4 ; finalPos ( $ command , $ n , $ x , $ y ) ;


"Final string after performing given operations | Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Code"
How can the above be solved in PHP?

< ? php function printFinalString ( $ s ) { $ n = strlen ( $ s ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' x ' ) $ x ++ ; else $ y ++ ; } $ finalString = ( string ) null ; if ( $ x > $ y ) for ( $ i = 0 ; $ i < $ x - $ y ; $ i ++ ) $ finalString . = " x " ; else for ( $ i = 0 ; $ i < $ y - $ x ; $ i ++ ) $ finalString . = " y " ; return $ finalString ; } $ s = " xxyyxyy " ; echo printFinalString ( $ s ) ; ? >


"Find ' N ' number of solutions with the given inequality equations | Function to calculate all the solutions ; there is no solutions ; print first element as y - n + 1 ; print rest n - 1 elements as 1 ; initialize the number of elements and the value of x an y"
How can the above be solved in PHP?

 < ? php function findsolution ( $ n , $ x , $ y ) { if ( ( $ y - $ n + 1 ) * ( $ y - $ n + 1 ) + $ n - 1 < $ x $ y < $ n ) { echo " No ▁ solution " ; return ; } echo $ y - $ n + 1 ; while ( $ n -- > 1 ) echo "
 " . 1 ; } $ n = 5 ; $ x = 15 ; $ y = 15 ; findsolution ( $ n , $ x , $ y ) ;


"Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code"
How can the above be solved in PHP?

< ? php function A_mod_B ( $ N , $ a ) { $ len = strlen ( $ N ) ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ ans = ( $ ans * 10 + ( int ) $ N [ $ i ] - '0' ) % $ a ; return $ ans % $ a ; } function findMod ( $ N ) { $ mod = A_mod_B ( $ N , 4 ) ; $ ans = ( 1 + pow ( 2 , $ mod ) + pow ( 3 , $ mod ) + pow ( 4 , $ mod ) ) ; return ( $ ans % 5 ) ; } $ N = "4" ; echo findMod ( $ N ) ; ? >


"Find 2 ^ ( 2 ^ A ) % B | Function to return 2 ^ ( 2 ^ A ) % B ; $Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ $A ) % $B"
How can the above be solved in PHP?

< ? php function F ( $ A , $ B ) { if ( $ A == 1 ) return ( 4 % $ B ) ; else { $ temp = F ( $ A - 1 , $ B ) ; return ( $ temp * $ temp ) % $ B ; } } $ A = 25 ; $ B = 50 ; echo F ( $ A , $ B ) ;


"Find A and B from list of divisors | Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code"
How can the above be solved in PHP?


"Find First element in AP which is multiple of given prime | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to find nearest element in common ; base conditions ; Driver code ; module both A and D ; function call"
How can the above be solved in PHP?

< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function NearestElement ( $ A , $ D , $ P ) { if ( $ A == 0 ) return 0 ; else if ( $ D == 0 ) return -1 ; else { $ X = power ( $ D , $ P - 2 , $ P ) ; return ( $ X * ( $ P - $ A ) ) % $ P ; } } $ A = 4 ; $ D = 9 ; $ P = 11 ; $ A %= $ P ; $ D %= $ P ; echo NearestElement ( $ A , $ D , $ P ) ; ? >


"Find GCD of factorial of elements of given array | Implementation of factorial function ; Function to find GCD of factorial of elements from array ; find the minimum element of array ; return the factorial of minimum element ; Driver Code"
How can the above be solved in PHP?

< ? php function factorial ( $ n ) { return ( $ n == 1 $ n == 0 ) ? 1 : factorial ( $ n - 1 ) * $ n ; } function gcdOfFactorial ( $ arr , $ n ) { $ minm = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minm = $ minm > $ arr [ $ i ] ? $ arr [ $ i ] : $ minm ; return factorial ( $ minm ) ; } $ arr = array ( 9 , 12 , 122 , 34 , 15 ) ; $ n = count ( $ arr ) ; echo gcdOfFactorial ( $ arr , $ n ) ; ? >


"Find HCF of two numbers without using recursion or Euclidean algorithm | Function to return the HCF of x and y ; Minimum of the two numbers ; If both the numbers are divisible by the minimum of these two then the HCF is equal to the minimum ; Highest number between 2 and minimum / 2 which can divide both the numbers is the required HCF ; If both the numbers are divisible by i ; 1 divides every number ; Driver code"
How can the above be solved in PHP?

< ? php function getHCF ( $ x , $ y ) { $ minimum = min ( $ x , $ y ) ; if ( $ x % $ minimum == 0 && $ y % $ minimum == 0 ) return $ minimum ; for ( $ i = $ minimum / 2 ; $ i >= 2 ; $ i -- ) { if ( $ x % $ i == 0 && $ y % $ i == 0 ) return $ i ; } return 1 ; } $ x = 16 ; $ y = 32 ; echo ( getHCF ( $ x , $ y ) ) ; ? >


"Find Harmonic mean using Arithmetic mean and Geometric mean | Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver Code"
How can the above be solved in PHP?

< ? php function compute ( $ a , $ b ) { $ AM ; $ GM ; $ HM ; $ AM = ( $ a + $ b ) / 2 ; $ GM = sqrt ( $ a * $ b ) ; $ HM = ( $ GM * $ GM ) / $ AM ; return $ HM ; } $ a = 5 ; $ b = 15 ; $ HM = compute ( $ a , $ b ) ; echo " Harmonic ▁ Mean ▁ between ▁ " . $ a . " ▁ and ▁ " . $ b . " ▁ is ▁ " . $ HM ; return 0 ; ? >


"Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array | Returns index of 0 to be replaced with 1 to get longest continuous sequence of 1 s . If there is no 0 in array , then it returns - 1. ; for maximum number of ; 1 around a zero for storing result ; index of previous zero ; index of previous to ; previous zero Traverse the input array ; If current element is 0 , then calculate the difference between curr and prev_prev_zero ; Update result if count of 1 s around prev_zero is more ; Update for next iteration ; Check for the last encountered zero ; Driver Code"
How can the above be solved in PHP?

< ? php function maxOnesIndex ( $ arr , $ n ) { $ max_count = 0 ; $ max_index ; $ prev_zero = -1 ; $ prev_prev_zero = -1 ; for ( $ curr = 0 ; $ curr < $ n ; ++ $ curr ) { if ( $ arr [ $ curr ] == 0 ) { if ( $ curr - $ prev_prev_zero > $ max_count ) { $ max_count = $ curr - $ prev_prev_zero ; $ max_index = $ prev_zero ; } $ prev_prev_zero = $ prev_zero ; $ prev_zero = $ curr ; } } if ( $ n - $ prev_prev_zero > $ max_count ) $ max_index = $ prev_zero ; return $ max_index ; } $ arr = array ( 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " , maxOnesIndex ( $ arr , $ n ) ; ? >


"Find Index of given fibonacci number in constant time | A simple PHP program to find index of given Fibonacci number . ; if Fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver Code"
How can the above be solved in PHP?

< ? php function findIndex ( $ n ) { if ( $ n <= 1 ) return $ n ; $ a = 0 ; $ b = 1 ; $ c = 1 ; $ res = 1 ; while ( $ c < $ n ) { $ c = $ a + $ b ; $ res ++ ; $ a = $ b ; $ b = $ c ; } return $ res ; } $ result = findIndex ( 21 ) ; echo ( $ result ) ; ? >


"Find Intersection of all Intervals | Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code"
How can the above be solved in PHP?

< ? php function findIntersection ( $ intervals , $ N ) { $ l = $ intervals [ 0 ] [ 0 ] ; $ r = $ intervals [ 0 ] [ 1 ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { if ( $ intervals [ $ i ] [ 0 ] > $ r $ intervals [ $ i ] [ 1 ] < $ l ) { echo - 1 ; return ; } else { $ l = max ( $ l , $ intervals [ $ i ] [ 0 ] ) ; $ r = min ( $ r , $ intervals [ $ i ] [ 1 ] ) ; } } echo " [ " ▁ . ▁ $ l ▁ . ▁ " , " ▁ . ▁ $ r ▁ . ▁ " ] " ; } $ intervals = array ( array ( 1 , 6 ) , array ( 2 , 8 ) , array ( 3 , 10 ) , array ( 5 , 8 ) ) ; $ N = sizeof ( $ intervals ) ; findIntersection ( $ intervals , $ N ) ; ? >


"Find Largest Special Prime which is less than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Largest Special Prime which is less than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else decrement the number . ; Driver code"
How can the above be solved in PHP?

 < ? php function checkSpecialPrime ( & $ sieve , $ num ) { while ( $ num ) { if ( ! $ sieve [ $ num ] ) { return false ; } $ num = ( int ) ( $ num / 10 ) ; } return true ; } function findSpecialPrime ( $ N ) { $ sieve = array_fill ( 0 , $ N + 10 , true ) ; $ sieve [ 0 ] = $ sieve [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ N ; $ i ++ ) { if ( $ sieve [ $ i ] ) { for ( $ j = $ i * $ i ; $ j <= $ N ; $ j += $ i ) { $ sieve [ $ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( $ sieve , $ N ) ) { echo $ N . "
 " ; break ; } else $ N -- ; } } findSpecialPrime ( 379 ) ; findSpecialPrime ( 100 ) ; ? >


"Find Last Digit of a ^ b for Large Numbers | Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return $mod ; return modulo ; function to find last digit of a ^ b ; if a and b both are 0 ; if exponent is 0 ; if base is 0 ; if exponent is divisible by 4 that means last digit will be pow ( a , 4 ) % 10. if exponent is not divisible by 4 that means last digit will be pow ( a , b % 4 ) % 10 ; Find last digit in ' a ' and compute its exponent ; Return last digit of result ; Driver program to run test case"
How can the above be solved in PHP?


"Find M | number whosesum till one digit is N ; Driver Code"
How can the above be solved in PHP?

< ? php function findNumber ( $ n , $ m ) { $ num = ( $ m - 1 ) * 9 + $ n ; return $ num ; } $ n = 2 ; $ m = 5 ; echo findNumber ( $ n , $ m ) ; ? >


"Find Multiples of 2 or 3 or 5 less than or equal to N | Bit count function ; Function to count number of multiples of 2 or 3 or 5 less than or equal to N ; As we have to check divisibility by three numbers , So we can implement bit masking ; we check whether jth bit is set or not , if jth bit is set , simply multiply to prod ; check for set bit ; check multiple of product ; Driver code"
How can the above be solved in PHP?

< ? php function popcount ( $ value ) { $ count = 0 ; while ( $ value ) { $ count += ( $ value & 1 ) ; $ value = $ value >> 1 ; } return $ count ; } function countMultiples ( $ n ) { $ multiple = array ( 2 , 3 , 5 ) ; $ count = 0 ; $ mask = pow ( 2 , 3 ) ; for ( $ i = 1 ; $ i < $ mask ; $ i ++ ) { $ prod = 1 ; for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { if ( $ i & 1 << $ j ) $ prod = $ prod * $ multiple [ $ j ] ; } if ( popcount ( $ i ) % 2 == 1 ) $ count = $ count + ( int ) ( $ n / $ prod ) ; else $ count = $ count - ( int ) ( $ n / $ prod ) ; } return $ count ; } $ n = 10 ; echo countMultiples ( $ n ) ; ? >


"Find N Arithmetic Means between A and B | Prints N arithmetic means between A and B . ; calculate common difference ( d ) ; for finding N the arithmetic mean between A and B ; Driver Code"
How can the above be solved in PHP?


"Find N Geometric Means between A and B | Pr$s N geometric means between A and B . ; calculate common ratio ( R ) ; for finding N the Geometric mean between A and B ; Driver Code"
How can the above be solved in PHP?


"Find N digits number which is divisible by D | Function to return N digits number which is divisible by D ; to store answer ; Driver code"
How can the above be solved in PHP?

< ? php function findNumber ( $ n , $ d ) { $ ans = " " ; if ( $ d != 10 ) { $ ans . = strval ( $ d ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ ans . = '0' ; } else { if ( n == 1 ) $ ans . = " Impossible " ; else $ ans . = '1' ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ ans . = '0' ; } return $ ans ; } $ n = 12 ; $ d = 3 ; print ( findNumber ( $ n , $ d ) ) ;


"Find N integers with given difference between product and sum | Function to implement calculation ; Driver Code"
How can the above be solved in PHP?

 < ? php function findNumbers ( $ n , $ d ) { for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) echo "1" , " ▁ " ; echo "2" , " ▁ " ; echo $ n + $ d , "
 " ; } $ N = 3 ; $ D = 5 ; findNumbers ( $ N , $ D ) ; ? >


"Find Next Sparse Number | PHP program to find next sparse number ; Find binary representation of x and store it in bin [ ] . bin [ 0 ] contains least significant bit ( LSB ) , next bit is in bin [ 1 ] , and so on . ; There my be extra bit in result , so add one extra bit ; The position till which all bits are finalized ; Start from second bit ( next to LSB ) ; If current bit and its previous bit are 1 , but next bit is not 1. ; Make the next bit 1 ; Make all bits before current bit as 0 to make sure that we get the smallest next number ; Store position of the bit set so that this bit and bits before it are not changed next time . ; Find decimal equivalent of modified bin [ ] ; Driver Code"
How can the above be solved in PHP?

< ? php function nextSparse ( $ x ) { $ bin = array ( ) ; while ( $ x != 0 ) { array_push ( $ bin , $ x & 1 ) ; $ x >>= 1 ; } array_push ( $ bin , 0 ) ; $ last_final = 0 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ bin [ $ i ] == 1 && $ bin [ $ i - 1 ] == 1 && $ bin [ $ i + 1 ] != 1 ) { $ bin [ $ i + 1 ] = 1 ; for ( $ j = $ i ; $ j >= $ last_final ; $ j -- ) $ bin [ $ j ] = 0 ; $ last_final = $ i + 1 ; } } $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ ans += $ bin [ $ i ] * ( 1 << $ i ) ; return $ ans ; } $ x = 38 ; echo " Next ▁ Sparse ▁ Number ▁ is ▁ " . nextSparse ( $ x ) ; ? >


"Find Nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | PHP program to find Nth term of the series ; function to return nth term of the series ; Taking n as 4 ; function call"
How can the above be solved in PHP?

< ? php $ mod = 1000000009 ; function NthTerm ( $ n ) { global $ mod ; $ x = ( 2 * $ n * $ n ) % $ mod ; return ( $ x - $ n + $ mod ) % $ mod ; } $ N = 4 ; echo NthTerm ( $ N ) ; ? >


"Find Nth positive number whose digital root is X | Function to find the N - th number with digital root as X ; Driver Code"
How can the above be solved in PHP?

< ? php function findAnswer ( $ X , $ N ) { return ( $ N - 1 ) * 9 + $ X ; } $ X = 7 ; $ N = 43 ; echo ( findAnswer ( $ X , $ N ) ) ; ? >


"Find Nth positive number whose digital root is X | Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code"
How can the above be solved in PHP?

< ? php function findDigitalRoot ( $ num ) { $ sum = PHP_INT_MAX ; $ tempNum = $ num ; while ( $ sum >= 10 ) { $ sum = 0 ; while ( $ tempNum > 0 ) { $ sum += $ tempNum % 10 ; $ tempNum /= 10 ; } $ tempNum = $ sum ; } return $ sum ; } function findAnswer ( $ X , $ N ) { $ counter = 0 ; for ( $ i = 1 ; $ counter < $ N ; ++ $ i ) { $ digitalRoot = findDigitalRoot ( $ i ) ; if ( $ digitalRoot == $ X ) { ++ $ counter ; } if ( $ counter == $ N ) { echo ( $ i ) ; break ; } } } $ X = 1 ; $ N = 3 ; findAnswer ( $ X , $ N ) ;


"Find Nth term ( A matrix exponentiation example ) | PHP program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence will be first row of res * Initial Vector . ; Driver code"
How can the above be solved in PHP?


"Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function for finding factorial of N ; return factorial of N ; Function for calculating Nth term of series ; Driver code"
How can the above be solved in PHP?

< ? php function factorial ( $ N ) { $ fact = 1 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function nthTerm ( $ N ) { return ( factorial ( $ N ) * ( $ N + 2 ) / 2 ) ; } $ N = 6 ; echo nthTerm ( $ N ) ; ? >


"Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function to find factorial of N with recursion ; base condition ; use recursion ; calculate Nth term of series ; Driver Code"
How can the above be solved in PHP?

< ? php function factorial ( $ N ) { if ( $ N == 0 or $ N == 1 ) return 1 ; return $ N * factorial ( $ N - 1 ) ; } function nthTerm ( $ N ) { return ( factorial ( $ N ) * ( $ N + 2 ) / 2 ) ; } $ N = 6 ; echo nthTerm ( $ N ) ; ? >


"Find Nth term of the series 0 , 2 , 4 , 8 , 12 , 18. . . | Calculate Nth term of series ; Driver Code"
How can the above be solved in PHP?

< ? php function nthTerm ( $ N ) { return ( int ) ( ( $ N + $ N * ( $ N - 1 ) ) / 2 ) ; } $ N = 5 ; echo nthTerm ( $ N ) ; ? >


"Find Nth term of the series 1 , 5 , 32 , 288 ... | Function to generate a fixed \ number ; Finding nth term ; Driver Code"
How can the above be solved in PHP?

< ? php function nthTerm ( $ N ) { $ nth = 0 ; $ i ; for ( $ i = $ N ; $ i > 0 ; $ i -- ) { $ nth += pow ( $ i , $ i ) ; } return $ nth ; } $ N = 3 ; echo ( nthTerm ( $ N ) ) ; ? >


"Find Nth term of the series 1 , 6 , 18 , 40 , 75 , ... . | Function to generate a fixed number ; ( N ^ 2 * ( N + 1 ) ) / 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function nthTerm ( $ N ) { $ nth = 0 ; $ nth = ( $ N * $ N * ( $ N + 1 ) ) / 2 ; return $ nth ; } $ N = 5 ; echo nthTerm ( $ N ) ; ? >


"Find Nth term of the series 1 , 8 , 54 , 384. . . | calculate factorial of N ; calculate Nth term of series ; Driver Code"
How can the above be solved in PHP?

< ? php function fact ( $ N ) { $ product = 1 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ product = $ product * $ i ; return $ product ; } function nthTerm ( $ N ) { return ( $ N * $ N ) * fact ( $ N ) ; } $ N = 4 ; echo nthTerm ( $ N ) ; ? >


"Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver Code"
How can the above be solved in PHP?

 < ? php function print2largest ( $ arr , $ arr_size ) { if ( $ arr_size < 2 ) { echo ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } $ first = $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) { if ( $ arr [ $ i ] > $ first ) { $ second = $ first ; $ first = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ second && $ arr [ $ i ] != $ first ) $ second = $ arr [ $ i ] ; } if ( $ second == PHP_INT_MIN ) echo ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element
 " ) ; else echo ( " The ▁ second ▁ largest ▁ element ▁ is ▁ " . $ second . "
 " ) ; } $ arr = array ( 12 , 35 , 1 , 10 , 34 , 1 ) ; $ n = sizeof ( $ arr ) ; print2largest ( $ arr , $ n ) ; ? >


"Find Selling Price from given Profit Percentage and Cost | Function to calculate the Selling Price ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Get the CP and Profit % ; Printing the returned value"
How can the above be solved in PHP?

< ? php function SellingPrice ( $ CP , $ PP ) { $ P_decimal = 1 + ( $ PP / 100 ) ; $ res = $ P_decimal * $ CP ; return $ res ; } $ C = 720 ; $ P = 13 ; echo SellingPrice ( $ C , $ P ) ; ? >


"Find Square Root under Modulo p | Set 2 ( Shanks Tonelli algorithm ) | utility function to find pow ( base , exponent ) % modulus ; utility function to find gcd ; Returns k such that b ^ k = 1 ( mod p ) ; Initializing k with first odd prime number ; function return p - 1 ( = x argument ) as x * 2 ^ e , where x will be odd sending e as reference because updation is needed in actual e ; Main function for finding the modular square root ; a and p should be coprime for finding the modular square root ; If below expression return ( p - 1 ) then modular square root is not possible ; expressing p - 1 , in terms of s * 2 ^ e , where s is odd number ; finding smallest q such that q ^ ( ( p - 1 ) / 2 ) ( mod p ) = p - 1 ; q - 1 is in place of ( - 1 % p ) ; Initializing variable x , b and g ; keep looping until b become 1 or m becomes 0 ; finding m such that b ^ ( 2 ^ m ) = 1 ; updating value of x , g and b according to algorithm ; Driver Code ; p should be prime"
How can the above be solved in PHP?

 < ? php function pow1 ( $ base , $ exponent , $ modulus ) { $ result = 1 ; $ base = $ base % $ modulus ; while ( $ exponent > 0 ) { if ( $ exponent % 2 == 1 ) $ result = ( $ result * $ base ) % $ modulus ; $ exponent = $ exponent >> 1 ; $ base = ( $ base * $ base ) % $ modulus ; } return $ result ; } function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; else return gcd ( $ b , $ a % $ b ) ; } function order ( $ p , $ b ) { if ( gcd ( $ p , $ b ) != 1 ) { print ( " p ▁ and ▁ b ▁ are ▁ not ▁ co - prime .
 " ) ; return -1 ; } $ k = 3 ; while ( 1 ) { if ( pow1 ( $ b , $ k , $ p ) == 1 ) return $ k ; $ k ++ ; } } function convertx2e ( $ x , & $ e ) { $ e = 0 ; while ( $ x % 2 == 0 ) { $ x = ( int ) ( $ x / 2 ) ; $ e ++ ; } return $ x ; } function STonelli ( $ n , $ p ) { if ( gcd ( $ n , $ p ) != 1 ) { print ( " a ▁ and ▁ p ▁ are ▁ not ▁ coprime
 " ) ; return -1 ; } if ( pow1 ( $ n , ( $ p - 1 ) / 2 , $ p ) == ( $ p - 1 ) ) { printf ( " no ▁ sqrt ▁ possible
 " ) ; return -1 ; } $ e = 0 ; $ s = convertx2e ( $ p - 1 , $ e ) ; $ q = 2 ; for ( ; ; $ q ++ ) { if ( pow1 ( $ q , ( $ p - 1 ) / 2 , $ p ) == ( $ p - 1 ) ) break ; } $ x = pow1 ( $ n , ( $ s + 1 ) / 2 , $ p ) ; $ b = pow1 ( $ n , $ s , $ p ) ; $ g = pow1 ( $ q , $ s , $ p ) ; $ r = $ e ; while ( 1 ) { $ m = 0 ; for ( ; $ m < $ r ; $ m ++ ) { if ( order ( $ p , $ b ) == -1 ) return -1 ; if ( order ( $ p , $ b ) == pow ( 2 , $ m ) ) break ; } if ( $ m == 0 ) return $ x ; $ x = ( $ x * pow1 ( $ g , pow ( 2 , $ r - $ m - 1 ) , $ p ) ) % $ p ; $ g = pow1 ( $ g , pow ( 2 , $ r - $ m ) , $ p ) ; $ b = ( $ b * $ g ) % $ p ; if ( $ b == 1 ) return $ x ; $ r = $ m ; } } $ n = 2 ; $ p = 113 ; $ x = STonelli ( $ n , $ p ) ; if ( $ x == -1 ) print ( " Modular ▁ square ▁ root ▁ is ▁ not ▁ exist
 " ) ; else print ( " Modular ▁ square ▁ root ▁ of ▁ " . " $ n ▁ and ▁ $ p ▁ is ▁ $ x
 " ) ; ? >


"Find Sum of Series 1 ^ 2 | Function to find sum of series ; If n is even ; If n is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum"
How can the above be solved in PHP?

 < ? php function sum_of_series ( $ n ) { $ result = 0 ; if ( $ n % 2 == 0 ) { $ result = - ( $ n * ( $ n + 1 ) ) / 2 ; } else { $ result = ( $ n * ( $ n + 1 ) ) / 2 ; } return $ result ; } $ n = 3 ; echo sum_of_series ( $ n ) ; echo ( "
 " ) ; $ n = 10 ; echo sum_of_series ( $ n ) ; echo ( "
 " ) ; ? >


"Find Sum of Series 1 ^ 2 | PHP program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... Function to find sum of series ; If i is even ; If i is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum ; This Code is Contributed by anuj_67"
How can the above be solved in PHP?

 < ? php function sum_of_series ( $ n ) { $ result = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ result = $ result - pow ( $ i , 2 ) ; else $ result = $ result + pow ( $ i , 2 ) ; } return $ result ; } $ n = 3 ; echo sum_of_series ( $ n ) , "
 " ; $ n = 10 ; echo sum_of_series ( $ n ) , "
 " ; ? >


"Find Surpasser Count of each element in array | Function to find surpasser count of each element in array ; stores surpasser count for element arr [ i ] ; Function to print an array ; Driver Code"
How can the above be solved in PHP?

 < ? php function findSurpasser ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 0 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] > $ arr [ $ i ] ) $ count ++ ; echo $ count , " " ; } } function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; echo "
 " ; } $ arr = array ( 2 , 7 , 5 , 3 , 0 , 8 , 1 ) ; $ n = count ( $ arr ) ; echo " Given ▁ array ▁ is ▁
 " ; printArray ( $ arr , $ n ) ; echo " Surpasser ▁ Count ▁ of ▁ array ▁ is ▁
 " ; findSurpasser ( $ arr , $ n ) ; ? >


"Find Tangent at a given point on the curve | function for find Tangent ; differentiate given equation ; check that point on the curve or not ; if differentiate is negative ; differentiate is positive ; differentiate is zero ; declare variable ; call function findTangent"
How can the above be solved in PHP?

< ? php function findTangent ( $ A , $ x , $ y ) { $ dif = $ A - $ x * 2 ; if ( $ y == ( 2 * $ x - $ x * $ x ) ) { if ( $ dif < 0 ) echo " y = " , ▁ $ dif ▁ , ▁ " x " ( $ x * $ dif ) + ( $ y ) ; else if ( $ dif > 0 ) echo " y ▁ = ▁ " , $ dif , " x + " , - $ x * $ dif + $ y ; else echo " Not ▁ possible " ; } } $ A = 2 ; $ x = 2 ; $ y = 0 ; findTangent ( $ A , $ x , $ y ) ; ? >


"Find Two Missing Numbers | Set 2 ( XOR based solution ) | Function to find two missing numbers in range [ 1 , n ] . This function assumes that size of array is n - 2 and all array elements are distinct ; Get the XOR of all elements in arr [ ] and { 1 , 2 . . n } ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver Code ; Range of numbers is 2 plus size of array"
How can the above be solved in PHP?

 < ? php function findTwoMissingNumbers ( $ arr , $ n ) { $ XOR = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n - 2 ; $ i ++ ) $ XOR ^= $ arr [ $ i ] ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ XOR ^= $ i ; $ set_bit_no = $ XOR & ~ ( $ XOR - 1 ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i & $ set_bit_no ) $ x = $ x ^ $ i ; else $ y = $ y ^ $ i ; } echo " Two ▁ Missing ▁ Numbers ▁ are
 " , $ x ; echo "
 " , $ y ; } $ arr = array ( 1 , 3 , 5 , 6 ) ; $ n = 2 + count ( $ arr ) ; findTwoMissingNumbers ( $ arr , $ n ) ; ? >


"Find Union and Intersection of two unsorted arrays | Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call"
How can the above be solved in PHP?


"Find Union and Intersection of two unsorted arrays | Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be presen in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver program to test above function ; Function call"
How can the above be solved in PHP?

 < ? php function printUnion ( $ arr1 , $ arr2 , $ m , $ n ) { if ( $ m > $ n ) { $ tempp = $ arr1 ; $ arr1 = $ arr2 ; $ arr2 = $ tempp ; $ temp = $ m ; $ m = $ n ; $ n = $ temp ; } sort ( $ arr1 ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ arr1 [ $ i ] . " ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( binarySearch ( $ arr1 , 0 , $ m - 1 , $ arr2 [ $ i ] ) == -1 ) echo $ arr2 [ $ i ] . " ▁ " ; } function printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) { if ( $ m > $ n ) { $ tempp = $ arr1 ; $ arr1 = $ arr2 ; $ arr2 = $ tempp ; $ temp = $ m ; $ m = $ n ; $ n = $ temp ; } sort ( $ arr1 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( binarySearch ( $ arr1 , 0 , $ m - 1 , $ arr2 [ $ i ] ) != -1 ) echo $ arr2 [ $ i ] . " ▁ " ; } function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = ( int ) ( $ l + ( $ r - $ l ) / 2 ) ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } $ arr1 = array ( 7 , 1 , 5 , 2 , 3 , 6 ) ; $ arr2 = array ( 3 , 8 , 6 , 20 , 7 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; echo " Union ▁ of ▁ two ▁ arrays ▁ is ▁
 " ; printUnion ( $ arr1 , $ arr2 , $ m , $ n ) ; echo " Intersection of two arrays is " ; printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >


"Find Unique pair in an array with pairs of numbers | PHP program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code"
How can the above be solved in PHP?

< ? php function findUniquePair ( $ arr , $ n ) { $ XOR = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ XOR = $ XOR ^ $ arr [ $ i ] ; $ set_bit_no = $ XOR & ~ ( $ XOR - 1 ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } echo " The ▁ unique ▁ pair ▁ is ▁ " , " ( " , $ x , " ▁ " , $ y , " ) " ; } $ a = array ( 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 ) ; $ n = count ( $ a ) ; findUniquePair ( $ a , $ n ) ; ? >


"Find XOR of two number without using XOR operator | Returns XOR of x and y ; Driver Code"
How can the above be solved in PHP?

< ? php function myXOR ( $ x , $ y ) { return ( $ x $ y ) & ( ~ $ x ~ $ y ) ; } $ x = 3 ; $ y = 5 ; echo " XOR ▁ is ▁ " , myXOR ( $ x , $ y ) ; ? >


"Find a Symmetric matrix of order N that contain integers from 0 to N | Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver code"
How can the above be solved in PHP?


"Find a distinct pair ( x , y ) in given range such that x divides y | Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code"
How can the above be solved in PHP?

< ? php function findpair ( $ l , $ r ) { $ ans1 = $ l ; $ ans2 = 2 * $ l ; echo ( $ ans1 . " , ▁ " . $ ans2 ) ; } $ l = 1 ; $ r = 10 ; findpair ( $ l , $ r ) ;


"Find a distinct pair ( x , y ) in given range such that x divides y | PHP implementation of the approach ; Driver code"
How can the above be solved in PHP?


"Find a number x such that sum of x and its digits is equal to given n . | utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it is equal to n . ; if no such i found return - 1 ; Driver Code"
How can the above be solved in PHP?

< ? php function digSum ( $ n ) { $ sum = 0 ; $ rem = 0 ; while ( $ n ) { $ rem = $ n % 10 ; $ sum += $ rem ; $ n /= 10 ; } return $ sum ; } function findX ( $ n ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( $ i + digSum ( $ i ) == $ n ) return $ i ; return -1 ; } $ n = 43 ; echo " x = " ? >


"Find a pair from the given array with maximum nCr value | Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code"
How can the above be solved in PHP?

< ? php function printMaxValPair ( $ v , $ n ) { sort ( $ v ) ; $ N = $ v [ $ n - 1 ] ; if ( $ N % 2 == 1 ) { $ first_maxima = $ N / 2 ; $ second_maxima = $ first_maxima + 1 ; $ ans1 = 3e18 ; $ ans2 = 3e18 ; $ from_left = -1 ; $ from_right = -1 ; $ from = -1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ v [ $ i ] > $ first_maxima ) { $ from = $ i ; break ; } else { $ diff = $ first_maxima - $ v [ $ i ] ; if ( $ diff < $ ans1 ) { $ ans1 = $ diff ; $ from_left = $ v [ $ i ] ; } } } $ from_right = $ v [ $ from ] ; $ diff1 = $ first_maxima - $ from_left ; $ diff2 = $ from_right - $ second_maxima ; if ( $ diff1 < $ diff2 ) echo $ N . " ▁ " . $ from_left ; else echo $ N . " ▁ " . $ from_right ; } else { $ maxima = $ N / 2 ; $ ans1 = 3e18 ; $ R = -1 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { $ diff = abs ( $ v [ $ i ] - $ maxima ) ; if ( $ diff < $ ans1 ) { $ ans1 = $ diff ; $ R = $ v [ $ i ] ; } } echo $ N . " ▁ " . $ R ; } } $ v = array ( 1 , 1 , 2 , 3 , 6 , 1 ) ; $ n = count ( $ v ) ; printMaxValPair ( $ v , $ n ) ; ? >


"Find a peak element | A binary search based function that returns index of a peak element ; Find index of middle element ( low + high ) / 2 ; Compare middle element with its neighbours ( if neighbours exist ) ; If middle element is not peak and its left neighbour is greater than it , then left half must have a peak element ; If middle element is not peak and its right neighbour is greater than it , then right half must have a peak element ; A wrapper over recursive function findPeakUtil ( ) ; Driver Code"
How can the above be solved in PHP?

< ? php function findPeakUtil ( $ arr , $ low , $ high , $ n ) { $ mid = $ low + ( $ high - $ low ) / 2 ; if ( ( $ mid == 0 $ arr [ $ mid - 1 ] <= $ arr [ $ mid ] ) && ( $ mid == $ n - 1 $ arr [ $ mid + 1 ] <= $ arr [ $ mid ] ) ) return $ mid ; else if ( $ mid > 0 && $ arr [ $ mid - 1 ] > $ arr [ $ mid ] ) return findPeakUtil ( $ arr , $ low , ( $ mid - 1 ) , $ n ) ; else return ( findPeakUtil ( $ arr , ( $ mid + 1 ) , $ high , $ n ) ) ; } function findPeak ( $ arr , $ n ) { return floor ( findPeakUtil ( $ arr , 0 , $ n - 1 , $ n ) ) ; } $ arr = array ( 1 , 3 , 20 , 4 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; echo " Index ▁ of ▁ a ▁ peak ▁ point ▁ is ▁ " , findPeak ( $ arr , $ n ) ; ? >


"Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver Code"
How can the above be solved in PHP?

< ? php function printPermutation ( $ n , $ k ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ x = 2 * $ i - 1 ; $ y = 2 * $ i ; if ( $ i <= $ k ) echo $ y . " " ▁ . ▁ $ x ▁ . ▁ " " else echo $ x . " ▁ " . $ y . " ▁ " ; } } $ n = 2 ; $ k = 1 ; printPermutation ( $ n , $ k ) ; ? >


"Find a point such that sum of the Manhattan distances is minimized | Function to print the required points which minimizes the sum of Manhattan distances ; Sorting points in all k dimension ; Output the required k points ; Driver code ; function call to print required points"
How can the above be solved in PHP?


"Find a range of composite numbers of given length | method to find factorial of given number ; to print range of length n having all composite integers ; Driver Code"
How can the above be solved in PHP?

< ? php function factorial ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * factorial ( $ n - 1 ) ; } function printRange ( $ n ) { $ a = factorial ( $ n + 2 ) + 2 ; $ b = $ a + $ n - 1 ; echo " [ " , $ a , " , ▁ " , $ b , " ] " ; return 0 ; } $ n = 3 ; printRange ( $ n ) ; ? >


"Find a rotation with maximum hamming distance | Return the maximum hamming distance of a rotation ; arr [ ] to brr [ ] two times so that we can traverse through all rotations . ; We know hamming distance with 0 rotation would be 0. ; We try other rotations one by one and compute Hamming distance of every rotation ; We can never get more than n . ; Driver Code"
How can the above be solved in PHP?

< ? php function maxHamming ( $ arr , $ n ) { $ brr = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ brr [ $ i ] = $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ brr [ $ n + $ i ] = $ arr [ $ i ] ; $ maxHam = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ currHam = 0 ; for ( $ j = $ i , $ k = 0 ; $ j < ( $ i + $ n ) ; $ j ++ , $ k ++ ) if ( $ brr [ $ j ] != $ arr [ $ k ] ) $ currHam ++ ; if ( $ currHam == $ n ) return $ n ; $ maxHam = max ( $ maxHam , $ currHam ) ; } return $ maxHam ; } $ arr = array ( 2 , 4 , 6 , 80 ) ; $ n = count ( $ arr ) ; echo maxHamming ( $ arr , $ n ) ; ? >


"Find a triplet that sum to a given value | returns true if there is triplet with sum equal to ' sum ' present in A [ ] . Also , prints the triplet ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; If we reach here , then no triplet was found ; Driver Code"
How can the above be solved in PHP?

< ? php function find3Numbers ( $ A , $ arr_size , $ sum ) { $ l ; $ r ; for ( $ i = 0 ; $ i < $ arr_size - 2 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ arr_size - 1 ; $ j ++ ) { for ( $ k = $ j + 1 ; $ k < $ arr_size ; $ k ++ ) { if ( $ A [ $ i ] + $ A [ $ j ] + $ A [ $ k ] == $ sum ) { echo " Triplet ▁ is " , " ▁ " , $ A [ $ i ] , " , ▁ " , $ A [ $ j ] , " , ▁ " , $ A [ $ k ] ; return true ; } } } } return false ; } $ A = array ( 1 , 4 , 45 , 6 , 10 , 8 ) ; $ sum = 22 ; $ arr_size = sizeof ( $ A ) ; find3Numbers ( $ A , $ arr_size , $ sum ) ; ? >


"Find a triplet that sum to a given value | returns true if there is triplet with sum equal to ' sum ' present in A [ ] . Also , prints the triplet ; Sort the elements ; Now fix the first element one by one and find the other two elements ; To find the other two elements , start two index variables from two corners of the array and move them toward each other index of the first element ; in the remaining elements index of the last element ; A [ i ] + A [ l ] + A [ r ] > sum ; If we reach here , then no triplet was found ; Driver Code"
How can the above be solved in PHP?

 < ? php function find3Numbers ( $ A , $ arr_size , $ sum ) { $ l ; $ r ; sort ( $ A ) ; for ( $ i = 0 ; $ i < $ arr_size - 2 ; $ i ++ ) { $ l = $ i + 1 ; $ r = $ arr_size - 1 ; while ( $ l < $ r ) { if ( $ A [ $ i ] + $ A [ $ l ] + $ A [ $ r ] == $ sum ) { echo " Triplet ▁ is ▁ " , $ A [ $ i ] , " ▁ " , $ A [ $ l ] , " ▁ " , $ A [ $ r ] , "
 " ; return true ; } else if ( $ A [ $ i ] + $ A [ $ l ] + $ A [ $ r ] < $ sum ) $ l ++ ; else $ r -- ; } } return false ; } $ A = array ( 1 , 4 , 45 , 6 , 10 , 8 ) ; $ sum = 22 ; $ arr_size = sizeof ( $ A ) ; find3Numbers ( $ A , $ arr_size , $ sum ) ; ? >


"Find a value whose XOR with given number is maximum | Function To Calculate Answer ; Find number of bits in the given integer ; XOR the given integer with poe ( 2 , number_of_bits - 1 and print the result ; Driver Code"
How can the above be solved in PHP?

 < ? php function calculate ( $ X ) { $ number_of_bits = 8 ; return ( ( 1 << $ number_of_bits ) - 1 ) ^ $ X ; } $ X = 4 ; echo " Required ▁ Number ▁ is ▁ : ▁ " . calculate ( $ X ) . "
 " ; ? >


"Find all angles of a triangle in 3D | function for finding the angle ; Driver code"
How can the above be solved in PHP?


"Find all combinations that add upto given number | arr - array to store the combination index - next location in array num - given number reducedNum - reduced number ; Base condition ; If combination is found , print it ; Find the previous number stored in arr [ ] It helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ) ; array to store the combinations It can contain max n elements ; find all combinations ; Driver code"
How can the above be solved in PHP?

 < ? php function findCombinationsUtil ( $ arr , $ index , $ num , $ reducedNum ) { if ( $ reducedNum < 0 ) return ; if ( $ reducedNum == 0 ) { for ( $ i = 0 ; $ i < $ index ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; echo "
 " ; return ; } $ prev = ( $ index == 0 ) ? 1 : $ arr [ $ index - 1 ] ; for ( $ k = $ prev ; $ k <= $ num ; $ k ++ ) { $ arr [ $ index ] = $ k ; findCombinationsUtil ( $ arr , $ index + 1 , $ num , $ reducedNum - $ k ) ; } } function findCombinations ( $ n ) { $ arr = array ( ) ; findCombinationsUtil ( $ arr , 0 , $ n , $ n ) ; } $ n = 5 ; findCombinations ( $ n ) ; ? >


"Find all factorial numbers less than or equal to n | PHP program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code"
How can the above be solved in PHP?

< ? php function printFactorialNums ( $ n ) { $ fact = 1 ; $ x = 2 ; while ( $ fact <= $ n ) { echo $ fact , " " ; $ fact = $ fact * $ x ; $ x ++ ; } } $ n = 100 ; echo printFactorialNums ( $ n ) ; ? >


"Find all pairs ( a , b ) in an array such that a % b = k | Function to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver Code"
How can the above be solved in PHP?

< ? php function printPairs ( $ arr , $ n , $ k ) { $ isPairFound = true ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i != $ j && $ arr [ $ i ] % $ arr [ $ j ] == $ k ) { echo " ( " , $ arr [ $ i ] , " , ▁ " , $ arr [ $ j ] , " ) " , " ▁ " ; $ isPairFound = true ; } } } return $ isPairFound ; } $ arr = array ( 2 , 3 , 5 , 4 , 7 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; if ( printPairs ( $ arr , $ n , $ k ) == false ) echo " No ▁ such ▁ pair ▁ exists " ; ? >


"Find all possible coordinates of parallelogram | coordinates of A ; coordinates of B ; coordinates of C"
How can the above be solved in PHP?


"Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver Code"
How can the above be solved in PHP?

 < ? php $ eps = .0000001 ; function getArea ( $ base , $ hypotenuse ) { $ height = sqrt ( $ hypotenuse * $ hypotenuse - $ base * $ base ) ; return 0.5 * $ base * $ height ; } function printRightAngleTriangle ( $ hypotenuse , $ area ) { global $ eps ; $ hsquare = $ hypotenuse * $ hypotenuse ; $ sideForMaxArea = sqrt ( $ hsquare / 2.0 ) ; $ maxArea = getArea ( $ sideForMaxArea , $ hypotenuse ) ; if ( $ area > $ maxArea ) { echo " Not ▁ possiblen " ; return ; } $ low = 0.0 ; $ high = $ sideForMaxArea ; $ base ; while ( abs ( $ high - $ low ) > $ eps ) { $ base = ( $ low + $ high ) / 2.0 ; if ( getArea ( $ base , $ hypotenuse ) >= $ area ) $ high = $ base ; else $ low = $ base ; } $ height = sqrt ( $ hsquare - $ base * $ base ) ; echo ( ceil ( $ base ) ) , " ▁ " , ( floor ( $ height ) ) , "
 " ; } $ hypotenuse = 5 ; $ area = 6 ; printRightAngleTriangle ( $ hypotenuse , $ area ) ; ? >


"Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ ▁ we ▁ got ▁ a ▁ ' 1 ', counter  incremented ; Last character stored ; Driver Code"
How can the above be solved in PHP?

< ? php function patternCount ( $ str ) { $ last = $ str [ 0 ] ; $ i = 1 ; $ counter = 0 ; while ( $ i < strlen ( $ str ) ) { if ( $ str [ $ i ] == '0' && $ last == '1' ) { while ( $ str [ $ i ] == '0' ) $ i ++ ; if ( $ str [ $ i ] == '1' ) $ counter ++ ; } $ last = $ str [ $ i ] ; $ i ++ ; } return $ counter ; } $ str = "1001ab010abc01001" ; echo patternCount ( $ str ) ; ? >


"Find amount of water wasted after filling the tank | Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code"
How can the above be solved in PHP?

 < ? php function wastedWater ( $ V , $ M , $ N ) { $ amt_per_min = $ M - $ N ; $ time_to_fill = $ V / $ amt_per_min ; $ wasted_amt = $ N * $ time_to_fill ; return $ wasted_amt ; } $ V = 700 ; $ M = 10 ; $ N = 3 ; echo wastedWater ( $ V , $ M , $ N ) , "
 " ; $ V = 1000 ; $ M = 100 ; $ N = 50 ; echo wastedWater ( $ V , $ M , $ N ) ;


"Find amount to be added to achieve target ratio in a given mixture | PHP program to find amount of water to be added to achieve given target ratio . ; Driver Code"
How can the above be solved in PHP?

< ? php function findAmount ( $ X , $ W , $ Y ) { return ( $ X * ( $ Y - $ W ) ) / ( 100 - $ Y ) ; } $ X = 100 ; $ W = 50 ; $ Y = 60 ; echo " Water ▁ to ▁ be ▁ added ▁ = ▁ " . findAmount ( $ X , $ W , $ Y ) ; ? >


"Find an array element such that all elements are divisible by it | function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver Code"
How can the above be solved in PHP?

< ? php function findSmallest ( $ a , $ n ) { $ smallest = min ( $ a ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] % $ smallest ) return -1 ; return $ smallest ; } $ a = array ( 25 , 20 , 5 , 10 , 100 ) ; $ n = count ( $ a ) ; echo findSmallest ( $ a , $ n ) ; ? >


"Find an array element such that all elements are divisible by it | function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; Driver Code"
How can the above be solved in PHP?

< ? php function findSmallest ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ a [ $ j ] % $ a [ $ i ] ) break ; if ( $ j == $ n ) return $ a [ $ i ] ; } return -1 ; } $ a = array ( 25 , 20 , 5 , 10 , 100 ) ; $ n = sizeof ( $ a ) ; echo findSmallest ( $ a , $ n ) ; ? >


"Find an equal point in a string of brackets | Method to find an equal index ; Store the number of opening brackets at each index ; Store the number of closing brackets at each index ; check if there is no opening or closing brackets ; check if there is any index at which both brackets are equal ; Driver Code"
How can the above be solved in PHP?


"Find an index such that difference between product of elements before and after it is minimum | Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code"
How can the above be solved in PHP?

< ? php function findIndex ( $ a , $ n ) { $ min_diff = PHP_INT_MAX ; $ prod = array ( ) ; $ prod [ 0 ] = $ a [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ prod [ $ i ] = $ prod [ $ i - 1 ] * $ a [ $ i ] ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ curr_diff = abs ( ( $ prod [ $ n - 1 ] / $ prod [ $ i ] ) - $ prod [ $ i ] ) ; if ( $ curr_diff < $ min_diff ) { $ min_diff = $ curr_diff ; $ res = $ i ; } } return $ res ; } $ arr = array ( 3 , 2 , 5 , 7 , 2 , 9 ) ; $ N = count ( $ arr ) ; echo findIndex ( $ arr , $ N ) ; ? >


"Find any pair with given GCD and LCM | Function to print the pairs ; Driver Code"
How can the above be solved in PHP?

< ? php function printPair ( $ g , $ l ) { echo $ g ; echo ( " ▁ " ) ; echo $ l ; } $ g = 3 ; $ l = 12 ; printPair ( $ g , $ l ) ; ? >


"Find area of parallelogram if vectors of two adjacent sides are given | Function to calculate area of parallelogram ; Driver Code"
How can the above be solved in PHP?


"Find area of the larger circle when radius of the smaller circle and difference in the area is given | PHP implementation of the approach ; Function to return the area of the bigger circle ; Find the radius of the bigger circle ; Calculate the area of the bigger circle ; Driver Code"
How can the above be solved in PHP?

< ? php const PI = 3.14 ; function find_area ( $ r , $ d ) { $ R = $ d / PI ; $ R += pow ( $ r , 2 ) ; $ R = sqrt ( $ R ) ; $ area = PI * pow ( $ R , 2 ) ; return $ area ; } $ r = 4 ; $ d = 5 ; echo ( find_area ( $ r , $ d ) ) ; ? >


"Find area of triangle if two vectors of two adjacent sides are given | function to calculate area of triangle ; Driver Code"
How can the above be solved in PHP?


"Find array using different XORs of elements in groups of size 4 | Utility function to print the contents of the array ; Function to find the required array ; Print the array ; Driver code"
How can the above be solved in PHP?


"Find bitonic point in given bitonic sequence | Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver Code"
How can the above be solved in PHP?

< ? php function binarySearch ( $ arr , $ left , $ right ) { if ( $ left <= $ right ) { $ mid = ( $ left + $ right ) / 2 ; if ( $ arr [ $ mid - 1 ] < $ arr [ $ mid ] && $ arr [ $ mid ] > $ arr [ $ mid + 1 ] ) return $ mid ; if ( $ arr [ $ mid ] < $ arr [ $ mid + 1 ] ) return binarySearch ( $ arr , $ mid + 1 , $ right ) ; else return binarySearch ( $ arr , $ left , $ mid - 1 ) ; } return -1 ; } $ arr = array ( 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 ) ; $ n = sizeof ( $ arr ) ; $ index = binarySearch ( $ arr , 1 , $ n - 2 ) ; if ( $ index != -1 ) echo $ arr [ $ index ] ; ? >


"Find bitwise AND ( & ) of all possible sub | function to return AND of sub - arrays ; Driver Code ; size of the array ; print and of all subarrays"
How can the above be solved in PHP?

< ? php function ANDS ( & $ a , $ n ) { $ ans = $ a [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ ans &= $ a [ $ i ] ; return $ ans ; } $ a = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ a ) ; echo ANDS ( $ a , $ n ) ; ? >


"Find bitwise OR of all possible sub | function to return OR of sub - arrays ; Driver Code ; print OR of all subarrays"
How can the above be solved in PHP?


"Find ceil of a / b without using ceil ( ) function | taking input 1 ; example of perfect division taking input 2"
How can the above be solved in PHP?

 < ? php $ a = 4 ; $ b = 3 ; $ val = ( $ a + $ b - 1 ) / $ b ; echo " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " , floor ( $ val ) , "
 " ; $ a = 6 ; $ b = 3 ; $ val = ( $ a + $ b - 1 ) / $ b ; echo " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " , floor ( $ val ) ; ? >


 < ? php $ a = 4 ; $ b = 3 ; $ val = ( $ a / $ b ) + ( ( $ a % $ b ) != 0 ) ; echo " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " , floor ( $ val ) , "
 " ; $ a = 6 ; $ b = 3 ; $ val = ( $ a / $ b ) + ( ( $ a % $ b ) != 0 ) ; echo " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " , $ val ; ? >


"Find closest number in array | Returns element closest to target in arr [ ] ; Corner cases ; Doing binary search ; If target is less than array element , then search in left ; If target is greater than previous to mid , return closest of two ; Repeat for left half ; If target is greater than mid ; update i ; Only single element left after search ; Method to compare which one is the more close . We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two . ; Driver code"
How can the above be solved in PHP?

< ? php function findClosest ( $ arr , $ n , $ target ) { if ( $ target <= $ arr [ 0 ] ) return $ arr [ 0 ] ; if ( $ target >= $ arr [ $ n - 1 ] ) return $ arr [ $ n - 1 ] ; $ i = 0 ; $ j = $ n ; $ mid = 0 ; while ( $ i < $ j ) { $ mid = ( $ i + $ j ) / 2 ; if ( $ arr [ $ mid ] == $ target ) return $ arr [ $ mid ] ; if ( $ target < $ arr [ $ mid ] ) { if ( $ mid > 0 && $ target > $ arr [ $ mid - 1 ] ) return getClosest ( $ arr [ $ mid - 1 ] , $ arr [ $ mid ] , $ target ) ; $ j = $ mid ; } else { if ( $ mid < $ n - 1 && $ target < $ arr [ $ mid + 1 ] ) return getClosest ( $ arr [ $ mid ] , $ arr [ $ mid + 1 ] , $ target ) ; $ i = $ mid + 1 ; } } return $ arr [ $ mid ] ; } function getClosest ( $ val1 , $ val2 , $ target ) { if ( $ target - $ val1 >= $ val2 - $ target ) return $ val2 ; else return $ val1 ; } $ arr = array ( 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 ) ; $ n = sizeof ( $ arr ) ; $ target = 11 ; echo ( findClosest ( $ arr , $ n , $ target ) ) ; ? >


"Find combined mean and variance of two series | Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver Code ; Function call to combined mean ."
How can the above be solved in PHP?

 < ? php function mean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; $ mean = ( float ) ( $ sum / $ n ) ; return $ mean ; } function sd ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) * ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) ; $ sdd = $ sum / $ n ; return $ sdd ; } function combinedVariance ( $ arr1 , $ arr2 , $ n , $ m ) { $ mean1 = mean ( $ arr1 , $ n ) ; $ mean2 = mean ( $ arr2 , $ m ) ; echo ( " Mean1 : ▁ " . round ( $ mean1 , 2 ) . " " ▁ .
 	 	 " mean2 : " $ sd1 = sd ( $ arr1 , $ n ) ; $ sd2 = sd ( $ arr2 , $ m ) ; echo ( " StandardDeviation1 : " ▁ . ▁ round ( $ sd1 , ▁ 2 ) ▁ . ▁ " "
 	 	 . ▁ " StandardDeviation2 : " $ combinedMean = ( float ) ( $ n * $ mean1 + $ m * $ mean2 ) / ( $ n + $ m ) ; echo ( " Combined Mean : " round ( $ combinedMean , 2 ) ) ; $ d1_square = ( $ mean1 - $ combinedMean ) * ( $ mean1 - $ combinedMean ) ; $ d2_square = ( $ mean2 - $ combinedMean ) * ( $ mean2 - $ combinedMean ) ; echo ( " d1 square : " ▁ . ▁ round ( $ d1 _ square , ▁ 2 ) ▁ . ▁ " "
 	 	 . ▁ " d2_square : " $ combinedVar = ( $ n * ( $ sd1 + $ d1_square ) + $ m * ( $ sd2 + $ d2_square ) ) / ( $ n + $ m ) ; return $ combinedVar ; } $ arr1 = array ( 23 , 45 , 34 , 78 , 12 , 76 , 34 ) ; $ arr2 = array ( 65 , 67 , 34 , 23 , 45 ) ; $ n = sizeof ( $ arr1 ) ; $ m = sizeof ( $ arr2 ) ; echo ( " Combined Variance : " . round ( combinedVariance ( $ arr1 , $ arr2 , $ n , $ m ) , 2 ) ) ; ? >


"Find common elements in three sorted arrays | This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver program to test above function"
How can the above be solved in PHP?


"Find coordinates of the triangle given midpoint of each side | PHP program to find coordinate of the triangle given midpoint of each side ; Return after solving the equations and finding the vertices coordinate . ; Finding sum of all three coordinate . ; Solving the equation . ; Finds vertices of a triangles from given middle vertices . ; Find X coordinates of vertices . ; Find Y coordinates of vertices . ; Output the solution . ; Driver code"
How can the above be solved in PHP?

 < ? php $ N = 3 ; function solve ( $ v ) { $ res = array ( ) ; $ all3 = $ v [ 0 ] + $ v [ 1 ] + $ v [ 2 ] ; array_push ( $ res , $ all3 - $ v [ 1 ] * 2 ) ; array_push ( $ res , $ all3 - $ v [ 2 ] * 2 ) ; array_push ( $ res , $ all3 - $ v [ 0 ] * 2 ) ; return $ res ; } function findVertex ( $ xmid , $ ymid ) { $ V1 = solve ( $ xmid ) ; $ V2 = solve ( $ ymid ) ; for ( $ i = 0 ; $ i < 3 ; $ i ++ ) print ( $ V1 [ $ i ] . " ▁ " . $ V2 [ $ i ] . "
 " ) ; } $ xmid = array ( 5 , 4 , 5 ) ; $ ymid = array ( 3 , 4 , 5 ) ; findVertex ( $ xmid , $ ymid ) ? >


"Find cost price from given selling price and profit or loss percentage | Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code"
How can the above be solved in PHP?

 < ? php function CPwithProfit ( $ sellingPrice , $ profit ) { $ costPrice = ( $ sellingPrice * 100.0 ) / ( 100 + $ profit ) ; return $ costPrice ; } function CPwithLoss ( $ sellingPrice , $ loss ) { $ costPrice = ( $ sellingPrice * 100.0 ) / ( 100 - $ loss ) ; return $ costPrice ; } $ SP = 1020 ; $ profit = 20 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithProfit ( $ SP , $ profit ) ) ; echo ( "
 " ) ; $ SP = 900 ; $ loss = 10 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithLoss ( $ SP , $ loss ) ) ; echo ( "
 " ) ; $ SP = 42039 ; $ profit = 8 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithProfit ( $ SP , $ profit ) ) ; echo ( "
 " ) ; ? >


"Find count of digits in a number that divide the number | Return the number of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver Code"
How can the above be solved in PHP?

 < ? php function countDigit ( $ n ) { $ temp = $ n ; $ count = 0 ; while ( $ temp != 0 ) { $ d = $ temp % 10 ; $ temp /= 10 ; if ( $ d > 0 && $ n % $ d == 0 ) $ count ++ ; } return $ count ; } $ n = 1012 ; echo countDigit ( $ n ) , "
 " ; ? >


"Find cubic root of a number | Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver Code"
How can the above be solved in PHP?

< ? php function diff ( $ n , $ mid ) { if ( $ n > ( $ mid * $ mid * $ mid ) ) return ( $ n - ( $ mid * $ mid * $ mid ) ) ; else return ( ( $ mid * $ mid * $ mid ) - $ n ) ; } function cubicRoot ( $ n ) { $ start = 0 ; $ end = $ n ; $ e = 0.0000001 ; while ( true ) { $ mid = ( ( $ start + $ end ) / 2 ) ; $ error = diff ( $ n , $ mid ) ; if ( $ error <= $ e ) return $ mid ; if ( ( $ mid * $ mid * $ mid ) > $ n ) $ end = $ mid ; else $ start = $ mid ; } } $ n = 3 ; echo ( " Cubic ▁ root ▁ of ▁ $ n ▁ is ▁ " ) ; echo ( cubicRoot ( $ n ) ) ; ? >


"Find determinant of matrix generated by array rotation | PHP program for finding determinant of generated matrix ; Function to calculate determinant ; Driver code"
How can the above be solved in PHP?

< ? php $ N = 3 ; function calcDeterminant ( $ arr ) { global $ N ; $ determinant = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ determinant += pow ( $ arr [ $ i ] , 3 ) ; } $ determinant -= 3 * $ arr [ 0 ] * $ arr [ 1 ] * $ arr [ 2 ] ; return $ determinant ; } $ arr = array ( 4 , 5 , 3 ) ; echo calcDeterminant ( $ arr ) ; ? >


"Find difference between sums of two diagonals | PHP program to find the difference between the sum of diagonal . ; Initialize sums of diagonals ; Absolute difference of the sums across the diagonals ; Driver Code"
How can the above be solved in PHP?


"Find difference between sums of two diagonals | PHP program to find the difference between the sum of diagonal . ; Initialize sums of diagonals ; finding sum of primary diagonal ; finding sum of secondary diagonal ; Absolute difference of the sums across the diagonals ; Driver Code"
How can the above be solved in PHP?


"Find duplicate in an array in O ( n ) and by using O ( 1 ) extra space | Function to find duplicate ; Find the intersection point of the slow and fast . ; Find the " entrance " to the cycle . ; Driver code"
How can the above be solved in PHP?

< ? php function findDuplicate ( & $ arr ) { $ slow = $ arr [ 0 ] ; $ fast = $ arr [ 0 ] ; do { $ slow = $ arr [ $ slow ] ; $ fast = $ arr [ $ arr [ $ fast ] ] ; } while ( $ slow != $ fast ) ; $ ptr1 = $ arr [ 0 ] ; $ ptr2 = $ slow ; while ( $ ptr1 != $ ptr2 ) { $ ptr1 = $ arr [ $ ptr1 ] ; $ ptr2 = $ arr [ $ ptr2 ] ; } return $ ptr1 ; } $ arr = array ( 1 , 3 , 2 , 1 ) ; echo " ▁ " . findDuplicate ( $ arr ) ; ? >


"Find element at given index after a number of rotations | Function to compute the element at given index ; Range [ left ... right ] ; Rotation will not have any effect ; Returning new element ; Driver Code ; No . of rotations ; Ranges according to 0 - based indexing"
How can the above be solved in PHP?

< ? php function findElement ( $ arr , $ ranges , $ rotations , $ index ) { for ( $ i = $ rotations - 1 ; $ i >= 0 ; $ i -- ) { $ left = $ ranges [ $ i ] [ 0 ] ; $ right = $ ranges [ $ i ] [ 1 ] ; if ( $ left <= $ index && $ right >= $ index ) { if ( $ index == $ left ) $ index = $ right ; else $ index -- ; } } return $ arr [ $ index ] ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ rotations = 2 ; $ ranges = array ( array ( 0 , 2 ) , array ( 0 , 3 ) ) ; $ index = 1 ; echo findElement ( $ arr , $ ranges , $ rotations , $ index ) ; ? >


"Find element in a sorted array whose frequency is greater than or equal to n / 2. | PHP code to find majority element in a sorted array ; Driver Code"
How can the above be solved in PHP?

< ? php function findMajority ( $ arr , $ n ) { return $ arr [ intval ( $ n / 2 ) ] ; } $ arr = array ( 1 , 2 , 2 , 3 ) ; $ n = count ( $ arr ) ; echo findMajority ( $ arr , $ n ) ; ? >


"Find element in array that divides all array elements | Returns gcd of two numbers ; Function to return the desired number if exists ; Find GCD of array ; Check if GCD is present in array ; Driver Code"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findNumber ( $ arr , $ n ) { $ ans = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ ans = gcd ( $ ans , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ ans ) return $ ans ; return -1 ; } $ arr = array ( 2 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; echo findNumber ( $ arr , $ n ) , "
 " ; ? >


"Find element position in given monotonic sequence | PHP implementation of the approach from math import log2 , floor ; Function to return the value of f ( n ) for given values of a , b , c , n ; if c is 0 , then value of n can be in order of 10 ^ 15. if c != 0 , then n ^ 3 value has to be in order of 10 ^ 18 so maximum value of n can be 10 ^ 6. ; for efficient searching , use binary search . ; Driver code"
How can the above be solved in PHP?

< ? php $ SMALL_N = 1000000 ; $ LARGE_N = 1000000000000000 ; function func ( $ a , $ b , $ c , $ n ) { $ res = $ a * $ n ; $ logVlaue = floor ( log ( $ n , 2 ) ) ; $ res += $ b * $ n * $ logVlaue ; $ res += $ c * ( $ n * $ n * $ n ) ; return $ res ; } function getPositionInSeries ( $ a , $ b , $ c , $ k ) { global $ SMALL_N , $ LARGE_N ; $ start = 1 ; $ end = $ SMALL_N ; if ( $ c == 0 ) $ end = $ LARGE_N ; $ ans = 0 ; while ( $ start <= $ end ) { $ mid = ( int ) ( ( $ start + $ end ) / 2 ) ; $ val = func ( $ a , $ b , $ c , $ mid ) ; if ( $ val == $ k ) { $ ans = $ mid ; break ; } else if ( $ val > $ k ) $ end = $ mid - 1 ; else $ start = $ mid + 1 ; } return $ ans ; } $ a = 2 ; $ b = 1 ; $ c = 1 ; $ k = 12168587437017 ; print ( getPositionInSeries ( $ a , $ b , $ c , $ k ) ) ; ? >


"Find element using minimum segments in Seven Segment Display | Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driver Code"
How can the above be solved in PHP?

< ? php $ seg = array ( 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ) ; function computeSegment ( $ x ) { global $ seg ; if ( $ x == 0 ) return $ seg [ 0 ] ; $ count = 0 ; while ( $ x ) { $ count += $ seg [ $ x % 10 ] ; $ x = ( int ) $ x / 10 ; } return $ count ; } function elementMinSegment ( $ arr , $ n ) { $ minseg = computeSegment ( $ arr [ 0 ] ) ; $ minindex = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ temp = computeSegment ( $ arr [ $ i ] ) ; if ( $ temp < $ minseg ) { $ minseg = $ temp ; $ minindex = $ i ; } } return $ arr [ $ minindex ] ; } $ arr = array ( 489 , 206 , 745 , 123 , 756 ) ; $ n = sizeof ( $ arr ) ; echo elementMinSegment ( $ arr , $ n ) , " " ; ? >


"Find element with the maximum set bits in an array | Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code"
How can the above be solved in PHP?

< ? php function maxBitElement ( $ arr , $ n ) { $ num = 0 ; $ max = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ cnt = BitCount ( $ arr [ $ i ] ) ; if ( $ cnt > $ max ) { $ max = $ cnt ; $ num = $ arr [ $ i ] ; } } return $ num ; } function BitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; } $ arr = array ( 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 ) ; $ n = count ( $ arr ) ; echo ( maxBitElement ( $ arr , $ n ) ) ; ? >


"Find elements larger than half of the elements in an array | Prints elements larger than n / 2 element ; Sort the array in ascending order ; Print last ceil ( n / 2 ) elements ; Driver Code"
How can the above be solved in PHP?

< ? php function findLarger ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = $ n - 1 ; $ i >= $ n / 2 ; $ i -- ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 1 , 3 , 6 , 1 , 0 , 9 ) ; $ n = count ( $ arr ) ; findLarger ( $ arr , $ n ) ; ? >


"Find elements of array using XOR of consecutive elements | Function to find the array elements using XOR of consecutive elements ; first element a i . e elements [ 0 ] = a ; To get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; Printing the original array elements ; Driver Code"
How can the above be solved in PHP?

< ? php function getElements ( $ a , & $ arr , & $ n ) { $ elements [ 0 ] = $ a ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ elements [ $ i + 1 ] = $ arr [ $ i ] ^ $ elements [ $ i ] ; } for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) { echo ( $ elements [ $ i ] . " " ) ; } } $ arr = array ( 13 , 2 , 6 , 1 ) ; $ n = sizeof ( $ arr ) ; $ a = 5 ; getElements ( $ a , $ arr , $ n ) ; ? >


"Find elements which are present in first array and not in second | Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver code"
How can the above be solved in PHP?


"Find even occurring elements in an array of limited range | Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code"
How can the above be solved in PHP?


"Find final value if we double after every successful search in array | Function to Find the value of k ; Search for k . After every successful search , double k . ; Driver Code"
How can the above be solved in PHP?

< ? php function findValue ( $ arr , $ n , $ k ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ k ) $ k *= 2 ; return $ k ; } $ arr = array ( 2 , 3 , 4 , 10 , 8 , 1 ) ; $ k = 2 ; $ n = count ( $ arr ) ; echo findValue ( $ arr , $ n , $ k ) ; ? >


"Find first and last digits of a number | Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver Code"
How can the above be solved in PHP?

< ? php function firstDigit ( $ n ) { $ digits = ( int ) log10 ( $ n ) ; $ n = ( int ) ( $ n / pow ( 10 , $ digits ) ) ; return $ n ; } function lastDigit ( $ n ) { return ( $ n % 10 ) ; } $ n = 98562 ; echo firstDigit ( $ n ) , " " , lastDigit ( $ n ) , " " ; ? >


"Find first and last digits of a number | Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver Code"
How can the above be solved in PHP?

 < ? php function firstDigit ( $ n ) { while ( $ n >= 10 ) $ n /= 10 ; return ( int ) $ n ; } function lastDigit ( $ n ) { return ( ( int ) $ n % 10 ) ; } $ n = 98562 ; echo firstDigit ( $ n ) . " ▁ " . lastDigit ( $ n ) . "
 " ;


"Find first k natural numbers missing in given array | Prints first k natural numbers in arr [ 0. . n - 1 ] ; Find first positive number ; Now find missing numbers between array elements ; Find missing numbers after maximum . ; Driver code"
How can the above be solved in PHP?

< ? php function printKMissing ( $ arr , $ n , $ k ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ i = 0 ; while ( $ i < $ n && $ arr [ $ i ] <= 0 ) $ i ++ ; $ count = 0 ; $ curr = 1 ; while ( $ count < $ k && $ i < $ n ) { if ( $ arr [ $ i ] != $ curr ) { echo $ curr , " " ; $ count ++ ; } else $ i ++ ; $ curr ++ ; } while ( $ count < $ k ) { echo $ curr , " " ; $ curr ++ ; $ count ++ ; } } $ arr = array ( 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; printKMissing ( $ arr , $ n , $ k ) ; ? >


"Find four elements that sum to a given value | Set 1 ( n ^ 3 solution ) | A naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; Fix the first element and find other three ; Fix the second element and find other two ; Fix the third element and find the fourth ; find the fourth ; Driver program to test above function"
How can the above be solved in PHP?


"Find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Finds GCD of a and b ; Finds GCD of a ^ n and c ; check if c is a divisor of a ; First compute ( a ^ n ) % c ; Now simply return GCD of modulo power and c . ; Driver code"
How can the above be solved in PHP?

< ? php function modPower ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } function gcdPow ( $ a , $ n , $ c ) { if ( $ a % $ c == 0 ) return $ c ; $ modexpo = modPower ( $ a , $ n , $ c ) ; return gcd ( $ modexpo , $ c ) ; } $ a = 10248585 ; $ n = 1000000 ; $ c = 12564 ; echo gcdPow ( $ a , $ n , $ c ) ; ? >


"Find if a molecule can be formed from 3 atoms using their valence numbers | Function to check if it is possible ; Driver code"
How can the above be solved in PHP?

< ? php function printPossible ( $ a , $ b , $ c ) { if ( ( $ a + $ b + $ c ) % 2 != 0 $ a + $ b < $ c ) echo ( " NO " ) ; else echo ( " YES " ) ; } $ a = 2 ; $ b = 4 ; $ c = 2 ; printPossible ( $ a , $ b , $ c ) ; ? >


"Find if a number is divisible by every number in a list | Function which check is a number divided with every element in list or not ; Driver Code"
How can the above be solved in PHP?

< ? php function findNoIsDivisibleOrNot ( $ a , $ n , $ l ) { for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ a [ $ i ] % $ n != 0 ) return false ; } return true ; } $ a = array ( 14 , 12 , 4 , 18 ) ; $ n = 2 ; $ l = sizeof ( $ a ) ; if ( findNoIsDivisibleOrNot ( $ a , $ n , $ l ) ) echo " Yes " ; else echo " No " ; ? >


"Find if a number is part of AP whose first element and difference are given | returns yes if exist else no . ; If difference is 0 , then x must be same as a ; Else difference between x and a must be divisible by d . ; Driver code ."
How can the above be solved in PHP?

< ? php function isMember ( $ a , $ d , $ x ) { if ( $ d == 0 ) return ( $ x == $ a ) ; return ( ( $ x - $ a ) % $ d == 0 && ( $ x - $ a ) / $ d >= 0 ) ; } $ a = 1 ; $ x = 7 ; $ d = 3 ; if ( isMember ( $ a , $ d , $ x ) ) echo " Yes " ; else echo " No " ; ? >


"Find if a string starts and ends with another given string | PHP program to find if a given corner string is present at corners . ; If length of corner string is more , it cannot be present at corners . ; Return true if corner string is present at both corners of given string . ; Driver Code"
How can the above be solved in PHP?

< ? php function isCornerPresent ( $ str , $ corner ) { $ n = strlen ( $ str ) ; $ cl = strlen ( $ corner ) ; if ( $ n < $ cl ) return false ; return ( ! strcmp ( substr ( $ str , 0 , $ cl ) , $ corner ) && ! strcmp ( substr ( $ str , $ n - $ cl , $ cl ) , $ corner ) ) ; } $ str = " geeksforgeeks " ; $ corner = " geeks " ; if ( isCornerPresent ( $ str , $ corner ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Find if given matrix is Toeplitz or not | Function to check if all elements present in descending diagonal starting from position ( i , j ) in the matrix are all same or not ; mismatch found ; we only reach here when all elements in given diagonal are same ; Function to check whether given matrix is a Toeplitz matrix or not ; do for each element in first row ; check descending diagonal starting from position ( 0 , j ) in the matrix ; do for each element in first column ; check descending diagonal starting from position ( i , 0 ) in the matrix ; we only reach here when each descending diagonal from left to right is same ; Driver code ; Function call"
How can the above be solved in PHP?

< ? php function checkDiagonal ( $ mat , $ i , $ j ) { $ N = 5 ; $ M = 4 ; $ res = $ mat [ $ i ] [ $ j ] ; while ( ++ $ i < $ N && ++ $ j < $ M ) { if ( $ mat [ $ i ] [ $ j ] != $ res ) return false ; } return true ; } function isToepliz ( $ mat ) { $ N = 5 ; $ M = 4 ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { if ( ! checkDiagonal ( $ mat , 0 , $ i ) ) return false ; } for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { if ( ! checkDiagonal ( $ mat , $ i , 0 ) ) return false ; } return true ; } $ mat = array ( array ( 6 , 7 , 8 , 9 ) , array ( 4 , 6 , 7 , 8 ) , array ( 1 , 4 , 6 , 7 ) , array ( 0 , 1 , 4 , 6 ) , array ( 2 , 0 , 1 , 4 ) ) ; if ( isToepliz ( $ mat ) ) echo " Matrix ▁ is ▁ a ▁ Toepliz ▁ " ; else echo " Matrix ▁ is ▁ not ▁ a ▁ Toepliz ▁ " ; ? >


"Find if given number is sum of first n natural numbers | Function to find no . of elements to be added from 1 to get n ; Start adding numbers from 1 ; If sum becomes equal to s return n ; Drivers code"
How can the above be solved in PHP?

< ? php function findS ( $ s ) { $ sum = 0 ; for ( $ n = 1 ; $ sum < $ s ; $ n ++ ) { $ sum += $ n ; if ( $ sum == $ s ) return $ n ; } return -1 ; } $ s = 15 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo " - 1" ; else echo $ n ; ? >


"Find if given number is sum of first n natural numbers | Function to find no . of elements to be added to get s ; Apply Binary search ; Find mid ; find sum of 1 to mid natural numbers using formula ; If sum is equal to n return mid ; If greater than n do r = mid - 1 ; else do l = mid + 1 ; If not possible , return - 1 ; Drivers code"
How can the above be solved in PHP?

< ? php function findS ( $ s ) { $ l = 1 ; $ r = 1 + ( int ) $ s / 2 ; while ( $ l <= $ r ) { $ mid = ( int ) ( ( $ l + $ r ) / 2 ) ; $ sum = ( int ) ( $ mid * ( $ mid + 1 ) / 2 ) ; if ( $ sum == $ s ) return $ mid ; else if ( $ sum > $ s ) $ r = $ mid - 1 ; else $ l = $ mid + 1 ; } return -1 ; } $ s = 15 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo " - 1" ; else echo $ n ; ? >


"Find if it 's possible to rotate the page by an angle or not. | function to find if it 's possible to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Points a , b , and c"
How can the above be solved in PHP?


"Find if it is possible to get a ratio from given ranges of costs and quantities | Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver Code"
How can the above be solved in PHP?

 < ? php function isRatioPossible ( $ lowCost , $ upCost , $ lowQuant , $ upQuant , $ r ) { for ( $ i = $ lowQuant ; $ i <= $ upQuant ; $ i ++ ) { $ ans = $ i * $ r ; if ( $ lowCost <= $ ans && $ ans <= $ upCost ) return true ; } return false ; } $ lowCost = 14 ; $ upCost = 30 ; $ lowQuant = 5 ; $ upQuant = 12 ; $ r = 9 ; if ( isRatioPossible ( $ lowCost , $ upCost , $ lowQuant , $ upQuant , $ r ) ) echo " Yes " ; else echo " No " ; # This  code is contributed by ajit
? >


"Find if n can be written as product of k numbers | Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in vector ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver Code"
How can the above be solved in PHP?


"Find if the given number is present in the infinite sequence or not | Function that returns true if the sequence will contain B ; Driver code"
How can the above be solved in PHP?

< ? php function doesContainB ( $ a , $ b , $ c ) { if ( $ a == $ b ) return true ; if ( ( $ b - $ a ) * $ c > 0 && ( $ b - $ a ) % $ c == 0 ) return true ; return false ; } $ a = 1 ; $ b = 7 ; $ c = 3 ; if ( doesContainB ( $ a , $ b , $ c ) ) echo " Yes " ; else echo " No " ; ? >


"Find if two people ever meet after same number of jumps | function to find if any one of them can overtake the other ; Since starting points are always different , they will meet if following conditions are met . ( 1 ) Speeds are not same ( 2 ) Difference between speeds divide the total distance between initial points . ; Driver Code"
How can the above be solved in PHP?


"Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | Function that returns true if s is palindrome ; Function to return the required index ; Copy the ith character in S ; Copy all the character of string s2 in Temp ; Check whether the string is palindrome ; Driver code"
How can the above be solved in PHP?

< ? php function isPalindrome ( $ s ) { $ i = 0 ; $ j = strlen ( $ s ) - 1 ; while ( $ i < $ j ) { if ( $ s [ $ i ] != $ s [ $ j ] ) return false ; $ i ++ ; $ j -- ; } return true ; } function getIndex ( $ S1 , $ S2 , $ n ) { $ S = " " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ S = $ S . $ S1 [ $ i ] ; $ Temp = " " ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ Temp . = $ S2 [ $ j ] ; if ( isPalindrome ( $ S . $ Temp ) ) { return $ i ; } } return -1 ; } $ S1 = " abcdf " ; $ S2 = " sfgba " ; $ n = strlen ( $ S1 ) ; echo getIndex ( $ S1 , $ S2 , $ n ) ; ? >


"Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays"
How can the above be solved in PHP?

< ? php function findExtra ( $ arr1 , $ arr2 , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr1 [ $ i ] != $ arr2 [ $ i ] ) return $ i ; return $ n ; } $ arr1 = array ( 2 , 4 , 6 , 8 , 10 , 12 , 13 ) ; $ arr2 = array ( 2 , 4 , 6 , 8 , 10 , 12 ) ; $ n = sizeof ( $ arr2 ) ; echo findExtra ( $ arr1 , $ arr2 , $ n ) ; ? >


"Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left , our search is complete . ; Driver code ; Solve is passed both arrays"
How can the above be solved in PHP?

< ? php function findExtra ( $ arr1 , $ arr2 , $ n ) { $ index = $ n ; $ left = 0 ; $ right = $ n - 1 ; while ( $ left <= $ right ) { $ mid = ( $ left + $ right ) / 2 ; if ( $ arr2 [ $ mid ] == $ arr1 [ $ mid ] ) $ left = $ mid + 1 ; else { $ index = $ mid ; $ right = $ mid - 1 ; } } return $ index ; } { $ arr1 = array ( 2 , 4 , 6 , 8 , 10 , 12 , 13 ) ; $ arr2 = array ( 2 , 4 , 6 , 8 , 10 , 12 ) ; $ n = sizeof ( $ arr2 ) / sizeof ( $ arr2 [ 0 ] ) ; echo findExtra ( $ arr1 , $ arr2 , $ n ) ; return 0 ; } ? >


"Find index of first occurrence when an unsorted array is sorted | PHP program to find index of first occurrence of x when array is sorted . ; Driver Code"
How can the above be solved in PHP?

< ? php function findFirst ( $ arr , $ n , $ x ) { $ count = 0 ; $ isX = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ x ) $ isX = true ; else if ( $ arr [ $ i ] < $ x ) $ count ++ ; } return ( $ isX == false ) ? -1 : $ count ; } $ x = 20 ; $ arr = array ( 10 , 30 , 20 , 50 , 20 ) ; $ n = sizeof ( $ arr ) ; echo findFirst ( $ arr , $ n , $ x ) ; ? >


"Find index of first occurrence when an unsorted array is sorted | PHP program to find index of first occurrence of x when array is sorted . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ; Code driven"
How can the above be solved in PHP?

 < ? php function findFirst ( $ arr , $ n , $ x ) { sort ( $ arr ) ; $ ptr = floor ( $ arr ) ; return ( $ ptr != $ x ) ? 1 : ( $ ptr - $ arr ) ; } $ x = 20 ; $ arr = array ( 10 , 30 , 20 , 50 , 20 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo findFirst ( $ arr , $ n , $ x ) ; #This  code is contributed by Tushil.
? >


"Find indices of all occurrence of one string in other | PHP program to find indices of all occurrences of one string in other . ; Driver Code"
How can the above be solved in PHP?

< ? php function printIndex ( $ str , $ s ) { $ flag = false ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( substr ( $ str , $ i , strlen ( $ s ) ) == $ s ) { echo $ i . " " ; $ flag = true ; } } if ( $ flag == false ) echo " NONE " ; } $ str1 = " GeeksforGeeks " ; $ str2 = " Geeks " ; printIndex ( $ str1 , $ str2 ) ; ? >


"Find integers that divides maximum number of elements of the array | Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code"
How can the above be solved in PHP?

< ? php function maximumFactor ( $ arr ) { $ rank = array ( ) ; $ factors = array ( ) ; for ( $ i = 2 ; $ i <= max ( $ arr ) ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < sizeof ( $ arr ) ; $ j ++ ) if ( $ arr [ $ j ] % $ i == 0 ) $ count += 1 ; array_push ( $ rank , $ count ) ; array_push ( $ factors , $ i ) ; } $ m = max ( $ rank ) ; for ( $ i = 0 ; $ i < sizeof ( $ rank ) ; $ i ++ ) { if ( $ rank [ $ i ] == $ m ) echo $ factors [ $ i ] , " ▁ " ; } } $ arr = array ( 120 , 15 , 24 , 63 , 18 ) ; maximumFactor ( $ arr ) ? >


"Find iâ €™ th index character in a binary string obtained after n iterations | Set 2 | Function to find the i - th character ; distance between two consecutive elements after N iterations ; binary representation of M ; kth digit will be derived from root for sure ; Check whether there is need to flip root or not ; Driver Code"
How can the above be solved in PHP?

 < ? php function KthCharacter ( $ m , $ n , $ k ) { $ distance = pow ( 2 , $ n ) ; $ Block_number = intval ( $ k / $ distance ) ; $ remaining = $ k % $ distance ; $ s = array ( 32 ) ; $ x = 0 ; for ( ; $ m > 0 ; $ x ++ ) { $ s [ $ x ] = $ m % 2 ; $ m = intval ( $ m / 2 ) ; } $ root = $ s [ $ x - 1 - $ Block_number ] ; if ( $ remaining == 0 ) { echo $ root . "
 " ; return ; } $ flip = true ; while ( $ remaining > 1 ) { if ( $ remaining & 1 ) { $ flip = ! $ flip ; } $ remaining = $ remaining >> 1 ; } if ( $ flip ) { echo ! $ root . "
 " ; } else { echo $ root . "
 " ; } } $ m = 5 ; $ k = 5 ; $ n = 3 ; KthCharacter ( $ m , $ n , $ k ) ; ? >


"Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code"
How can the above be solved in PHP?

< ? php function kthSmallest ( $ n , $ k ) { return ( 2 * $ k ) ; } $ n = 8 ; $ k = 4 ; echo ( kthSmallest ( $ n , $ k ) ) ; ? >


"Find larger of x ^ y and y ^ x | PHP program to print greater of x ^ y and y ^ x ; Driver Code"
How can the above be solved in PHP?

< ? php function printGreater ( $ x , $ y ) { $ X = $ y * log ( $ x ) ; $ Y = $ x * log ( $ y ) ; if ( abs ( $ X - $ Y ) < 1e-9 ) { echo " Equal " ; } else if ( $ X > $ Y ) { echo $ x . " ^ " . $ y ; } else { echo $ y . " ^ " . $ x ; } } $ x = 5 ; $ y = 8 ; printGreater ( $ x , $ y ) ; ? >