"Add two numbers without using arithmetic operators | PHP Program to add two numbers without using arithmetic operator ; Iterate till there is no carry ; carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Driver Code"
How can the above be solved in PHP?

< ? php function Add ( $ x , $ y ) { while ( $ y != 0 ) { $ carry = $ x & $ y ; $ x = $ x ^ $ y ; $ y = $ carry << 1 ; } return $ x ; } echo Add ( 15 , 32 ) ; ? >


"Apothem of a n | Function to find the apothem of a regular polygon ; Side and side length cannot be negative ; Degree converted to radians ; Driver code"
How can the above be solved in PHP?


"Arc length from given Angle | function to calculate arc length ; Driver Code"
How can the above be solved in PHP?

< ? php function arcLength ( $ diameter , $ angle ) { $ pi = 22.0 / 7.0 ; $ arc ; if ( $ angle >= 360 ) { echo " Angle ▁ cannot " , " ▁ be ▁ formed " ; return 0 ; } else { $ arc = ( $ pi * $ diameter ) * ( $ angle / 360.0 ) ; return $ arc ; } } $ diameter = 25.0 ; $ angle = 45.0 ; $ arc_len = arcLength ( $ diameter , $ angle ) ; echo ( $ arc_len ) ; ? >


"Area of a Circular Sector | PHP program to find Area of a Sector ; Calculating area of the sector ; Driver code"
How can the above be solved in PHP?

< ? php function SectorArea ( $ radius , $ angle ) { if ( $ angle >= 360 ) echo ( " Angle ▁ not ▁ possible " ) ; else { $ sector = ( ( 22 * $ radius * $ radius ) / 7 ) * ( $ angle / 360 ) ; echo ( $ sector ) ; } } $ radius = 9 ; $ angle = 60 ; SectorArea ( $ radius , $ angle ) ; ? >


"Area of a Hexagon | function for calculating area of the hexagon . ; Length of a side"
How can the above be solved in PHP?

< ? php function hexagonArea ( $ s ) { return ( ( 3 * sqrt ( 3 ) * ( $ s * $ s ) ) / 2 ) ; } $ s = 4 ; echo ( " Area ▁ : ▁ " ) ; echo ( hexagonArea ( $ s ) ) ; ? >


"Area of a n | Function to find the area of a regular polygon ; Side and radius cannot be negative ; Area degree converted to radians ; Driver code"
How can the above be solved in PHP?


"Area of a n | Function to find the area of a regular polygon ; Side and side length cannot be negative ; Area degree converted to radians ; Driver code"
How can the above be solved in PHP?


"Area of a square inscribed in a circle which is inscribed in a hexagon | Function to find the area of the square ; side of hexagon cannot be negative ; area of the square ; Driver code"
How can the above be solved in PHP?

 < ? php function area ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = pow ( ( $ a * sqrt ( 3 ) ) / ( sqrt ( 2 ) ) , 2 ) ; return $ area ; } $ a = 5 ; echo area ( $ a ) . "
 " ; ? >


"Area of a square inscribed in a circle which is inscribed in an equilateral triangle | Function to find the area of the square ; a cannot be negative ; area of the square ; Driver code"
How can the above be solved in PHP?

< ? php function area ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = sqrt ( $ a ) / 6 ; return $ area ; } $ a = 10 ; echo area ( $ a ) ; ? >


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

 < ? php function rhombusarea ( $ l , $ b ) { if ( $ l < 0 $ b < 0 ) return -1 ; return ( $ l * $ b ) / 2 ; } $ l = 16 ; $ b = 6 ; echo rhombusarea ( $ l , $ b ) . "
 " ;


"Arrange given numbers to form the smallest number | Utility function to print the contents of an array ; A comparison function that return true if ' AB ' is smaller than ' BA ' when we concatenate two numbers ' A ' and ' B ' For example , it will return true if we pass 12 and 24 as arguments . This function will be used by sort ( ) function ; Convert first number to string format ; Convert second number to string format ; Check if ' AB ' is smaller or ' BA ' and return bool value since comparison operator ' < = ' returns true or false ;  ; Function to print the arrangement with the smallest value ; If we pass the name of the comparison function it will sort the array according to the compare function ; Print the sorted array ; Driver code"
How can the above be solved in PHP?

< ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] ; } function compare ( $ num1 , $ num2 ) { $ A = ( string ) $ num1 ; $ B = ( string ) $ num2 ; if ( ( int ) ( $ A . $ B ) <= ( int ) ( $ B . $ A ) ) { return true ; } else return false ; } function sort_arr ( $ arr ) { for ( $ i = 0 ; $ i < count ( $ arr ) ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < count ( $ arr ) ; $ j ++ ) { if ( compare ( $ arr [ $ i ] , $ arr [ $ j ] ) == false ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; } } } return $ arr ; } function printSmallest ( $ N , $ arr ) { $ arr = sort_arr ( $ arr ) ; printArr ( $ arr , $ N ) ; } $ arr = array ( 5 , 6 , 2 , 9 , 21 , 1 ) ; $ N = count ( $ arr ) ; printSmallest ( $ N , $ arr ) ; ? >


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

< ? php function isAutomorphic ( $ N ) { $ sq = $ N * $ N ; while ( $ N > 0 ) { if ( $ N % 10 != $ sq % 10 ) return -1 ; $ N /= 10 ; $ sq /= 10 ; } return 1 ; } $ N = 5 ; $ geeks = isAutomorphic ( $ N ) ? " Automorphic " : " Not ▁ Automorphic " ; echo $ geeks ; ? >


"Average of even numbers till a given even number | Function to calculate the average of even numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function averageEven ( $ n ) { if ( $ n % 2 != 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } return ( $ n + 2 ) / 2 ; } $ n = 16 ; echo ( averageEven ( $ n ) ) ; return 0 ; ? >


"Average of even numbers till a given even number | Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function averageEven ( $ n ) { if ( $ n % 2 != 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } $ sum = 0 ; $ count = 0 ; while ( $ n >= 2 ) { $ count ++ ; $ sum += $ n ; $ n = $ n - 2 ; } return $ sum / $ count ; } $ n = 16 ; echo ( averageEven ( $ n ) ) ; ? >


"Average of first n odd naturals numbers | Return the average of sum of first n odd numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function avg_of_odd_num ( $ n ) { return $ n ; } $ n = 8 ; echo ( avg_of_odd_num ( $ n ) ) ; ? >


"Average of first n odd naturals numbers | Returns the Avg of first n odd numbers ; sum of first n odd number ; Average of first n odd numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function avg_of_odd_num ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( 2 * $ i + 1 ) ; return $ sum / $ n ; } $ n = 20 ; echo ( avg_of_odd_num ( $ n ) ) ; ? >


"Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function averageOdd ( $ n ) { if ( $ n % 2 == 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } return ( $ n + 1 ) / 2 ; } $ n = 15 ; echo ( averageOdd ( $ n ) ) ; ? >


"Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; count odd numbers ; store the sum of odd numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function averageOdd ( $ n ) { if ( $ n % 2 == 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } $ sum = 0 ; $ count = 0 ; while ( $ n >= 1 ) { $ count ++ ; $ sum += $ n ; $ n = $ n - 2 ; } return $ sum / $ count ; } $ n = 15 ; echo ( averageOdd ( $ n ) ) ; ? >


"Biggest Square that can be inscribed within an Equilateral triangle | Function to find the side of the square ; the side cannot be negative ; side of the square ; Driver code"
How can the above be solved in PHP?

< ? php function square ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = 0.464 * $ a ; return $ x ; } $ a = 5 ; echo square ( $ a ) ; ? >


"Bubble Sort | An optimized version of Bubble Sort ; traverse the array from 0 to n - i - 1. Swap if the element found is greater than the next element ; IF no two elements were swapped by inner loop , then break ; Driver code to test above"
How can the above be solved in PHP?

 < ? php function bubbleSort ( & $ arr ) { $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ swapped = False ; for ( $ j = 0 ; $ j < $ n - $ i - 1 ; $ j ++ ) { if ( $ arr [ $ j ] > $ arr [ $ j + 1 ] ) { $ t = $ arr [ $ j ] ; $ arr [ $ j ] = $ arr [ $ j + 1 ] ; $ arr [ $ j + 1 ] = $ t ; $ swapped = True ; } } if ( $ swapped == False ) break ; } } $ arr = array ( 64 , 34 , 25 , 12 , 22 , 11 , 90 ) ; $ len = sizeof ( $ arr ) ; bubbleSort ( $ arr ) ; echo " Sorted ▁ array ▁ : ▁
 " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Calculate 7 n / 8 without using division and multiplication operators | PHP program to evaluate 7 n / 8 without using * and / ; Step 1 ) First multiply number by 7 i . e . 7 n = ( n << 3 ) - n Step 2 ) Divide result by 8 ; Driver Code"
How can the above be solved in PHP?

< ? php function multiplyBySevenByEight ( $ n ) { return ( ( $ n << 3 ) - $ n ) >> 3 ; } $ n = 15 ; echo multiplyBySevenByEight ( $ n ) ; ? >


"Calculate 7 n / 8 without using division and multiplication operators | PHP program to evaluate ceil ( 7 n / 8 ) without using * and ; Note the inner bracket here . This is needed because precedence of ' - ' operator is higher than ' < < ' ; Driver Code"
How can the above be solved in PHP?

< ? php function multiplyBySevenByEight ( $ n ) { return ( $ n - ( $ n >> 3 ) ) ; } $ n = 9 ; echo multiplyBySevenByEight ( $ n ) ; ? >


"Check divisibility of binary string by 2 ^ k | function to check whether given binary number is evenly divisible by 2 ^ k or not ; count of number of 0 from last ; if count = k , number is evenly divisible , so returns true else false ; first example ; Second example"
How can the above be solved in PHP?


"Check for Majority Element in a sorted array | PHP Program to check for majority element in a sorted array function returns majority element in a sorted array ; get last index according to n ( even or odd ) ; search for first occurrence of x in arr [ ] ; check if x is present and is present more than n / 2 times ; Driver Code"
How can the above be solved in PHP?

< ? php function isMajority ( $ arr , $ n , $ x ) { $ i ; $ last_index = $ n % 2 ? ( $ n / 2 + 1 ) : ( $ n / 2 ) ; for ( $ i = 0 ; $ i < $ last_index ; $ i ++ ) { if ( $ arr [ $ i ] == $ x && $ arr [ $ i + $ n / 2 ] == $ x ) return 1 ; } return 0 ; } $ arr = array ( 1 , 2 , 3 , 4 , 4 , 4 , 4 ) ; $ n = sizeof ( $ arr ) ; $ x = 4 ; if ( isMajority ( $ arr , $ n , $ x ) ) echo $ x , " ▁ appears ▁ more ▁ than ▁ " , floor ( $ n / 2 ) , " ▁ times ▁ in ▁ arr [ ] " ; else echo $ x , " does ▁ not ▁ appear ▁ more ▁ than ▁ " , floor ( $ n / 2 ) , " times ▁ in ▁ arr [ ] " ; ? >


"Check for an array element that is co | PHP implementation of the approach ; Stores smallest prime factor for every number ; Hash to store prime factors count ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to store the prime factors after dividing by the smallest prime factor at every step ; Storing the count of prime factors in hash ; Function that returns true if there are no common prime factors between x and other numbers of the array ; Checking whether it common prime factor with other numbers ; Function that returns true if there is an element in the array which is coprime with all the other elements of the array ; Using sieve for generating prime factors ; Checking the common prime factors with other numbers ; Driver code"
How can the above be solved in PHP?

< ? php $ MAXN = 10001 ; $ spf = array_fill ( 0 , $ MAXN , 0 ) ; $ hash1 = array_fill ( 0 , $ MAXN , 0 ) ; function sieve ( ) { global $ spf , $ MAXN , $ hash1 ; $ spf [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAXN ; $ i ++ ) $ spf [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAXN ; $ i += 2 ) $ spf [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAXN ; $ i ++ ) { if ( $ spf [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAXN ; $ j += $ i ) if ( $ spf [ $ j ] == $ j ) $ spf [ $ j ] = $ i ; } } } function getFactorization ( $ x ) { global $ spf , $ MAXN , $ hash1 ; while ( $ x != 1 ) { $ temp = $ spf [ $ x ] ; if ( $ x % $ temp == 0 ) { $ hash1 [ $ spf [ $ x ] ] ++ ; $ x = ( int ) ( $ x / $ spf [ $ x ] ) ; } while ( $ x % $ temp == 0 ) $ x = ( int ) ( $ x / $ temp ) ; } } function check ( $ x ) { global $ spf , $ MAXN , $ hash1 ; while ( $ x != 1 ) { $ temp = $ spf [ $ x ] ; if ( $ x % $ temp == 0 && $ hash1 [ $ temp ] > 1 ) return false ; while ( $ x % $ temp == 0 ) $ x = ( int ) ( $ x / $ temp ) ; } return true ; } function hasValidNum ( $ arr , $ n ) { global $ spf , $ MAXN , $ hash1 ; sieve ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) getFactorization ( $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( check ( $ arr [ $ i ] ) ) return true ; return false ; } $ arr = array ( 2 , 8 , 4 , 10 , 6 , 7 ) ; $ n = count ( $ arr ) ; if ( hasValidNum ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Check if Decimal representation of an Octal number is divisible by 7 | Function to check Divisibility ; Sum of all individual digits ; Condition ; Octal number"
How can the above be solved in PHP?

 < ? php function check ( $ n ) { $ sum = 0 ; while ( $ n != 0 ) { $ sum += $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; } if ( $ sum % 7 == 0 ) return 1 ; else return 0 ; } $ n = 25 ; ( check ( $ n ) == 1 ) ? print ( " YES
 " ) : print ( " NO
 " ) ; ? >


"Check if N is Strong Prime | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a strong prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a strong prime ; Driver code"
How can the above be solved in PHP?

< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function isStrongPrime ( $ n ) { if ( ! isPrime ( $ n ) $ n == 2 ) return false ; $ previous_prime = $ n - 1 ; $ next_prime = $ n + 1 ; while ( ! isPrime ( $ next_prime ) ) $ next_prime ++ ; while ( ! isPrime ( $ previous_prime ) ) $ previous_prime -- ; $ mean = ( $ previous_prime + $ next_prime ) / 2 ; if ( $ n > $ mean ) return true ; else return false ; } $ n = 11 ; if ( isStrongPrime ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if N is a Factorial Prime | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a factorial prime ; If n is not prime then return false ; Calculate factorial ; If n is a factorial prime ; n is not a factorial prime ; Driver code"
How can the above be solved in PHP?

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


"Check if a line touches or intersects a circle | PHP program to check if a line touches or intersects or outside a circle . ; Finding the distance of line from center . ; Checking if the distance is less than , greater than or equal to radius . ; Driver Code"
How can the above be solved in PHP?

< ? php function checkCollision ( $ a , $ b , $ c , $ x , $ y , $ radius ) { $ dist = ( abs ( $ a * $ x + $ b * $ y + $ c ) ) / sqrt ( $ a * $ a + $ b * $ b ) ; if ( $ radius == $ dist ) echo " Touch " ; else if ( $ radius > $ dist ) echo " Intersect " ; else echo " Outside " ; } $ radius = 5 ; $ x = 0 ; $ y = 0 ; $ a = 3 ; $ b = 4 ; $ c = 25 ; checkCollision ( $ a , $ b , $ c , $ x , $ y , $ radius ) ; ? >


"Check if a number is an Unusual Number or not | Utility function to find largest prime factor of a number ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; $n >>= 1 ; equivalent to n /= 2 ; n must be odd at this point , thus skip the even numbers and iterate only for odd integers ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check Unusual number ; Get the largest Prime Factor of the number ; Check if largest prime factor is greater than sqrt ( n ) ; Driver Code"
How can the above be solved in PHP?

 < ? php function largestPrimeFactor ( $ n ) { $ max = -1 ; while ( $ n % 2 == 0 ) { $ max = 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ max = $ i ; $ n = $ n / $ i ; } } if ( $ n > 2 ) $ max = $ n ; return $ max ; } function checkUnusual ( $ n ) { $ factor = largestPrimeFactor ( $ n ) ; if ( $ factor > sqrt ( $ n ) ) { return true ; } else { return false ; } } $ n = 14 ; if ( checkUnusual ( $ n ) ) { echo " YES " . "
 " ; } else { echo " NO " . "
 " ; } ? >


"Check if a number is divisible by 17 using bitwise operators | function to check recursively if the number is divisible by 17 or not ; if n = 0 or n = 17 then yes ; if n is less then 17 , not divisible by 17 ; reducing the number by floor ( n / 16 ) - n % 16 ; Driver Code"
How can the above be solved in PHP?

< ? php function isDivisibleby17 ( $ n ) { if ( $ n == 0 $ n == 17 ) return true ; if ( $ n < 17 ) return false ; return isDivisibleby17 ( ( int ) ( $ n >> 4 ) - ( int ) ( $ n & 15 ) ) ; } $ n = 35 ; if ( isDivisibleby17 ( $ n ) ) echo $ n . " ▁ is ▁ divisible ▁ by ▁ 17" ; else echo $ n . " ▁ is ▁ not ▁ divisible ▁ by ▁ 17" ; ? >


"Check if a number is divisible by 8 using bitwise operators | function to check number is div by 8 or not using bitwise operator ; Driver program"
How can the above be solved in PHP?

< ? php function Div_by_8 ( $ n ) { return ( ( ( $ n >> 3 ) << 3 ) == $ n ) ; } $ n = 16 ; if ( Div_by_8 ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Check if a point lies inside a rectangle | Set | function to find if given point lies inside a given rectangle or not . ; bottom - left and top - right corners of rectangle ; given point ; function call"
How can the above be solved in PHP?

< ? php function FindPoint ( $ x1 , $ y1 , $ x2 , $ y2 , $ x , $ y ) { if ( $ x > $ x1 and $ x < $ x2 and $ y > $ y1 and $ y < $ y2 ) return true ; return false ; } $ x1 = 0 ; $ y1 = 0 ; $ x2 = 10 ; $ y2 = 8 ; $ x = 1 ; $ y = 5 ; if ( FindPoint ( $ x1 , $ y1 , $ x2 , $ y2 , $ x , $ y ) ) echo " Yes " ; else echo " No " ; ? >


"Check if a string can be repeated to make another string | Function to return the count of repetitions of string a to generate string b ; If b cannot be generated by repeating a ; Repeat a count number of times Repeat a count number of times ; Driver code"
How can the above be solved in PHP?

< ? php function getCount ( $ a , $ b ) { if ( strlen ( $ b ) % strlen ( $ a ) != 0 ) return -1 ; $ count = floor ( strlen ( $ b ) / strlen ( $ a ) ) ; $ str = " " ; for ( $ i = 0 ; $ i < $ count ; $ i ++ ) { $ str = $ str . $ a ; } if ( strcmp ( $ a , $ b ) ) return $ count ; return -1 ; } $ a = ' eeks ' $ b = ' eeksgeeks ' echo getCount ( $ a , $ b ) ; ? >


"Check if a string has m consecutive 1 ' s ▁ or ▁ 0' s | Function that checks if the binary string contains m consecutive 1 ' s ▁ or ▁ 0' s ; length of binary string ; counts zeros ; counts 1 's ; count consecutive 0 's ; count consecutive 1 's ; Driver Code ; function call"
How can the above be solved in PHP?


"Check if an array is Wave Array | Function to check if array is wave array arr : input array n : size of array ; Check the wave form * If arr [ 1 ] is greater than left and right * Same pattern will be followed by whole * elements , else reverse pattern * will be followed by array elements ; Check for last element ; Check for last element ; Array"
How can the above be solved in PHP?

< ? php function isWaveArray ( $ arr , $ n ) { $ result = true ; if ( $ arr [ 1 ] > $ arr [ 0 ] && $ arr [ 1 ] > $ arr [ 2 ] ) { for ( $ i = 1 ; $ i < ( $ n - 1 ) ; $ i += 2 ) { if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] && $ arr [ $ i ] > $ arr [ $ i + 1 ] ) { $ result = true ; } else { $ result = false ; break ; } } if ( $ result == true && $ n % 2 == 0 ) { if ( $ arr [ $ n - 1 ] <= $ arr [ $ n - 2 ] ) { $ result = false ; } } } else if ( $ arr [ 1 ] < $ arr [ 0 ] && $ arr [ 1 ] < $ arr [ 2 ] ) { for ( $ i = 1 ; $ i < $ n - 1 ; $ i += 2 ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] && $ arr [ $ i ] < $ arr [ $ i + 1 ] ) { $ result = true ; } else { $ result = false ; break ; } } if ( $ result == true && $ n % 2 == 0 ) { if ( $ arr [ $ n - 1 ] >= $ arr [ $ n - 2 ] ) { $ result = false ; } } } return $ result ; } $ arr = array ( 1 , 3 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; if ( isWaveArray ( $ arr , $ n ) ) { echo " YES " ; } else { echo " NO " ; } ? >


"Check if array contains contiguous integers with duplicates allowed | function to check whether the array contains a set of contiguous integers ; Sort the array ; After sorting , check if current element is either same as previous or is one more . ; Driver Code"
How can the above be solved in PHP?

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


"Check if at least half array is reducible to zero by performing some operations | Function to print the desired result after computation ; Driver Code"
How can the above be solved in PHP?

 < ? php function isHalfReducible ( $ arr , $ n , $ m ) { $ frequencyHash = array_fill ( 0 , $ m + 1 , 0 ) ; $ i = 0 ; for ( ; $ i < $ n ; $ i ++ ) { $ frequencyHash [ ( $ arr [ $ i ] % ( $ m + 1 ) ) ] ++ ; } for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { if ( $ frequencyHash [ $ i ] >= ( $ n / 2 ) ) break ; } if ( $ i <= $ m ) echo " Yes
 " ; else echo " No
 " ; } $ arr = array ( 8 , 16 , 32 , 3 , 12 ) ; $ n = sizeof ( $ arr ) ; $ m = 7 ; isHalfReducible ( $ arr , $ n , $ m ) ; ? >


"Check if given number is Emirp Number or not | Returns true if n is prime else false ; Corner case ; Check from 2 to n - 1 ; Function will check whether number is Emirp or not ; Check if n is prime ; Find reverse of n ; If both Original and Reverse are Prime , then it is an Emirp number ; Input number"
How can the above be solved in PHP?

< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return -1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return -1 ; return 1 ; } function isEmirp ( $ n ) { if ( isPrime ( $ n ) == -1 ) return -1 ; $ rev = 0 ; while ( $ n != 0 ) { $ d = $ n % 10 ; $ rev = $ rev * 10 + $ d ; $ n /= 10 ; } return isPrime ( $ rev ) ; } $ n = 13 ; if ( isEmirp ( $ n ) == -1 ) echo " Yes " ; else echo " No " ; ? >


"Check if it is possible to rearrange a binary string with alternate 0 s and 1 s | function to check the binary string ; length of string ; count zero 's ; count one 's ; if length is even ; if length is odd ; Driver code"
How can the above be solved in PHP?

< ? php function is_possible ( $ s ) { $ l = strlen ( $ s ) ; $ one = 0 ; $ zero = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ s [ $ i ] == '0' ) $ zero ++ ; else $ one ++ ; } if ( $ l % 2 == 0 ) return ( $ one == $ zero ) ; else return ( abs ( $ one - $ zero ) == 1 ) ; } $ s = "100110" ; if ( is_possible ( $ s ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Check if right triangle possible from given area and hypotenuse | Prints three sides of a right triangle from given area and hypotenuse if triangle is possible , else prints - 1. ; Descriminant of the equation ; applying the linear equation formula to find both the roots ; Driver code"
How can the above be solved in PHP?

< ? php function findRightAngle ( $ A , $ H ) { $ D = pow ( $ H , 4 ) - 16 * $ A * $ A ; if ( $ D >= 0 ) { $ root1 = ( $ H * $ H + sqrt ( $ D ) ) / 2 ; $ root2 = ( $ H * $ H - sqrt ( $ D ) ) / 2 ; $ a = sqrt ( $ root1 ) ; $ b = sqrt ( $ root2 ) ; if ( $ b >= $ a ) echo $ a , " ▁ " , $ b , " ▁ " , $ H ; else echo $ b , " ▁ " , $ a , " ▁ " , $ H ; } else echo " - 1" ; } findRightAngle ( 6 , 5 ) ;


"Check if the Xor of the frequency of all digits of a number N is zero or not | PHP implementation of the above approach ; creating a frequency array ; Finding the last digit of the number ; Dividing the number by 10 to eliminate last digit ; counting frequency of each digit ; checking if the xor of all frequency is zero or not ; Main Drive"
How can the above be solved in PHP?

< ? php function check ( $ s ) { $ freq = array_fill ( 0 , 10 , 0 ) ; while ( $ s != 0 ) { $ r = $ s % 10 ; $ s = ( int ) ( $ s / 10 ) ; $ freq [ $ r ] += 1 ; } $ xor = 0 ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ xor = $ xor ^ $ freq [ $ i ] ; if ( $ xor == 0 ) return true ; else return false ; } $ s = 122233 ; if ( check ( $ s ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >


"Check if the first and last digit of the smallest number forms a prime | function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver Code"
How can the above be solved in PHP?

< ? php function isPrime ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ c ++ ; } if ( $ c == 1 ) return 1 ; else return 0 ; } function findMinNum ( $ arr , $ n ) { $ first = 0 ; $ last = 0 ; $ num ; $ rev ; $ i ; $ hash = array_fill ( 0 , 20 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ hash [ $ arr [ $ i ] ] ++ ; } echo " Minimum ▁ number : ▁ " ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { if ( $ hash [ $ i ] != 0 ) { $ first = $ i ; break ; } } for ( $ i = 9 ; $ i >= 0 ; $ i -- ) { if ( $ hash [ $ i ] != 0 ) { $ last = $ i ; break ; } } $ num = $ first * 10 + $ last ; $ rev = $ last * 10 + $ first ; echo " Prime combinations : " if ( isPrime ( $ num ) && isPrime ( $ rev ) ) echo $ num . " ▁ " . $ rev ; else if ( isPrime ( $ num ) ) echo $ num ; else if ( isPrime ( $ rev ) ) echo $ rev ; else echo " No ▁ combinations ▁ exist " ; } $ arr = array ( 1 , 2 , 4 , 7 , 8 ) ; findMinNum ( $ arr , 5 ) ; ? >


"Check if two arrays are permutations of each other using Mathematical Operation | Function to check if arrays are permutations of each other . ; Calculating sum and multiply of first array ; Calculating sum and multiply of second array ; If sum and mul of both arrays are equal , return true , else return false . ; Driver code"
How can the above be solved in PHP?


"Check whether Arithmetic Progression can be formed from the given array | Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; Driver Code"
How can the above be solved in PHP?

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


"Check whether a given point lies on or inside the rectangle | Set 3 | function to Check whether a given point lies inside or on the rectangle or not ; Driver code"
How can the above be solved in PHP?

< ? php function LiesInsieRectangle ( $ a , $ b , $ x , $ y ) { if ( $ x - $ y - $ b <= 0 && $ x - $ y + $ b >= 0 && $ x + $ y - 2 * $ a + $ b <= 0 && $ x + $ y - $ b >= 0 ) return true ; return false ; } $ a = 7 ; $ b = 2 ; $ x = 4 ; $ y = 5 ; if ( LiesInsieRectangle ( $ a , $ b , $ x , $ y ) ) echo " Given ▁ point ▁ lies ▁ " . " inside ▁ the ▁ rectangle " ; else echo " Given ▁ point ▁ does ▁ not " . " ▁ lie ▁ on ▁ the ▁ rectangle " ; ? >


"Check whether the given number is Wagstaff prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Utility function to check power of two ; Driver Code ; Check if number is prime and of the form ( 2 ^ q + 1 ) / 3"
How can the above be solved in PHP?

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


"Closest sum partition ( into two subsets ) of numbers from 1 to n | Function to return the minimum required absolute difference ; Driver code"
How can the above be solved in PHP?

< ? php function minAbsDiff ( $ n ) { $ mod = $ n % 4 ; if ( $ mod == 0 $ mod == 3 ) return 0 ; return 1 ; } $ n = 5 ; echo minAbsDiff ( $ n ) ; ? >


"Coin Change | DP | Dynamic Programming PHP implementation of Coin Change problem ; table [ i ] will be storing the number of solutions for value i . We need n + 1 rows as the table is constructed in bottom up manner using the base case ( n = 0 ) ; Base case ( If given value is 0 ) ; Pick all coins one by one and update the table [ ] values after the index greater than or equal to the value of the picked coin ; Driver Code"
How can the above be solved in PHP?

< ? php function count_1 ( & $ S , $ m , $ n ) { $ table = array_fill ( 0 , $ n + 1 , NULl ) ; $ table [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = $ S [ $ i ] ; $ j <= $ n ; $ j ++ ) $ table [ $ j ] += $ table [ $ j - $ S [ $ i ] ] ; return $ table [ $ n ] ; } $ arr = array ( 1 , 2 , 3 ) ; $ m = sizeof ( $ arr ) ; $ n = 4 ; $ x = count_1 ( $ arr , $ m , $ n ) ; echo $ x ; ? >


"Coin Change | DP | Returns the count of ways we can sum S [ 0. . . m - 1 ] coins to get sum n ; If n is 0 then there is 1 solution ( do not include any coin ) ; If n is less than 0 then no solution exists ; If there are no coins and n is greater than 0 , then no solution exist ; count is sum of solutions ( i ) including S [ m - 1 ] ( ii ) excluding S [ m - 1 ] ; Driver Code"
How can the above be solved in PHP?

< ? php function coun ( $ S , $ m , $ n ) { if ( $ n == 0 ) return 1 ; if ( $ n < 0 ) return 0 ; if ( $ m <= 0 && $ n >= 1 ) return 0 ; return coun ( $ S , $ m - 1 , $ n ) + coun ( $ S , $ m , $ n - $ S [ $ m - 1 ] ) ; } $ arr = array ( 1 , 2 , 3 ) ; $ m = count ( $ arr ) ; echo coun ( $ arr , $ m , 4 ) ; ? >


"Composite Number | A optimized school method based PHP program to check if a number is composite . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code"
How can the above be solved in PHP?

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


"Composite XOR and Coprime AND | Function to return the count of odd numbers in the array ; Variable to count odd numbers ; Odd number ; Function to return the count of valid pairs ; Driver Code"
How can the above be solved in PHP?

< ? php function countOdd ( $ arr , $ n ) { $ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % 2 == 1 ) $ odd ++ ; } return $ odd ; } function countValidPairs ( $ arr , $ n ) { $ odd = countOdd ( $ arr , $ n ) ; return ( $ odd * ( $ odd - 1 ) ) / 2 ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo countValidPairs ( $ arr , $ n ) ; ? >


"Compute modulus division by a power | This function will return n % d . d must be one of : 1 , 2 , 4 , 8 , 16 , 32 , ... ; Driver Code ; d must be a power of 2"
How can the above be solved in PHP?

< ? php function getModulo ( $ n , $ d ) { return ( $ n & ( $ d - 1 ) ) ; } $ n = 6 ; $ d = 4 ; echo $ n , " ▁ moduo " , " ▁ " , $ d , " ▁ is ▁ " , " ▁ " , getModulo ( $ n , $ d ) ; ? >


"Convert a number m to n using minimum number of given operations | Function to find minimum number of given operations to convert m to n ; only way is to do - 1 ( m - n ) times ; not possible ; n is greater and n is odd ; perform ' - 1' on m ( or + 1 on n ) ; n is even ; perform ' * 2' on m ( or n / 2 on n ) ; Driver code"
How can the above be solved in PHP?

< ? php function convert ( $ m , $ n ) { if ( $ m == $ n ) return 0 ; if ( $ m > $ n ) return $ m - $ n ; if ( $ m <= 0 && $ n > 0 ) return -1 ; if ( $ n % 2 == 1 ) return 1 + convert ( $ m , $ n + 1 ) ; else return 1 + convert ( $ m , $ n / 2 ) ; } { $ m = 3 ; $ n = 11 ; echo " Minimum ▁ number ▁ of ▁ " . " operations ▁ : ▁ " , convert ( $ m , $ n ) ; return 0 ; } ? >


"Count Negative Numbers in a Column | Function to count negative number ; initialize result ; Start with top right corner ; Follow the path shown using arrows above ; j is the index of the last negative number in this row . So there must be ( j + 1 ) ; negative numbers in this row . ; move to the left and see if we can find a negative number there ; Driver Code"
How can the above be solved in PHP?

< ? php function countNegative ( $ M , $ n , $ m ) { $ count = 0 ; $ i = 0 ; $ j = $ m - 1 ; while ( $ j >= 0 and $ i < $ n ) { if ( $ M [ $ i ] [ $ j ] < 0 ) { $ count += $ j + 1 ; $ i += 1 ; } else $ j -= 1 ; } return $ count ; } $ M = array ( array ( -3 , -2 , -1 , 1 ) , array ( -2 , 2 , 3 , 4 ) , array ( 4 , 5 , 7 , 8 ) ) ; echo countNegative ( $ M , 3 , 4 ) ; return 0 ; ? >


"Count Negative Numbers in a Column | PHP implementation of Naive method to count of negative numbers in M [ n ] [ m ] ; Follow the path shown using arrows above ; no more negative numbers in this row ; Driver Code"
How can the above be solved in PHP?

< ? php function countNegative ( $ M , $ n , $ m ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ M [ $ i ] [ $ j ] < 0 ) $ count += 1 ; else break ; } } return $ count ; } $ M = array ( array ( -3 , -2 , -1 , 1 ) , array ( -2 , 2 , 3 , 4 ) , array ( 4 , 5 , 7 , 8 ) ) ; echo countNegative ( $ M , 3 , 4 ) ; ? >


"Count divisors of array multiplication | To count number of factors in a number ; Initialize count with 0 ; Increment count for every factor of the given number X . ; Return number of factors ; Returns number of divisors in array multiplication ; Multipliying all elements of the given array . ; Calling function which count number of factors of the number ; Driver code"
How can the above be solved in PHP?

< ? php function counDivisors ( $ X ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ X ; ++ $ i ) { if ( $ X % $ i == 0 ) { $ count ++ ; } } return $ count ; } function countDivisorsMult ( $ arr , $ n ) { $ mul = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ mul *= $ arr [ $ i ] ; return counDivisors ( $ mul ) ; } $ arr = array ( 2 , 4 , 6 ) ; $ n = sizeof ( $ arr ) ; echo countDivisorsMult ( $ arr , $ n ) ; ? >


"Count n digit numbers not having a particular digit | PHP Implementation of above method ; Finding number of possible number with n digits excluding a particular digit ; Checking if number of digits is zero ; Checking if number of digits is one ; Checking if number of digits is odd ; Calling digitNumber function with ( digit - 1 ) / 2 digits ; ; Calling digitNumber function with n / 2 digits ; Calling digitNumber function Checking if excluding digit is zero or non - zero ; Initializing variables"
How can the above be solved in PHP?

< ? php $ mod = 1000000007 ; function digitNumber ( $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; if ( $ n == 1 ) return 9 ; if ( $ n % 2 != 0 ) { $ temp = digitNumber ( ( $ n - 1 ) / 2 ) % $ mod ; return ( 9 * ( $ temp * $ temp ) % $ mod ) % $ mod ; } else { $ temp = digitNumber ( $ n / 2 ) % $ mod ; return ( $ temp * $ temp ) % $ mod ; } } function countExcluding ( $ n , $ d ) { global $ mod ; if ( $ d == 0 ) return ( 9 * digitNumber ( $ n - 1 ) ) % $ mod ; else return ( 8 * digitNumber ( $ n - 1 ) ) % $ mod ; } $ d = 9 ; $ n = 3 ; print ( countExcluding ( $ n , $ d ) ) ; ? >


"Count numbers in range such that digits in it and it 's product with q are unequal | Function to check if all of the digits in a number and it 's product with q are unequal or not ; convert first number into string ; Insert elements from 1 st number to hash ; Calculate corresponding product ; Convert the product to string ; Using the hash check if any digit of product matches with the digits of input number ; If yes , return false ; Else , return true ; Function to count numbers in the range [ l , r ] such that all of the digits of the number and it 's product with q are unequal ; check for every number between l and r ; Driver Code ; Function call"
How can the above be solved in PHP?

< ? php function checkIfUnequal ( $ n , $ q ) { $ s1 = strval ( $ n ) ; $ a = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) $ a [ ord ( $ s1 [ $ i ] ) - ord ( '0' ) ] ++ ; $ prod = $ n * $ q ; $ s2 = strval ( $ prod ) ; for ( $ i = 0 ; $ i < strlen ( $ s2 ) ; $ i ++ ) { if ( $ a [ ord ( $ s2 [ $ i ] ) - ord ( '0' ) ] ) return false ; } return true ; } function countInRange ( $ l , $ r , $ q ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { if ( checkIfUnequal ( $ i , $ q ) ) $ count ++ ; } return $ count ; } $ l = 10 ; $ r = 12 ; $ q = 2 ; echo countInRange ( $ l , $ r , $ q ) ; ? >


"Count numbers with unit digit k in given range | Returns count of numbers with k as last digit . ; Driver Code"
How can the above be solved in PHP?

< ? php function counLastDigitK ( $ low , $ high , $ k ) { $ count = 0 ; for ( $ i = $ low ; $ i <= $ high ; $ i ++ ) if ( $ i % 10 == $ k ) $ count ++ ; return $ count ; } $ low = 3 ; $ high = 35 ; $ k = 3 ; echo counLastDigitK ( $ low , $ high , $ k ) ; ? >


"Count occurrences of a character in a repeated string | Function to count the character ' a ' ; atleast k repetition are required ; if n is not the multiple of the string size check for the remaining repeating character . ; Driver code"
How can the above be solved in PHP?

< ? php function countChar ( $ str , $ x ) { $ count = 0 ; $ n = 10 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( $ str [ $ i ] == $ x ) $ count ++ ; $ repetitions = ( int ) ( $ n / strlen ( $ str ) ) ; $ count = $ count * $ repetitions ; for ( $ i = 0 ; $ i < $ n % strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == $ x ) $ count ++ ; } return $ count ; } $ str = " abcac " ; echo countChar ( $ str , ' a ' ) ; ? >


"Count occurrences of a word in string | PHP program to count the number of occurrence of a word in the given string ; split the string by spaces ; search for pattern in string ; if match found increase count ; Driver code"
How can the above be solved in PHP?

< ? php function countOccurrences ( $ str , $ word ) { $ a = explode ( " ▁ " , $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ a ) ; $ i ++ ) { if ( $ word == $ a [ $ i ] ) $ count ++ ; } return $ count ; } $ str = " GeeksforGeeks ▁ A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks ▁ " ; $ word = " portal " ; echo ( countOccurrences ( $ str , $ word ) ) ; ? >


"Count of occurrences of a "1(0 + ) 1" pattern in a string | Returns count of occurrences of "1(0 + ) 1" ; $count = 0 ; Initialize result ; Check if encountered '1' forms a valid pattern as specified ; if 1 encountered for first time set oneSeen to 1 ; Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern ; Driver Code"
How can the above be solved in PHP?

< ? php function countPattern ( $ str ) { $ len = strlen ( $ str ) ; $ oneSeen = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == '1' && $ oneSeen == 1 ) if ( $ str [ $ i - 1 ] == '0' ) $ count ++ ; if ( $ str [ $ i ] == '1' && $ oneSeen == 0 ) $ oneSeen = 1 ; if ( $ str [ $ i ] != '0' && $ str [ $ i ] != '1' ) $ oneSeen = 0 ; } return $ count ; } $ str = "100001abc101" ; echo countPattern ( $ str ) ; ? >


"Count set bits in an integer | Driver code"
How can the above be solved in PHP?


"Count set bits in an integer | Function to get no of set bits in binary representation of positive integer n ; Driver Code"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count += $ n & 1 ; $ n >>= 1 ; } return $ count ; } $ i = 9 ; echo countSetBits ( $ i ) ; ? >


"Count set bits in an integer | PHP program to count set bits by pre - storing count set bits in nibbles . ; Recursively get nibble of a given number and map them in the array ; Find last nibble ; Use pre - stored values to find count in last nibble plus recursively add remaining nibbles . ; Driver code"
How can the above be solved in PHP?

< ? php $ num_to_bits = array ( 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 , 1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 ) ; function countSetBitsRec ( $ num ) { global $ num_to_bits ; $ nibble = 0 ; if ( 0 == $ num ) return $ num_to_bits [ 0 ] ; $ nibble = $ num & 0xf ; return $ num_to_bits [ $ nibble ] + countSetBitsRec ( $ num >> 4 ) ; } $ num = 31 ; echo ( countSetBitsRec ( $ num ) ) ; ? >


"Count set bits in an integer | recursive function to count set bits ; base case ; get value from user ; function calling"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { if ( $ n == 0 ) return 0 ; else return 1 + countSetBits ( $ n & ( $ n - 1 ) ) ; } $ n = 9 ; echo countSetBits ( $ n ) ; ? >


"Count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] | Function to find number of unordered pairs ; Maximum element from the array ; Array to store the frequency of each element ; Stores the number of unordered pairs ; Store the frequency of each element ; Find the number of unordered pairs ; If the number j divisible by ith element is present in the array ; If the ith element of the array has frequency more than one ; Driver code"
How can the above be solved in PHP?

< ? php function freqPairs ( $ arr , $ n ) { $ max = max ( $ arr ) ; $ freq = array_fill ( 0 , $ max + 1 , 0 ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ $ arr [ $ i ] ] ++ ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 2 * $ arr [ $ i ] ; $ j <= $ max ; $ j += $ arr [ $ i ] ) { if ( $ freq [ $ j ] >= 1 ) $ count += $ freq [ $ j ] ; } if ( $ freq [ $ arr [ $ i ] ] > 1 ) { $ count += $ freq [ $ arr [ $ i ] ] - 1 ; $ freq [ $ arr [ $ i ] ] -- ; } } return $ count ; } $ arr = array ( 3 , 2 , 4 , 2 , 6 ) ; $ n = count ( $ arr ) ; echo freqPairs ( $ arr , $ n ) ; ? >


"Count the number of primes in the prefix sum array of the given array | Function to return the count of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find all primes in arr [ ] ; Function to generate the prefix array ; Fill the prefix array ; Driver code ; Prefix array of arr [ ] ; Count of primes in the prefix array"
How can the above be solved in PHP?

< ? php function primeCount ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ prime [ $ arr [ $ i ] ] ) $ count ++ ; return $ count ; } function getPrefixArray ( $ arr , $ n , $ pre ) { $ pre [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ arr [ $ i ] ; } return $ pre ; } $ arr = array ( 1 , 4 , 8 , 4 ) ; $ n = count ( $ arr ) ; $ pre = array ( ) ; $ pre = getPrefixArray ( $ arr , $ n , $ pre ) ; echo primeCount ( $ pre , $ n ) ; ? >


"Count the triplets such that A [ i ] < B [ j ] < C [ k ] | Function to return the count of elements in arr [ ] which are less than the given key ; Modified binary search ; Function to return the count of elements in arr [ ] which are greater than the given key ; Modified binary search ; Function to return the count of the required triplets ; Sort all three arrays ; Iterate for all the elements of array B ; Count of elements in A [ ] which are less than the chosen element from B [ ] ; Count of elements in C [ ] which are greater than the chosen element from B [ ] ; Update the count ; Driver code"
How can the above be solved in PHP?

< ? php function countLessThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ; while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ m ] < $ key ) { $ l = $ m + 1 ; $ index = $ m ; } else { $ r = $ m - 1 ; } } return ( $ index + 1 ) ; } function countGreaterThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ; while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ m ] <= $ key ) { $ l = $ m + 1 ; } else { $ r = $ m - 1 ; $ index = $ m ; } } if ( $ index == -1 ) return 0 ; return ( $ n - $ index ) ; } function countTriplets ( $ n , & $ a , & $ b , & $ c ) { sort ( $ a ) ; sort ( $ b ) ; sort ( $ c ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ current = $ b [ $ i ] ; $ a_index = -1 ; $ c_index = -1 ; $ low = countLessThan ( $ a , $ n , $ current ) ; $ high = countGreaterThan ( $ c , $ n , $ current ) ; $ count += ( $ low * $ high ) ; } return $ count ; } $ a = array ( 1 , 5 ) ; $ b = array ( 2 , 4 ) ; $ c = array ( 3 , 6 ) ; $ size = sizeof ( $ a ) ; echo countTriplets ( $ size , $ a , $ b , $ c ) ; ? >


"Count trailing zero bits using lookup table | PHP code for counting trailing zeros in binary representation of a number ; Map a bit value mod 37 to its position ; Only difference between ( x and - x ) is the value of signed magnitude ( leftmostbit ) negative numbers signed bit is 1 ; Driver Code"
How can the above be solved in PHP?


"Count trailing zero bits using lookup table | Simple PHP code for counting trailing zeros in binary representation of a number ; Driver Code"
How can the above be solved in PHP?

 < ? php function countTrailingZero ( $ x ) { $ count = 0 ; while ( ( $ x & 1 ) == 0 ) { $ x = $ x >> 1 ; $ count ++ ; } return $ count ; } echo countTrailingZero ( 11 ) , "
 " ; ? >


"Count ways of choosing a pair with maximum difference | PHP Code to find no . of Ways of choosing a pair with maximum difference ; To find minimum and maximum of the array ; to find the count of minimum and maximum elements ; Count variables ; condition for all elements equal ; Driver code"
How can the above be solved in PHP?


"Count ways to express a number as sum of consecutive numbers | Utility method to compute number of ways in which N can be represented as sum of consecutive number ; constraint on values of L gives us the time Complexity as O ( N ^ 0.5 ) ; Driver Code"
How can the above be solved in PHP?

 < ? php function countConsecutive ( $ N ) { $ count = 0 ; for ( $ L = 1 ; $ L * ( $ L + 1 ) < 2 * $ N ; $ L ++ ) { $ a = ( int ) ( 1.0 * $ N - ( $ L * ( int ) ( $ L + 1 ) ) / 2 ) / ( $ L + 1 ) ; if ( $ a - ( int ) $ a == 0.0 ) $ count ++ ; } return $ count ; } $ N = 15 ; echo countConsecutive ( $ N ) , "
 " ; $ N = 10 ; echo countConsecutive ( $ N ) , "
 " ; ? >


"Count ways to reach a score using 1 and 2 with no consecutive 2 s | A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; base cases ; For cases n > 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function CountWays ( $ n ) { if ( $ n == 0 ) { return 1 ; } if ( $ n == 1 ) { return 1 ; } if ( $ n == 2 ) { return 1 + 1 ; } return CountWays ( $ n - 1 ) + CountWays ( $ n - 3 ) ; } $ n = 5 ; echo CountWays ( $ n ) ; ? >


"Counting sets of 1 s and 0 s in a binary matrix | no of columns ; no of rows ; function to calculate the number of non empty sets of cell ; stores the final answer ; traverses row - wise ; traverses column wise ; at the end subtract n * m as no of single sets have been added twice . ; Driver Code"
How can the above be solved in PHP?


"Counts paths from a point to reach Origin | DP based function to count number of paths ; $dp [ $n + 1 ] [ $m + 1 ] ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code"
How can the above be solved in PHP?


"Counts paths from a point to reach Origin | Recursive function to count number of paths ; If we reach bottom or top left , we are have only one way to reach ( 0 , 0 ) ; Else count sum of both ways ; Driver Code"
How can the above be solved in PHP?

< ? php function countPaths ( $ n , $ m ) { if ( $ n == 0 $ m == 0 ) return 1 ; return ( countPaths ( $ n - 1 , $ m ) + countPaths ( $ n , $ m - 1 ) ) ; } $ n = 3 ; $ m = 2 ; echo " ▁ Number ▁ of ▁ Paths ▁ " , countPaths ( $ n , $ m ) ; ? >


"Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver program to test above functions"
How can the above be solved in PHP?

< ? php function cutRod ( $ price , $ n ) { $ val = array ( ) ; $ val [ 0 ] = 0 ; $ i ; $ j ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ max_val = PHP_INT_MIN ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ max_val = max ( $ max_val , $ price [ $ j ] + $ val [ $ i - $ j - 1 ] ) ; $ val [ $ i ] = $ max_val ; } return $ val [ $ n ] ; } $ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo " Maximum ▁ Obtainable ▁ Value ▁ is ▁ " , cutRod ( $ arr , $ size ) ; ? >


"Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Recursively cut the rod in different pieces and compare different configurations ; Driver Code"
How can the above be solved in PHP?

< ? php function cutRod ( $ price , $ n ) { if ( $ n <= 0 ) return 0 ; $ max_val = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ max_val = max ( $ max_val , $ price [ $ i ] + cutRod ( $ price , $ n - $ i - 1 ) ) ; return $ max_val ; } $ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo " Maximum ▁ Obtainable ▁ Value ▁ is ▁ " , cutRod ( $ arr , $ size ) ; ? >


"Decrypt a string encrypted by repeating i | Function to return the decrypted string ; Initial jump will be 1 ; Increment jump by 1 with every character ; Driver code"
How can the above be solved in PHP?

< ? php function decryptString ( $ str , $ n ) { $ i = 0 ; $ jump = 1 ; $ decryptedStr = " " ; while ( $ i < $ n ) { $ decryptedStr . = $ str [ $ i ] ; $ i += $ jump ; $ jump ++ ; } return $ decryptedStr ; } $ str = " geeeeekkkksssss " ; $ n = strlen ( $ str ) ; echo decryptString ( $ str , $ n ) ; ? >


"Distance between a point and a Plane in 3 D | Function to find distance ; Driver Code ; function call"
How can the above be solved in PHP?


"Distance between two parallel Planes in 3 | Function to find distance ; Driver Code"
How can the above be solved in PHP?


"Encrypt a string by repeating i | Function to return the encrypted string ; Number of times the current character will be repeated ; Repeat the current character in the encrypted string ; Driver code"
How can the above be solved in PHP?

< ? php function encryptString ( $ str , $ n ) { $ i = 0 ; $ cnt = 0 ; $ encryptedStr = " " ; while ( $ i < $ n ) { $ cnt = $ i + 1 ; while ( $ cnt -- ) $ encryptedStr . = $ str [ $ i ] ; $ i ++ ; } return $ encryptedStr ; } $ str = " geeks " ; $ n = strlen ( $ str ) ; echo encryptString ( $ str , $ n ) ; ? >


"Encrypt string with product of number of vowels and consonants in substring of size k | isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the dtring ; for each substring ; substring of size k ; counting number of vowels and consonants ; append product to answer . ; Driver Code"
How can the above be solved in PHP?

 < ? php function isVowel ( $ c ) { return ( $ c == ' a ' $ c == ' e ' $ c == ' i ' $ c == ' o ' $ c == ' u ' ) ; } function encryptString ( $ s , $ n , $ k ) { $ countVowels = 0 ; $ countConsonants = 0 ; $ ans = " " ; for ( $ l = 0 ; $ l <= $ n - $ k ; $ l ++ ) { $ countVowels = 0 ; $ countConsonants = 0 ; for ( $ r = $ l ; $ r <= $ l + $ k - 1 ; $ r ++ ) { if ( isVowel ( $ s [ $ r ] ) == true ) $ countVowels ++ ; else $ countConsonants ++ ; } $ ans = $ ans . ( string ) ( $ countVowels * $ countConsonants ) ; } return $ ans ; } $ s = " hello " ; $ n = strlen ( $ s ) ; $ k = 2 ; echo encryptString ( $ s , $ n , $ k ) . "
 " ;


"Fibonacci Cube Graph | function to find fibonacci number ; function for finding number of vertices in fibonacci cube graph ; return fibonacci number for f ( n + 2 ) ; Driver Code"
How can the above be solved in PHP?

< ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; } function findVertices ( $ n ) { return fib ( $ n + 2 ) ; } $ n = 3 ; echo findVertices ( $ n ) ; ? >


"Find ( a ^ b ) % m where ' a ' is very large | utility function to calculate a % m ; ( s [ i ] - '0' ) gives the digit value and form the number ; Returns find ( a ^ b ) % m ; Find a % m ; now multiply ans by b - 1 times and take mod with m ; Driver code"
How can the above be solved in PHP?


"Find ( a ^ b ) % m where ' b ' is very large | Function to find power ; Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Driver Code ; String input as b is very large ; Reduce the number B to a small number using Fermat Little"
How can the above be solved in PHP?


"Find Bit whose minimum sequence flips makes all bits same | Function to check which bit is to be flipped ; variable to store first and last character of string ; Check if first and last characters are equal , if yes , then return the character which is not at last ; else return last ; Driver Code"
How can the above be solved in PHP?

< ? php function bitToBeFlipped ( $ s ) { $ last = $ s [ strlen ( $ s ) - 1 ] ; $ first = $ s [ 0 ] ; if ( $ last == $ first ) { if ( $ last == '0' ) { return '1' ; } else { return '0' ; } } else if ( $ last != $ first ) { return $ last ; } } $ s = "1101011000" ; echo bitToBeFlipped ( $ s ) ; ? >


"Find Cube Pairs | Set 2 ( A n ^ ( 1 / 3 ) Solution ) | Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube [ ] whose sum is equal to n ; Driver code"
How can the above be solved in PHP?

 < ? php function findPairs ( $ n ) { $ cubeRoot = pow ( $ n , 1.0 / 3.0 ) ; $ cube = array ( ) ; for ( $ i = 1 ; $ i <= $ cubeRoot ; $ i ++ ) $ cube [ $ i ] = $ i * $ i * $ i ; $ l = 1 ; $ r = $ cubeRoot ; while ( $ l < $ r ) { if ( $ cube [ $ l ] + $ cube [ $ r ] < $ n ) $ l ++ ; else if ( $ cube [ $ l ] + $ cube [ $ r ] > $ n ) $ r -- ; else { echo " ( " , $ l , " , ▁ " , floor ( $ r ) , " ) " ; echo "
 " ; $ l ++ ; $ r -- ; } } } $ n = 20683 ; findPairs ( $ n ) ; ? >


"Find Square Root under Modulo p | Set 1 ( When p is in form of 4 * i + 3 ) | Utility function to do modular exponentiation . It returns ( x ^ y ) % p . ; 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 / 2 ; Returns true if square root of n under modulo p exists Assumption : p is of the form 3 * i + 4 where i >= 1 ; Try " + ( n ^ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; Try " - ( n ▁ ^ ▁ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; If none of the above two work , then square root doesn 't exist ; Driver Code"
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 ; $ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function squareRoot ( $ n , $ p ) { if ( $ p % 4 != 3 ) { echo " Invalid ▁ Input " ; return ; } $ n = $ n % $ p ; $ x = power ( $ n , ( $ p + 1 ) / 4 , $ p ) ; if ( ( $ x * $ x ) % $ p == $ n ) { echo " Square ▁ root ▁ is ▁ " , $ x ; return ; } $ x = $ p - $ x ; if ( ( $ x * $ x ) % $ p == $ n ) { echo " Square ▁ root ▁ is ▁ " , $ x ; return ; } echo " Square ▁ root ▁ doesn ' t ▁ exist ▁ " ; } $ p = 7 ; $ n = 2 ; squareRoot ( $ n , $ p ) ; ? >


"Find a pair with the given difference | The function assumes that the array is sorted ; Initialize positions of two elements ; Search for a pair ; Driver Code"
How can the above be solved in PHP?

< ? php function findPair ( & $ arr , $ size , $ n ) { $ i = 0 ; $ j = 1 ; while ( $ i < $ size && $ j < $ size ) { if ( $ i != $ j && $ arr [ $ j ] - $ arr [ $ i ] == $ n ) { echo " Pair ▁ Found : ▁ " . " ( " . $ arr [ $ i ] . " , ▁ " . $ arr [ $ j ] . " ) " ; return true ; } else if ( $ arr [ $ j ] - $ arr [ $ i ] < $ n ) $ j ++ ; else $ i ++ ; } echo " No ▁ such ▁ pair " ; return false ; } $ arr = array ( 1 , 8 , 30 , 40 , 100 ) ; $ size = sizeof ( $ arr ) ; $ n = 60 ; findPair ( $ arr , $ size , $ n ) ; ? >


"Find a specific pair in Matrix | A Naive method to find maximum value of $mat [ d ] [ e ] - ma [ a ] [ b ] such that $d > $a and $e > $b ; The function returns maximum value A ( d , e ) - A ( a , b ) over all choices of indexes such that both $d > $a and $e > $b . ; stores maximum value ; Consider all possible pairs $mat [ $a ] [ $b ] and $mat [ $d ] [ $e ] ; Driver Code"
How can the above be solved in PHP?


"Find a specific pair in Matrix | An efficient method to find maximum value of mat [ d ] - ma [ a ] [ b ] such that c > a and d > b ; The function returns maximum value A ( c , d ) - A ( a , b ) over all choices of indexes such that both c > a and d > b . ; stores maximum value ; maxArr [ i ] [ j ] stores max of elements in matrix from ( i , j ) to ( N - 1 , N - 1 ) ; last element of maxArr will be same 's as of  the input matrix ; preprocess last row Initialize max ; preprocess last column Initialize max ; preprocess rest of the matrix from bottom ; Update maxValue ; set maxArr ( i , j ) ; Driver Code"
How can the above be solved in PHP?


"Find all divisors of a natural number | Set 2 | function to print the divisors ; Vector to store half of the divisors ; check if divisors are equal ; push the second divisor in the vector ; The vector will be printed in reverse ; Driver code"
How can the above be solved in PHP?

 < ? php function printDivisors ( $ n ) { $ v ; $ t = 0 ; for ( $ i = 1 ; $ i <= ( int ) sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( ( int ) $ n / $ i == $ i ) echo $ i . " " ; else { echo $ i . " " ; $ v [ $ t ++ ] = ( int ) $ n / $ i ; } } } for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) echo $ v [ $ i ] . " ▁ " ; } echo " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁
 " ; printDivisors ( 100 ) ; ? >


"Find all elements in array which have at | PHP program to find all elements in array which have atleast two greater elements itself . ; 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 findElements ( $ arr , $ n ) { $ first = PHP_INT_MIN ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ first ) { $ second = $ first ; $ first = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ second ) $ second = $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] < $ second ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 2 , -6 , 3 , 5 , 1 ) ; $ n = count ( $ arr ) ; findElements ( $ arr , $ n ) ; ? >


"Find all elements in array which have at | Simple PHP program to find all elements in array which have at - least two greater elements itself . ; Pick elements one by one and count greater elements . If count is more than 2 , print that element . ; Driver code"
How can the above be solved in PHP?

< ? php function findElements ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] > $ arr [ $ i ] ) $ count ++ ; if ( $ count >= 2 ) echo $ arr [ $ i ] . " ▁ " ; } } $ arr = array ( 2 , -6 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; findElements ( $ arr , $ n ) ; ? >


"Find all elements in array which have at | Sorting based PHP program to find all elements in array which have atleast two greater elements itself . ; Driver Code"
How can the above be solved in PHP?

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


"Find all factors of a natural number | Set 1 | Function to print the divisors ; Note that this loop runs till square root ; If divisors are equal , print only one ; Otherwise print both ; Driver Code"
How can the above be solved in PHP?

 < ? php function printDivisors ( $ n ) { for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) echo $ i , " ▁ " ; else echo $ i , " ▁ " , $ n / $ i , " ▁ " ; } } } echo " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁
 " ; printDivisors ( 100 ) ; ? >


"Find all factors of a natural number | Set 1 | function to print the divisors ; Driver Code"
How can the above be solved in PHP?

 < ? php function printDivisors ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) echo $ i , " ▁ " ; } echo " The ▁ divisors ▁ of ▁ 100 ▁ are :
 " ; printDivisors ( 100 ) ; ? >


"Find all possible outcomes of a given expression | Utility function to eval1uate a simple expression with one operator only . ; This function eval1uates all possible values and returns a list of eval1uated values . ; To store result ( all possible evaluations of given expression ' expr ' ) ; If there is only one character , it must be a digit ( or operand ) , return it . ; If there are only three characters , middle one must be operator and corner ones must be operand ; every i refers to an operator ; l refers to all the possible values in the left of operator ' expr [ i ] ' ; r refers to all the possible values in the right of operator ' expr [ i ] ' ; Take above eval1uated all possible values in left side of ' i ' ; Take above eval1uated all possible values in right side of ' i ' ; Calculate value for every pair and add the value to result . ; Driver Code"
How can the above be solved in PHP?

 < ? php function eval1 ( $ a , $ op , $ b ) { if ( $ op == ' + ' ) return $ a + $ b ; if ( $ op == ' - ' ) return $ a - $ b ; if ( $ op == ' * ' ) return $ a * $ b ; } function eval1uateAll ( $ expr , $ low , $ high ) { $ res = array ( ) ; if ( $ low == $ high ) { array_push ( $ res , ord ( $ expr [ $ low ] ) - ord ( ' 0 ' ) ) ; return $ res ; } if ( $ low == ( $ high - 2 ) ) { $ num = eval1 ( ord ( $ expr [ $ low ] ) - ord ( '0' ) , $ expr [ $ low + 1 ] , ord ( $ expr [ $ low + 2 ] ) - ord ( '0' ) ) ; array_push ( $ res , $ num ) ; return $ res ; } for ( $ i = $ low + 1 ; $ i <= $ high ; $ i += 2 ) { $ l = eval1uateAll ( $ expr , $ low , $ i - 1 ) ; $ r = eval1uateAll ( $ expr , $ i + 1 , $ high ) ; for ( $ s1 = 0 ; $ s1 < count ( $ l ) ; $ s1 ++ ) { for ( $ s2 = 0 ; $ s2 < count ( $ r ) ; $ s2 ++ ) { $ val = eval1 ( $ l [ $ s1 ] , $ expr [ $ i ] , $ r [ $ s2 ] ) ; array_push ( $ res , $ val ) ; } } } return $ res ; } $ expr = "1*2 + 3*4" ; $ len = strlen ( $ expr ) ; $ ans = eval1uateAll ( $ expr , 0 , $ len - 1 ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) echo $ ans [ $ i ] . "
 " ; ? >


"Find average of two numbers using bit operation | Function to return the average of x and y using bit operations ; Calculate the average Floor value of ( x + y ) / 2 ; Driver code"
How can the above be solved in PHP?

< ? php function getAverage ( $ x , $ y ) { $ avg = ( $ x & $ y ) + ( ( $ x ^ $ y ) >> 1 ) ; return $ avg ; } $ x = 10 ; $ y = 9 ; echo getAverage ( $ x , $ y ) ; ? >


"Find four missing numbers in an array containing elements from 1 to N | Finds missing 4 numbers in O ( N ) time and O ( 1 ) auxiliary space . ; To keep track of 4 possible numbers greater than length of input array initialized as 0. ; Traverse the input array and mark visited elements either by marking them as negative in arr [ ] or in helper [ ] . ; If element is smaller than or equal to length , mark its presence in arr [ ] ; Mark presence in helper [ ] ; Print all those elements whose presence is not marked . ; Driver code"
How can the above be solved in PHP?


"Find if a point lies inside a Circle | PHP program to check if a point lies inside a circle or not ; Compare radius of circle with distance of its center from given point ; Driver Code"
How can the above be solved in PHP?

< ? php function isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) { if ( ( $ x - $ circle_x ) * ( $ x - $ circle_x ) + ( $ y - $ circle_y ) * ( $ y - $ circle_y ) <= $ rad * $ rad ) return true ; else return false ; } $ x = 1 ; $ y = 1 ; $ circle_x = 0 ; $ circle_y = 1 ; $ rad = 2 ; if ( isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) ) echo " Inside " ; else echo " Outside " ; ? >


"Find k closest elements to a given value | Function to find the cross over point ( the point before which elements are smaller than or equal to x and after which greater than x ) ; Base cases x is greater than all ; x is smaller than all ; Find the middle point ; If x is same as middle element , then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; This function prints k closest elements to x in arr [ ] . n is the number of elements in arr [ ] ; Find the crossover point ; Right index to search ; To keep track of count of elements already printed ; If x is present in arr [ ] , then reduce left index Assumption : all elements in arr [ ] are distinct ; Compare elements on left and right of crossover point to find the k closest elements ; If there are no more elements on right side , then print left elements ; If there are no more elements on left side , then print right elements ; Driver Code"
How can the above be solved in PHP?

< ? php function findCrossOver ( $ arr , $ low , $ high , $ x ) { if ( $ arr [ $ high ] <= $ x ) return $ high ; if ( $ arr [ $ low ] > $ x ) return $ low ; $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] <= $ x and $ arr [ $ mid + 1 ] > $ x ) return $ mid ; if ( $ arr [ $ mid ] < $ x ) return findCrossOver ( $ arr , $ mid + 1 , $ high , $ x ) ; return findCrossOver ( $ arr , $ low , $ mid - 1 , $ x ) ; } function printKclosest ( $ arr , $ x , $ k , $ n ) { $ l = findCrossOver ( $ arr , 0 , $ n - 1 , $ x ) ; $ r = $ l + 1 ; $ count = 0 ; if ( $ arr [ $ l ] == $ x ) $ l -- ; while ( $ l >= 0 and $ r < $ n and $ count < $ k ) { if ( $ x - $ arr [ $ l ] < $ arr [ $ r ] - $ x ) echo $ arr [ $ l -- ] , " ▁ " ; else echo $ arr [ $ r ++ ] , " ▁ " ; $ count ++ ; } while ( $ count < $ k and $ l >= 0 ) echo $ arr [ $ l -- ] , " ▁ " ; $ count ++ ; while ( $ count < $ k and $ r < $ n ) echo $ arr [ $ r ++ ] ; $ count ++ ; } $ arr = array ( 12 , 16 , 22 , 30 , 35 , 39 , 42 , 45 , 48 , 50 , 53 , 55 , 56 ) ; $ n = count ( $ arr ) ; $ x = 35 ; $ k = 4 ; printKclosest ( $ arr , $ x , 4 , $ n ) ; ? >


"Find largest d in array such that a + b + c = d | function to find largest d ; sort the array in ascending order ; iterating from backwards to find the required largest d ; since all four a , b , c , d should be distinct ; if the current combination of j , k , l in the set is equal to S [ i ] return this value as this would be the largest d since we are iterating in descending order ; Driver Code"
How can the above be solved in PHP?

< ? php function findLargestd ( $ S , $ n ) { $ found = false ; sort ( $ S ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) continue ; for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) { if ( $ i == $ k ) continue ; for ( $ l = $ k + 1 ; $ l < $ n ; $ l ++ ) { if ( $ i == $ l ) continue ; if ( $ S [ $ i ] == $ S [ $ j ] + $ S [ $ k ] + $ S [ $ l ] ) { $ found = true ; return $ S [ $ i ] ; } } } } } if ( $ found == false ) return PHP_INT_MIN ; } $ S = array ( 2 , 3 , 5 , 7 , 12 ) ; $ n = count ( $ S ) ; $ ans = findLargestd ( $ S , $ n ) ; if ( $ ans == PHP_INT_MIN ) echo " No ▁ Solution " ; else echo " Largest ▁ d ▁ such ▁ that ▁ a ▁ + ▁ b ▁ + ▁ " , " c ▁ = ▁ d ▁ is ▁ " , $ ans ; ? >


"Find maximum average subarray of k length | Returns beginning index of maximum average subarray of length ' k ' ; Check if ' k ' is valid ; Compute sum of first ' k ' elements ; Compute sum of remaining subarrays ; Return starting index ; Driver Code"
How can the above be solved in PHP?

< ? php function findMaxAverage ( $ arr , $ n , $ k ) { if ( $ k > $ n ) return -1 ; $ sum = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ k ; $ i ++ ) $ sum += $ arr [ $ i ] ; $ max_sum = $ sum ; $ max_end = $ k - 1 ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + $ arr [ $ i ] - $ arr [ $ i - $ k ] ; if ( $ sum > $ max_sum ) { $ max_sum = $ sum ; $ max_end = $ i ; } } return $ max_end - $ k + 1 ; } $ arr = array ( 1 , 12 , -5 , -6 , 50 , 3 ) ; $ k = 4 ; $ n = count ( $ arr ) ; echo " The ▁ maximum ▁ average ▁ subarray ▁ of ▁ " , " length ▁ " , $ k , " ▁ begins ▁ at ▁ index ▁ " , findMaxAverage ( $ arr , $ n , $ k ) ; ? >


"Find maximum average subarray of k length | Returns beginning index of maximum average subarray of length ' k ' ; Check if ' k ' is valid ; Create and fill array to store cumulative sum . csum [ i ] stores sum of arr [ 0 ] to arr [ i ] ; Initialize max_sm as sum of first subarray ; Find sum of other subarrays and update max_sum if required . ; Return starting index ; Driver Code"
How can the above be solved in PHP?

< ? php function findMaxAverage ( $ arr , $ n , $ k ) { if ( $ k > $ n ) return -1 ; $ csum = array ( ) ; $ csum [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ csum [ $ i ] = $ csum [ $ i - 1 ] + $ arr [ $ i ] ; $ max_sum = $ csum [ $ k - 1 ] ; $ max_end = $ k - 1 ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ curr_sum = $ csum [ $ i ] - $ csum [ $ i - $ k ] ; if ( $ curr_sum > $ max_sum ) { $ max_sum = $ curr_sum ; $ max_end = $ i ; } } return $ max_end - $ k + 1 ; } $ arr = array ( 1 , 12 , -5 , -6 , 50 , 3 ) ; $ k = 4 ; $ n = count ( $ arr ) ; echo " The ▁ maximum ▁ average ▁ subarray ▁ of ▁ " , " length ▁ " , $ k , " ▁ begins ▁ at ▁ index ▁ " , findMaxAverage ( $ arr , $ n , $ k ) ; ? >


"Find minimum adjustment cost of an array | PHP program to find minimum adjustment cost of an array ; Function to find minimum adjustment cost of an array ; dp [ i ] [ j ] stores minimal adjustment cost on changing A [ i ] to j ; handle first element of array separately ; do for rest elements of the array ; replace A [ i ] to j and calculate minimal adjustment cost dp [ i ] [ j ] ; initialize minimal adjustment cost to INT_MAX ; consider all k such that k >= max ( j - target , 0 ) and k <= min ( M , j + target ) and take minimum ; return minimum value from last row of dp table ; Driver Code"
How can the above be solved in PHP?

< ? php $ M = 100 ; function minAdjustmentCost ( $ A , $ n , $ target ) { global $ M ; $ dp = array ( array ( ) ) ; for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) $ dp [ 0 ] [ $ j ] = abs ( $ j - $ A [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) { $ dp [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = max ( $ j - $ target , 0 ) ; $ k <= min ( $ M , $ j + $ target ) ; $ k ++ ) $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i ] [ $ j ] , $ dp [ $ i - 1 ] [ $ k ] + abs ( $ A [ $ i ] - $ j ) ) ; } } $ res = PHP_INT_MAX ; for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) $ res = min ( $ res , $ dp [ $ n - 1 ] [ $ j ] ) ; return $ res ; } $ arr = array ( 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 ) ; $ n = count ( $ arr ) ; $ target = 10 ; echo " Minimum ▁ adjustment ▁ cost ▁ is ▁ " , minAdjustmentCost ( $ arr , $ n , $ target ) ; ? >


"Find most significant set bit of a number | PHP program to find MSB number for given n . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; Driver code"
How can the above be solved in PHP?


"Find most significant set bit of a number | PHP program to find MSB number for given n . ; To find the position of the most significant set bit ; To return the the value of the number with set bit at k - th position ; Driver code"
How can the above be solved in PHP?

< ? php function setBitNumber ( $ n ) { $ k = ( int ) ( log ( $ n , 2 ) ) ; return 1 << $ k ; } $ n = 273 ; echo setBitNumber ( $ n ) ; ? >


"Find most significant set bit of a number | Simple PhP program to find MSB number for given n . ; Driver code"
How can the above be solved in PHP?

< ? php function setBitNumber ( $ n ) { if ( $ n == 0 ) return 0 ; $ msb = 0 ; $ n = $ n / 2 ; while ( $ n != 0 ) { $ n = $ n / 2 ; $ msb ++ ; } return ( 1 << $ msb ) ; } $ n = 0 ; echo setBitNumber ( $ n ) ; ? >


"Find nth number that contains the digit k or divisible by k . | Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy the criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver code"
How can the above be solved in PHP?

< ? php function checkdigit ( $ n , $ k ) { while ( $ n ) { $ rem = $ n % 10 ; if ( $ rem == $ k ) return 1 ; $ n = $ n / 10 ; } return 0 ; } function findNthNumber ( $ n , $ k ) { for ( $ i = $ k + 1 , $ count = 1 ; $ count < $ n ; $ i ++ ) { if ( checkdigit ( $ i , $ k ) || ( $ i % $ k == 0 ) ) $ count ++ ; if ( $ count == $ n ) return $ i ; } return -1 ; } $ n = 10 ; $ k = 2 ; echo findNthNumber ( $ n , $ k ) ; ? >


"Find number of diagonals in n sided convex polygon | PHP function to find number of diagonals in n sided convex polygon ; Driver Code"
How can the above be solved in PHP?

< ? php function numberOfDiagonals ( $ n ) { return $ n * ( $ n - 3 ) / 2 ; } $ n = 5 ; echo $ n , " ▁ sided ▁ convex ▁ polygon ▁ have ▁ " ; echo numberOfDiagonals ( $ n ) , " ▁ diagonals " ;


"Find the Longest Increasing Subsequence in Circular manner | Utility function to find LIS using Dynamic programming ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Set j on the basis of current window i . e . first element of the current window ; Pick maximum of all LIS values ; Function to find LIS in Circular manner ; Make a copy of given array by appending same array elements to itself ; Perform LIS for each window of size n ; Driver Code"
How can the above be solved in PHP?

< ? php function computeLIS ( $ circBuff , $ start , $ end , $ n ) { $ LIS = Array ( ) ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ LIS [ $ i ] = 1 ; for ( $ i = $ start + 1 ; $ i < $ end ; $ i ++ ) for ( $ j = $ start ; $ j < $ i ; $ j ++ ) if ( $ circBuff [ $ i ] > $ circBuff [ $ j ] && $ LIS [ $ i ] < $ LIS [ $ j ] + 1 ) $ LIS [ $ i ] = $ LIS [ $ j ] + 1 ; $ res = PHP_INT_MIN ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ res = max ( $ res , $ LIS [ $ i ] ) ; return $ res ; } function LICS ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i ] ; for ( $ i = $ n ; $ i < 2 * $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i - $ n ] ; $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = max ( computeLIS ( $ circBuff , $ i , $ i + $ n , $ n ) , $ res ) ; return $ res ; } $ arr = array ( 1 , 4 , 6 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Length ▁ of ▁ LICS ▁ is ▁ " , LICS ( $ arr , $ n ) ; ? >


"Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ... | calculate Nth term of given series ; Driver code"
How can the above be solved in PHP?

< ? php function Nth_Term ( $ n ) { return ( 2 * pow ( $ n , 3 ) - 3 * pow ( $ n , 2 ) + $ n + 6 ) / 6 ; } $ N = 8 ; echo Nth_Term ( $ N ) ; ? >


"Find the maximum repeating number in O ( n ) time and O ( 1 ) extra space | Returns maximum repeating element in arr [ 0. . n - 1 ] . The array elements are in range from 0 to k - 1 ; Iterate though input array , for every element arr [ i ] , increment arr [ arr [ i ] % k ] by k ; Find index of the maximum repeating element ; Return index of the maximum element ; Driver Code"
How can the above be solved in PHP?

< ? php function maxRepeating ( $ arr , $ n , $ k ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ arr [ $ i ] % $ k ] += $ k ; $ max = $ arr [ 0 ] ; $ result = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ max ) { $ max = $ arr [ $ i ] ; $ result = $ i ; } } return $ result ; } $ arr = array ( 2 , 3 , 3 , 5 , 3 , 4 , 1 , 7 ) ; $ n = sizeof ( $ arr ) ; $ k = 8 ; echo " The ▁ maximum ▁ repeating ▁ number ▁ is ▁ " , maxRepeating ( $ arr , $ n , $ k ) ; ? >


"Find the minimum cost to reach destination using a train | A Dynamic Programming based solution to find min cost to reach station N - 1 from station 0. ; This function returns the smallest possible cost to reach station N - 1 from station 0. ; dist [ i ] stores minimum cost to reach station i from station 0. ; Go through every station and check if using it as an intermediate station gives better path ; Driver program to test above function"
How can the above be solved in PHP?

< ? php $ INF = PHP_INT_MAX ; $ N = 4 ; function minCost ( $ cost ) { global $ INF ; global $ N ; $ dist [ $ N ] = array ( ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ dist [ $ i ] = $ INF ; $ dist [ 0 ] = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ dist [ $ j ] > $ dist [ $ i ] + $ cost [ $ i ] [ $ j ] ) $ dist [ $ j ] = $ dist [ $ i ] + $ cost [ $ i ] [ $ j ] ; return $ dist [ $ N - 1 ] ; } $ cost = array ( array ( 0 , 15 , 80 , 90 ) , array ( INF , 0 , 40 , 50 ) , array ( INF , INF , 0 , 70 ) , array ( INF , INF , INF , 0 ) ) ; echo " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " , $ N , " ▁ is ▁ " , minCost ( $ cost ) ; ? >


"Find the minimum difference between Shifted tables of two numbers | Utility function to find GCD of a and b ; Returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' . ; Calculate gcd of a nd b ; Calculate difference between x and y ; Driver Code"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { while ( $ b != 0 ) { $ t = $ b ; $ b = $ a % $ b ; $ a = $ t ; } return $ a ; } function findMinDiff ( $ a , $ b , $ x , $ y ) { $ g = gcd ( $ a , $ b ) ; $ diff = abs ( $ x - $ y ) % $ g ; return min ( $ diff , $ g - $ diff ) ; } $ a = 20 ; $ b = 52 ; $ x = 5 ; $ y = 7 ; echo findMinDiff ( $ a , $ b , $ x , $ y ) , "
 " ; ? >


"Find the repeating and the missing number using two equations | Function to print the required numbers ; Sum of first n natural numbers ; Sum of squares of first n natural numbers ; To store the sum and sum of squares of the array elements ; Driver code"
How can the above be solved in PHP?


"Find the smallest missing number | function that returns smallest elements missing in a sorted array . ; Left half has all elements from 0 to mid ; Driver Code"
How can the above be solved in PHP?

< ? php function findFirstMissing ( $ array , $ start , $ end ) { if ( $ start > $ end ) return $ end + 1 ; if ( $ start != $ array [ $ start ] ) return $ start ; $ mid = ( $ start + $ end ) / 2 ; if ( $ array [ $ mid ] == $ mid ) return findFirstMissing ( $ array , $ mid + 1 , $ end ) ; return findFirstMissing ( $ array , $ start , $ mid ) ; } $ arr = array ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 10 ) ; $ n = count ( $ arr ) ; echo " Smallest ▁ missing ▁ element ▁ is ▁ " , findFirstMissing ( $ arr , 2 , $ n - 1 ) ; ? >


"Find the sum of first N odd Fibonacci numbers | PHP program to Find the sum of first N odd Fibonacci numbers ; Function to calculate sum of first N odd Fibonacci numbers ; base values ; Driver code"
How can the above be solved in PHP?


"Find the two numbers with odd occurrences in an unsorted array | Prints two numbers that occur odd number of times . The function assumes that the array size is at least 2 and there are exactly two numbers occurring odd number of times . ; Will hold XOR of two odd occurring elements ; Will have only single set bit of xor2 ; Get the xor of all elements in arr [ ] . The xor will basically be xor of two odd occurring elements ; Get one set bit in the xor2 . We get rightmost set bit in the following line as it is easy to get ; Now divide elements in two sets : 1 ) The elements having the corresponding bit as 1. 2 ) The elements having the corresponding bit as 0. ; XOR of first set is finally going to hold one odd occurring number x ; XOR of second set is finally going to hold the other odd occurring number y ; Driver Code"
How can the above be solved in PHP?

< ? php function printTwoOdd ( $ arr , $ size ) { $ xor2 = $ arr [ 0 ] ; $ set_bit_no ; $ i ; $ n = $ size - 2 ; $ x = 0 ; $ y = 0 ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ xor2 = $ xor2 ^ $ arr [ $ i ] ; $ set_bit_no = $ xor2 & ~ ( $ xor2 - 1 ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } echo " The ▁ two ▁ ODD ▁ elements ▁ are ▁ " , $ x , " ▁ & ▁ " , $ y ; } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 3 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; printTwoOdd ( $ arr , $ arr_size ) ; ? >


"Find the value of XXXX ... . . ( N times ) % M where N is large | Iterative function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; y must be even now y = y 2 ; Function to return XXX ... . . ( N times ) % M ; Return the mod by M of smaller numbers ; Creating a string of N X 's ; Converting the string to int and calculating the modulo ; Checking the parity of N ; Dividing the number into equal half ; Utilizing the formula for even N ; Dividing the number into equal half ; Utilizing the formula for odd N ; Driver code ; Print XXX ... ( N times ) % M"
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 ; $ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function findModuloByM ( $ X , $ N , $ M ) { if ( $ N < 6 ) { $ temp = chr ( 48 + $ X ) * $ N ; $ res = intval ( $ temp ) % $ M ; return $ res ; } if ( $ N % 2 == 0 ) { $ half = findModuloByM ( $ X , ( int ) ( $ N / 2 ) , $ M ) % $ M ; $ res = ( $ half * power ( 10 , ( int ) ( $ N / 2 ) , $ M ) + $ half ) % $ M ; return $ res ; } else { $ half = findModuloByM ( $ X , ( int ) ( $ N / 2 ) , $ M ) % $ M ; $ res = ( $ half * power ( 10 , ( int ) ( $ N / 2 ) + 1 , $ M ) + $ half * 10 + $ X ) % $ M ; return $ res ; } } $ X = 6 ; $ N = 14 ; $ M = 9 ; print ( findModuloByM ( $ X , $ N , $ M ) ) ; ? >


"Find trace of matrix formed by adding Row | Return sum of first n integers of an AP ; Return the trace of sum of row - major matrix and column - major matrix ; Finding nth element in AP in case of Row major matrix . ; Finding sum of first n integers of AP in case of Row major matrix ; Finding nth element in AP in case of Row major matrix ; Finding sum of first n integers of AP in case of Column major matrix ; Driver Code"
How can the above be solved in PHP?

 < ? php function sn ( $ n , $ an ) { return ( $ n * ( 1 + $ an ) ) / 2 ; } function trace ( $ n , $ m ) { $ an = 1 + ( $ n - 1 ) * ( $ m + 1 ) ; $ rowmajorSum = sn ( $ n , $ an ) ; $ an = 1 + ( $ n - 1 ) * ( $ n + 1 ) ; $ colmajorSum = sn ( $ n , $ an ) ; return $ rowmajorSum + $ colmajorSum ; } $ N = 3 ; $ M = 3 ; echo trace ( $ N , $ M ) , "
 " ; ? >


"Finding a Non Transitive Co | Function to return gcd of a and b ; function to check for gcd ; a and b are coprime if their gcd is 1. ; Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt ; Generate and check for all possible triplets between L and R ; if we find any such triplets set flag to true ; flag = True indicates that a pair exists between L and R ; Driver Code ; finding possible Triplet between 2 and 10 ; finding possible Triplet between 23 and 46"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function coprime ( $ a , $ b ) { return ( gcd ( $ a , $ b ) == 1 ) ; } function possibleTripletInRange ( $ L , $ R ) { $ flag = false ; $ possibleA ; $ possibleB ; $ possibleC ; for ( $ a = $ L ; $ a <= $ R ; $ a ++ ) { for ( $ b = $ a + 1 ; $ b <= $ R ; $ b ++ ) { for ( $ c = $ b + 1 ; $ c <= $ R ; $ c ++ ) { if ( coprime ( $ a , $ b ) && coprime ( $ b , $ c ) && ! coprime ( $ a , $ c ) ) { $ flag = true ; $ possibleA = $ a ; $ possibleB = $ b ; $ possibleC = $ c ; break ; } } } } if ( $ flag == true ) { echo " ( " , $ possibleA , " , ▁ " , $ possibleB , " , ▁ " , $ possibleC , " ) " , " ▁ is ▁ one ▁ such ▁ possible ▁ triplet ▁ between ▁ " , $ L , " ▁ and ▁ " , $ R , "
 " ; } else { echo " No ▁ Such ▁ Triplet ▁ exists ▁ between ▁ " , $ L , " ▁ and ▁ " , $ R , "
 " ; } } $ L ; $ R ; $ L = 2 ; $ R = 10 ; possibleTripletInRange ( $ L , $ R ) ; $ L = 23 ; $ R = 46 ; possibleTripletInRange ( $ L , $ R ) ; ? >


"Form lexicographically smallest string with minimum replacements having equal number of 0 s , 1 s and 2 s | Function that returns the modified lexicographically smallest string after performing minimum number of given operations ; Stores the initial frequencies of characters 0 s , 1 s and 2 s ; Stores number of processed characters upto that point of each type ; Required number of characters of each type ; If the current type has already reqd number of characters , no need to perform any operation ; Process all 3 cases ; Check for 1 first ; Else 2 ; Here we need to check processed [ 1 ] only for 2 since 0 is less than 1 and we can replace it anytime ; Here we can replace 2 with 0 and 1 anytime ; keep count of processed characters of each type ; Driver Code"
How can the above be solved in PHP?


"Form minimum number from given sequence | Prints the minimum number that can be formed from input sequence of I ' s ▁ and ▁ D ' s ; Initialize current_max ( to make sure that we don 't use repeated  character ; Initialize last_entry ( Keeps track for last printed digit ) ; Iterate over input array ; Initialize ' noOfNextD ' to get count of next D 's available ; If letter is ' I ' Calculate number of next consecutive D 's  available ; If ' I ' is first letter , print incremented sequence from 1 ; Set max digit reached ; If not first letter Get next digit to print ; Print digit for I ; For all next consecutive ' D ' print decremented sequence ; If letter is ' D ' ; If ' D ' is first letter in sequence . Find number of Next D 's available ; Calculate first digit to print based on number of consecutive D 's ; Print twice for the first time ; Store last entry ; If current ' D ' is not first letter Decrement last_entry ; Driver Code"
How can the above be solved in PHP?

 < ? php function PrintMinNumberForPattern ( $ arr ) { $ curr_max = 0 ; $ last_entry = 0 ; $ j ; for ( $ i = 0 ; $ i < strlen ( $ arr ) ; $ i ++ ) { $ noOfNextD = 0 ; switch ( $ arr [ $ i ] ) { case ' I ' : $ j = $ i + 1 ; while ( $ arr [ $ j ] == ' D ' && $ j < strlen ( $ arr ) ) { $ noOfNextD ++ ; $ j ++ ; } if ( $ i == 0 ) { $ curr_max = $ noOfNextD + 2 ; echo " ▁ " , ++ $ last_entry ; echo " ▁ " , $ curr_max ; $ last_entry = $ curr_max ; } else { $ curr_max = $ curr_max + $ noOfNextD + 1 ; $ last_entry = $ curr_max ; echo " ▁ " , $ last_entry ; } for ( $ k = 0 ; $ k < $ noOfNextD ; $ k ++ ) { echo " ▁ " , -- $ last_entry ; $ i ++ ; } break ; case ' D ' : if ( $ i == 0 ) { $ j = $ i + 1 ; while ( ( $ arr [ $ j ] == ' D ' ) && ( $ j < strlen ( $ arr ) ) ) { $ noOfNextD ++ ; $ j ++ ; } $ curr_max = $ noOfNextD + 2 ; echo " ▁ " , $ curr_max , " ▁ " , $ curr_max - 1 ; $ last_entry = $ curr_max - 1 ; } else { echo " ▁ " , $ last_entry - 1 ; $ last_entry -- ; } break ; } } echo "
 " ; } PrintMinNumberForPattern ( " IDID " ) ; PrintMinNumberForPattern ( " I " ) ; PrintMinNumberForPattern ( " DD " ) ; PrintMinNumberForPattern ( " II " ) ; PrintMinNumberForPattern ( " DIDI " ) ; PrintMinNumberForPattern ( " IIDDD " ) ; PrintMinNumberForPattern ( " DDIDDIID " ) ; ? >


"Given a HUGE number check if it 's a power of two. | Function to check whether a number is power of 2 or not ; Driver Code"
How can the above be solved in PHP?

< ? php function ispowerof2 ( $ num ) { if ( ( $ num & ( $ num - 1 ) ) == 0 ) return 1 ; return 0 ; } $ num = 549755813888 ; echo ispowerof2 ( $ num ) ; ? >


"Given a HUGE number check if it 's a power of two. | returns 1 when str is power of 2 return 0 when str is not a power of 2 ; sum stores the intermediate dividend while dividing . ; if the input is "1" then return 0 because 2 ^ k = 1 where k >= 1 and here k = 0 ; Divide the number until it gets reduced to 1 if we are successfully able to reduce the number to 1 it means input string is power of two if in between an odd number appears at the end it means string is not divisible by two hence not a power of 2. ; if the last digit is odd then string is not divisible by 2 hence not a power of two return 0. ; divide the whole string by 2. i is used to track index in current number . j is used to track index for next iteration . ; if num < 2 then we have to take another digit to the right of A [ i ] to make it bigger than A [ i ] . E . g . 214 / 2 -- > 107 ; if it 's not the first index. E.g 214  then we have to include 0. ; for eg . "124" we will not write 064 so if it is the first index just ignore ; After every division by 2 the length of string is changed . ; if the string reaches to 1 then the str is a power of 2. ; Driver code ."
How can the above be solved in PHP?


"Given a string and an integer k , find the kth sub | Function to prints kth sub - string ; Total sub - strings possible ; If k is greater than total number of sub - strings ; To store number of sub - strings starting with ith character of the string ; Compute the values ; substring [ i - 1 ] is added to store the cumulative sum ; Binary search to find the starting index of the kth sub - string ; To store the ending index of the kth sub - string ; Print the sub - string ; Driver code"
How can the above be solved in PHP?

 < ? php function Printksubstring ( $ str , $ n , $ k ) { $ total = floor ( ( $ n * ( $ n + 1 ) ) / 2 ) ; if ( $ k > $ total ) { printf ( " - 1
 " ) ; return ; } $ substring = array ( ) ; $ substring [ 0 ] = 0 ; $ temp = $ n ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ substring [ $ i ] = $ substring [ $ i - 1 ] + $ temp ; $ temp -- ; } $ l = 1 ; $ h = $ n ; $ start = 0 ; while ( $ l <= $ h ) { $ m = floor ( ( $ l + $ h ) / 2 ) ; if ( $ substring [ $ m ] > $ k ) { $ start = $ m ; $ h = $ m - 1 ; } else if ( $ substring [ $ m ] < $ k ) $ l = $ m + 1 ; else { $ start = $ m ; break ; } } $ end = $ n - ( $ substring [ $ start ] - $ k ) ; for ( $ i = $ start - 1 ; $ i < $ end ; $ i ++ ) print ( $ str [ $ i ] ) ; } $ str = " abc " ; $ k = 4 ; $ n = strlen ( $ str ) ; Printksubstring ( $ str , $ n , $ k ) ; ? >


"Given an n x n square matrix , find sum of all sub | Size of given matrix ; A O ( n ^ 2 ) function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; 1 : PREPROCESSING To store sums of all strips of size k x 1 ; Go column by column ; Calculate sum of first k x 1 rectangle in this column ; Calculate sum of remaining rectangles ; 2 : CALCULATE SUM of Sub - Squares using stripSum [ ] [ ] ; Calculate and print sum of first subsquare in this row ; Calculate sum of remaining squares in current row by removing the leftmost strip of previous sub - square and adding a new strip ; Driver Code"
How can the above be solved in PHP?


"Given an n x n square matrix , find sum of all sub | size k x k Size of given matrix ; function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; row number of first cell in current sub - square of size k x k ; column of first cell in current sub - square of size k x k ; Calculate and print sum of current sub - square ; Line separator for sub - squares starting with next row ; Driver Code"
How can the above be solved in PHP?


"Gold Mine Problem | Returns maximum amount of gold that can be collected when journey started from first column and moves allowed are right , right - up and right - down ; Create a table for storing intermediate results and initialize all cells to 0. The first row of goldMineTable gives the maximum gold that the miner can collect when starts that row ; Gold collected on going to the cell on the rigth ( -> ) ; Gold collected on going to the cell to right up ( / ) ; Gold collected on going to the cell to right down ( \ ) ; Max gold collected from taking either of the above 3 paths ; The max amount of gold collected will be the max value in first column of all rows ; Driver code"
How can the above be solved in PHP?

< ? php function getMaxGold ( $ gold , $ m , $ n ) { $ MAX = 100 ; $ goldTable = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ goldTable [ $ i ] [ $ j ] = 0 ; for ( $ col = $ n - 1 ; $ col >= 0 ; $ col -- ) { for ( $ row = 0 ; $ row < $ m ; $ row ++ ) { if ( $ col == $ n - 1 ) $ right = 0 ; else $ right = $ goldTable [ $ row ] [ $ col + 1 ] ; if ( $ row == 0 or $ col == $ n - 1 ) $ right_up = 0 ; else $ right_up = $ goldTable [ $ row - 1 ] [ $ col + 1 ] ; if ( $ row == $ m - 1 or $ col == $ n - 1 ) $ right_down = 0 ; else $ right_down = $ goldTable [ $ row + 1 ] [ $ col + 1 ] ; $ goldTable [ $ row ] [ $ col ] = $ gold [ $ row ] [ $ col ] + max ( $ right , $ right_up , $ right_down ) ; } } $ res = $ goldTable [ 0 ] [ 0 ] ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = max ( $ res , $ goldTable [ $ i ] [ 0 ] ) ; return $ res ; } $ gold = array ( array ( 1 , 3 , 1 , 5 ) , array ( 2 , 2 , 4 , 1 ) , array ( 5 , 0 , 2 , 3 ) , array ( 0 , 6 , 1 , 2 ) ) ; $ m = 4 ; $ n = 4 ; echo getMaxGold ( $ gold , $ m , $ n ) ; ? >


"Hamming distance between two Integers | Function to calculate hamming distance ; Driver code"
How can the above be solved in PHP?

< ? php function hammingDistance ( $ n1 , $ n2 ) { $ x = $ n1 ^ $ n2 ; $ setBits = 0 ; while ( $ x > 0 ) { $ setBits += $ x & 1 ; $ x >>= 1 ; } return $ setBits ; } $ n1 = 9 ; $ n2 = 14 ; echo ( hammingDistance ( 9 , 14 ) ) ; ? >


"Highest power of 2 less than or equal to given number | PHP program to find highest power of 2 smaller than or equal to n . ; Driver code"
How can the above be solved in PHP?

< ? php function highestPowerof2 ( $ n ) { $ p = ( int ) log ( $ n , 2 ) ; return ( int ) pow ( 2 , $ p ) ; } $ n = 10 ; echo highestPowerof2 ( $ n ) ; ? >


"Highest power of 2 less than or equal to given number | PHP program to find highest power of 2 smaller than or equal to n . ; If i is a power of 2 ; Driver code"
How can the above be solved in PHP?

< ? php function highestPowerof2 ( $ n ) { $ res = 0 ; for ( $ i = $ n ; $ i >= 1 ; $ i -- ) { if ( ( $ i & ( $ i - 1 ) ) == 0 ) { $ res = $ i ; break ; } } return $ res ; } $ n = 10 ; echo highestPowerof2 ( $ n ) ; ? >


"Insert minimum number in array so that sum of array becomes prime | PHP program to find minimum number to insert in array so their sum is prime ; function to calculate primes using sieve of eratosthenes ; Find prime number greater than a number ; To return prime number greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; call sieveOfEratostheneses to calculate primes ; To find sum of array elements ; To find prime number greater then sum ; Return difference of sum and num ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100005 ; function sieveOfEratostheneses ( ) { $ isPrime = array_fill ( true , $ MAX , NULL ) ; $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ isPrime [ $ i ] ) { for ( $ j = 2 * $ i ; $ j < $ MAX ; $ j += $ i ) $ isPrime [ $ j ] = false ; } } } function findPrime ( $ n ) { $ num = $ n + 1 ; while ( $ num ) { if ( $ isPrime [ $ num ] ) return $ num ; $ num = $ num + 1 ; } return 0 ; } function minNumber ( & $ arr , $ n ) { sieveOfEratostheneses ( ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ isPrime [ $ sum ] ) return 0 ; $ num = findPrime ( $ sum ) ; return $ num - $ sum ; } $ arr = array ( 2 , 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo minNumber ( $ arr , $ n ) ; return 0 ; ? >