"Group consecutive characters of same type in a string | Function to return the modified string ; Store original string ; Remove all white spaces ; To store the resultant string ; Traverse the string ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant string ; Driver code"
How can the above be solved in PHP?

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


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

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


"Hailstone Numbers | function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver Code ; Function to generate Hailstone Numbers ; Output : Number of Steps"
How can the above be solved in PHP?

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


"Hamming Distance between two strings | function to calculate Hamming distance ; Driver Code ; function call"
How can the above be solved in PHP?

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


"Happy Number | Utility method to return sum of square of digit of n ; method return true if n is Happy number ; initialize slow and fast by n ; move slow number by one iteration ; move fast number by two iteration ; if both number meet at 1 , then return true ; Driver Code"
How can the above be solved in PHP?

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


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

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


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

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


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

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


"Haversine formula to find distance between two points on a sphere | PHP program for the haversine formula ; distance between latitudes and longitudes ; convert to radians ; apply formulae ; Driver code"
How can the above be solved in PHP?


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

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


"Hendecagonal number | Function to find Hendecagonal number ; Formula to calculate nth Hendecagonal number ; Driver Code"
How can the above be solved in PHP?

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


"Heptagonal number | Function to return Nth Heptagonal number ; Driver Code"
How can the above be solved in PHP?

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


"Hexadecagonal number | Function to calculate hexadecagonal number ; Driver Code"
How can the above be solved in PHP?

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


"Highest power of 2 that divides a number represented in binary | Function to return the highest power of 2 which divides the given binary number ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code"
How can the above be solved in PHP?

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


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

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


"Highest power of two that divides a given number | PHP program to find highest power of 2 that divides n . ; Driver Code"
How can the above be solved in PHP?

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


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

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


"Hilbert Number | Utility function to return Nth Hilbert Number ; Driver code"
How can the above be solved in PHP?

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


"Hosoya 's Triangle | PHP Program to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driven Code"
How can the above be solved in PHP?


"Hosoya 's Triangle | PHP Program to print Hosoya 's triangle of height n. ; Print the Hosoya triangle of height n . ; base case . ; For each row . ; for each column ; ; recursive steps . ; printing the solution ; Driven Program"
How can the above be solved in PHP?


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

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


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

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


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

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


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

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


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

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


"How to compute mod of a big number ? | Function to compute num ( mod a ) ; Initialize result ; One by one process all digits of ' num ' ; Driver Code"
How can the above be solved in PHP?


"How to swap two numbers without using a temporary variable ? | Driver code ; Code to swap ' x ' and ' y ' x now becomes 50 ; y becomes 10 ; x becomes 5"
How can the above be solved in PHP?


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


"How to swap two numbers without using a temporary variable ? | Swap function ; Driver Code"
How can the above be solved in PHP?

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


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

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


"Hypercube Graph | PHP program to find vertices in a hypercube graph of order n ; Function to find power of 2 ; Driver Code"
How can the above be solved in PHP?

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


"Icosidigonal number | Function to calculate Icosidigonal number ; Formula for finding nth Icosidigonal number ; Driver Code"
How can the above be solved in PHP?

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


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

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


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

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


"Increasing sequence with given GCD | Function to print the required sequence ; Driver Code"
How can the above be solved in PHP?

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


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

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


"Increment a number without using ++ or + | function that increment the value . ; Invert bits and apply negative sign ; Driver code"
How can the above be solved in PHP?

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


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


"Indexed Sequential Search | PHP program for Indexed Sequential Search ; Storing element ; Storing the index ; Driver code ; Element to search ; Function call"
How can the above be solved in PHP?

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


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

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


"Interchange elements of first and last rows in matrix | PHP code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; input in the array ; printing the interchanged matrix"
How can the above be solved in PHP?


"Interchanging first and second halves of strings | Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . ; Driver Code ; Calling function"
How can the above be solved in PHP?

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


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


"Iterated Logarithm log * ( n ) | Recursive PhP program to find value of Iterated Logarithm ; Driver code"
How can the above be solved in PHP?

 < ? php function _log ( $ x , $ base ) { return ( int ) ( log ( $ x ) / log ( $ base ) ) ; } function recursiveLogStar ( $ n , $ b ) { if ( $ n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( $ n , $ b ) , $ b ) ; else return 0 ; } $ n = 100 ; $ base = 5 ; echo " Log * ( " ▁ , ▁ $ n ▁ , ▁ " ) " , " ▁ = ▁ " , recursiveLogStar ( $ n , $ base ) , "
 " ; ? >


"Iterative Quick Sort | A utility function to swap two elements ; This function is same in both iterative and recursive ; A [ ] -- > Array to be sorted , l -- > Starting index , h -- > Ending index ; Create an auxiliary stack ; initialize top of stack ; push initial values of l and h to stack ; Keep popping from stack while is not empty ; Pop h and l ; Set pivot element at its correct position in sorted array ; If there are elements on left side of pivot , then push left side to stack ; If there are elements on right side of pivot , then push right side to stack ; A utility function to print contents of arr ; Driver code ; Function calling"
How can the above be solved in PHP?

< ? php function swap ( & $ a , & $ b ) { $ t = $ a ; $ a = $ b ; $ b = $ t ; } function partition ( & $ arr , $ l , $ h ) { $ x = $ arr [ $ h ] ; $ i = ( $ l - 1 ) ; for ( $ j = $ l ; $ j <= $ h - 1 ; $ j ++ ) { if ( $ arr [ $ j ] <= $ x ) { $ i ++ ; swap ( $ arr [ $ i ] , $ arr [ $ j ] ) ; } } swap ( $ arr [ $ i + 1 ] , $ arr [ $ h ] ) ; return ( $ i + 1 ) ; } function quickSortIterative ( & $ arr , $ l , $ h ) { $ stack = array_fill ( 0 , $ h - $ l + 1 , 0 ) ; $ top = -1 ; $ stack [ ++ $ top ] = $ l ; $ stack [ ++ $ top ] = $ h ; while ( $ top >= 0 ) { $ h = $ stack [ $ top -- ] ; $ l = $ stack [ $ top -- ] ; $ p = partition ( $ arr , $ l , $ h ) ; if ( $ p - 1 > $ l ) { $ stack [ ++ $ top ] = $ l ; $ stack [ ++ $ top ] = $ p - 1 ; } if ( $ p + 1 < $ h ) { $ stack [ ++ $ top ] = $ p + 1 ; $ stack [ ++ $ top ] = $ h ; } } } function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 4 , 3 , 5 , 2 , 1 , 3 , 2 , 3 ) ; $ n = count ( $ arr ) ; quickSortIterative ( $ arr , 0 , $ n - 1 ) ; printArr ( $ arr , $ n ) ; ? >


"Iterative Quick Sort | Function to swap numbers ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; A [ ] -- > Array to be sorted , l -- > Starting index , h -- > Ending index ; Partitioning index ; Driver code"
How can the above be solved in PHP?


"Jacobsthal and Jacobsthal | Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driver Code"
How can the above be solved in PHP?

 < ? php function Jacobsthal ( $ n ) { $ dp [ $ n + 1 ] ; $ dp [ 0 ] = 0 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + 2 * $ dp [ $ i - 2 ] ; return $ dp [ $ n ] ; } function Jacobsthal_Lucas ( $ n ) { $ dp [ $ n + 1 ] ; $ dp [ 0 ] = 2 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + 2 * $ dp [ $ i - 2 ] ; return $ dp [ $ n ] ; } $ n = 5 ; echo " Jacobsthal ▁ number : ▁ " , Jacobsthal ( $ n ) , "
 " ; echo " Jacobsthal - Lucas ▁ number : ▁ " , Jacobsthal_Lucas ( $ n ) , "
 " ; ? >


"Josephus Problem Using Bit Magic | function to find the position of the Most Significant Bit ; keeps shifting bits to the right until we are left with 0 ; function to return at which place Josephus should sit to avoid being killed ; Getting the position of the Most Significant Bit ( MSB ) . The leftmost '1' . If the number is '41' then its binary is '101001' . So msbPos ( 41 ) = 6 ; ' j ' stores the number with which to XOR the number ' n ' . Since we need '100000' . We will do 1 << 6 - 1 to get '100000' ; Toggling the Most Significant Bit . Changing the leftmost '1' to '0' . 101001 ^ 100000 = 001001 ( 9 ) ; Left - shifting once to add an extra '0' to the right end of the binary number 001001 = 010010 ( 18 ) ; Toggling the '0' at the end to '1' which is essentially the same as putting the MSB at the rightmost place . 010010 | 1 = 010011 ( 19 ) ; Driver Code"
How can the above be solved in PHP?

< ? php function msbPos ( $ n ) { $ pos = 0 ; while ( $ n != 0 ) { $ pos ++ ; $ n = $ n >> 1 ; } return $ pos ; } function josephify ( $ n ) { $ position = msbPos ( $ n ) ; $ j = 1 << ( $ position - 1 ) ; $ n = $ n ^ $ j ; $ n = $ n << 1 ; $ n = $ n | 1 ; return $ n ; } $ n = 41 ; print ( josephify ( $ n ) ) ; ? >


"Jump Search | PHP program to implement Jump Search ; Finding block size to be jumped ; Finding the block where element is present ( if it is present ) ; Doing a linear search for x in block beginning with prev . ; If we reached next block or end of array , element is not present . ; If element is found ; Driver program to test function ; Find the index of ' $ x ' using Jump Search ; Print the index where ' $ x ' is located"
How can the above be solved in PHP?

< ? php function jumpSearch ( $ arr , $ x , $ n ) { $ step = sqrt ( $ n ) ; $ prev = 0 ; while ( $ arr [ min ( $ step , $ n ) - 1 ] < $ x ) { $ prev = $ step ; $ step += sqrt ( $ n ) ; if ( $ prev >= $ n ) return -1 ; } while ( $ arr [ $ prev ] < $ x ) { $ prev ++ ; if ( $ prev == min ( $ step , $ n ) ) return -1 ; } if ( $ arr [ $ prev ] == $ x ) return $ prev ; return -1 ; } $ arr = array ( 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 ) ; $ x = 55 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ index = jumpSearch ( $ arr , $ x , $ n ) ; echo " Number ▁ " . $ x . " ▁ is ▁ at ▁ index ▁ " . $ index ; return 0 ; ? >


"K length words that can be formed from given characters without repetition | Function to return the required count ; To store the count of distinct characters in $str ; Traverse $str character by character ; If current character is appearing for the first time in $str ; Increment the distinct character count ; Update the appearance of the current character ; Since P ( n , r ) = n ! / ( n - r ) ! ; Return the answer ; Driver code"
How can the above be solved in PHP?

< ? php function findPermutation ( $ str , $ k ) { $ has = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { $ has [ $ i ] = false ; } $ cnt = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ has [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] == false ) { $ cnt ++ ; $ has [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] = true ; } } $ ans = 1 ; for ( $ i = 2 ; $ i <= $ cnt ; $ i ++ ) $ ans *= $ i ; for ( $ i = $ cnt - $ k ; $ i > 1 ; $ i -- ) $ ans /= $ i ; return $ ans ; } $ str = " geeksforgeeks " ; $ k = 4 ; echo findPermutation ( $ str , $ k ) ; ? >


"K | Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1' ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Driver code ; get the Nth value of K - Fibonacci series"
How can the above be solved in PHP?


"K | PHP program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 10000 ; function ksmallest ( $ arr , $ n , $ k ) { global $ MAX ; $ b = array_fill ( 0 , $ MAX , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ b [ $ arr [ $ i ] ] = 1 ; for ( $ j = 1 ; $ j < $ MAX ; $ j ++ ) { if ( $ b [ $ j ] != 1 ) $ k -- ; if ( $ k == 0 ) return $ j ; } } $ k = 1 ; $ arr = array ( 1 ) ; $ n = count ( $ arr ) ; echo ksmallest ( $ arr , $ n , $ k ) ; ? >


"K | Program to find kth element from two sorted arrays ; Driver Code"
How can the above be solved in PHP?


"K | Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest element  after removing array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver Code"
How can the above be solved in PHP?

< ? php function ksmallest ( $ arr , $ n , $ k ) { sort ( $ arr ) ; if ( $ k < $ arr [ 0 ] ) return $ k ; if ( $ k == $ arr [ 0 ] ) return $ arr [ 0 ] + 1 ; if ( $ k > $ arr [ $ n - 1 ] ) return $ k + $ n ; if ( $ arr [ 0 ] == 1 ) $ k -- ; else $ k -= ( $ arr [ 0 ] - 1 ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ c = $ arr [ $ i ] - $ arr [ $ i - 1 ] - 1 ; if ( $ k <= $ c ) return $ arr [ $ i - 1 ] + $ k ; else $ k -= $ c ; } return $ arr [ $ n - 1 ] + $ k ; } $ k = 1 ; $ arr = array ( 1 ) ; $ n = sizeof ( $ arr ) ; echo ksmallest ( $ arr , $ n , $ k ) ; ? >


"K | Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print all numbers with k prime factors ; Driver code"
How can the above be solved in PHP?

< ? php function printKPFNums ( $ A , $ B , $ K ) { $ prime = array_fill ( true , $ B + 1 , NULL ) ; $ p_factors = array_fill ( 0 , $ B + 1 , NULL ) ; for ( $ p = 2 ; $ p <= $ B ; $ p ++ ) if ( $ p_factors [ $ p ] == 0 ) for ( $ i = $ p ; $ i <= $ B ; $ i += $ p ) $ p_factors [ $ i ] ++ ; for ( $ i = $ A ; $ i <= $ B ; $ i ++ ) if ( $ p_factors [ $ i ] == $ K ) echo $ i . " " ; } $ A = 14 ; $ B = 18 ; $ K = 2 ; printKPFNums ( $ A , $ B , $ K ) ; ? >


"K | To compute k - th digit in a ^ b ; computing a ^ b ; getting last digit ; increasing count by 1 ; if current number is required digit ; remove last digit ; Driver Code"
How can the above be solved in PHP?

< ? php function kthdigit ( $ a , $ b , $ k ) { $ p = pow ( $ a , $ b ) ; $ count = 0 ; while ( $ p > 0 and $ count < $ k ) { $ rem = $ p % 10 ; $ count ++ ; if ( $ count == $ k ) return $ rem ; $ p = $ p / 10 ; } return 0 ; } $ a = 5 ; $ b = 2 ; $ k = 1 ; echo kthdigit ( $ a , $ b , $ k ) ; ? >


"Kaprekar Constant | This function checks validity of kaprekar ' s ▁ constant . ▁ It ▁ returns ▁ kaprekar ' s constant for any four digit number " n " such that all digits of n are not same . ; Store current n as previous number ; Get four digits of given number ; Sort all four dgits in ascending order And giet in the form of number " asc " ; Get all four dgits in descending order in the form of number " desc " ; Get the difference of two numbers ; If difference is same as previous , we have reached kaprekar 's constant ; Else recur ; A wrapper over kaprekarRec ( ) ; Trying few four digit numbers , we always get 6174"
How can the above be solved in PHP?

 < ? php function kaprekarRec ( $ n , $ prev ) { if ( $ n == 0 ) return 0 ; $ prev = $ n ; $ digits = array_fill ( 0 , 4 , 0 ) ; for ( $ i = 0 ; $ i < 4 ; $ i ++ ) { $ digits [ $ i ] = $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; } sort ( $ digits ) ; $ asc = 0 ; for ( $ i = 0 ; $ i < 4 ; $ i ++ ) $ asc = $ asc * 10 + $ digits [ $ i ] ; rsort ( $ digits ) ; $ desc = 0 ; for ( $ i = 0 ; $ i < 4 ; $ i ++ ) $ desc = $ desc * 10 + $ digits [ $ i ] ; $ diff = abs ( $ asc - $ desc ) ; if ( $ diff == $ prev ) return $ diff ; return kaprekarRec ( $ diff , $ prev ) ; } function kaprekar ( $ n ) { $ rev = 0 ; return kaprekarRec ( $ n , $ rev ) ; } echo kaprekar ( 1000 ) . "
 " ; echo kaprekar ( 1112 ) . "
 " ; echo kaprekar ( 9812 ) . "
 " ; ? >


"Kaprekar Number | Returns true if n is a Kaprekar number , else false ; Count number of digits in square ; Split the square at different points and see if sum of any pair of splitted numbers is equal to n . ; To avoid numbers like 10 , 100 , 1000 ( These are not Karprekar numbers ; Find sum of current parts and compare with n ; compare with original number ; Driver code"
How can the above be solved in PHP?

 < ? php function iskaprekar ( $ n ) { if ( $ n == 1 ) return true ; $ sq_n = $ n * $ n ; $ count_digits = 0 ; while ( $ sq_n ) { $ count_digits ++ ; $ sq_n = ( int ) ( $ sq_n / 10 ) ; } for ( $ r_digits = 1 ; $ r_digits < $ count_digits ; $ r_digits ++ ) { $ eq_parts = pow ( 10 , $ r_digits ) ; if ( $ eq_parts == $ n ) continue ; $ sum = ( int ) ( $ sq_n1 / $ eq_parts ) + $ sq_n1 % $ eq_parts ; if ( $ sum == $ n ) return true ; } return false ; } echo " Printing ▁ first ▁ few ▁ Kaprekar ▁ " . " Numbers ▁ using ▁ iskaprekar ( )
 " ; for ( $ i = 1 ; $ i < 10000 ; $ i ++ ) if ( iskaprekar ( $ i ) ) echo $ i . " " ; ? >


"Keith Number | Returns true if x is Keith , else false . ; Store all digits of x in a vector " terms " Also find number of digits and store in " n " . ; $n = 0 ; n is number of digits in x ; To get digits in right order ( from MSB to LSB ) ; Keep finding next trms of a sequence generated using digits of x until we either reach x or a number greate than x ; Next term is sum of previous n terms ; When the control comes out of the while loop , either the next_term is equal to the number or greater than it . If next_term is equal to x , then x is a Keith number , else not ; Driver Code"
How can the above be solved in PHP?

 < ? php function isKeith ( $ x ) { $ terms = array ( ) ; $ temp = $ x ; while ( $ temp > 0 ) { array_push ( $ terms , $ temp % 10 ) ; $ temp = ( int ) ( $ temp / 10 ) ; $ n ++ ; } $ terms = array_reverse ( $ terms ) ; $ next_term = 0 ; $ i = $ n ; while ( $ next_term < $ x ) { $ next_term = 0 ; for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) $ next_term += $ terms [ $ i - $ j ] ; array_push ( $ terms , $ next_term ) ; $ i ++ ; } return ( $ next_term == $ x ) ; } isKeith ( 14 ) ? print ( " Yes
 " ) : print ( " No
 " ) ; isKeith ( 12 ) ? print ( " Yes
 " ) : print ( " No
 " ) ; isKeith ( 197 ) ? print ( " Yes
 " ) : print ( " No
 " ) ; ? >


"Kth odd number in an array | Function to return the kth odd element from the array ; Traverse the array ; If current element is odd ; If kth odd element is found ; Total odd elements in the array are < k ; Driver code"
How can the above be solved in PHP?


"LCM of digits of a given number | define lcm function ; If at any point LCM become 0. return it ; Driver code"
How can the above be solved in PHP?

< ? php function lcm_fun ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return lcm_fun ( $ b , $ a % $ b ) ; } function digitLCM ( $ n ) { $ lcm = 1 ; while ( $ n > 0 ) { $ lcm = ( int ) ( ( $ n % 10 * $ lcm ) / lcm_fun ( $ n % 10 , $ lcm ) ) ; if ( $ lcm == 0 ) return 0 ; $ n = ( int ) ( $ n / 10 ) ; } return $ lcm ; } $ n = 397 ; echo digitLCM ( $ n ) ; ? >


"LCM of factorial and its neighbors | function to calculate the factorial ; returning the factorial of the largest number in the given three consecutive numbers ; Driver code"
How can the above be solved in PHP?

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


"LCS ( Longest Common Subsequence ) of three strings | PHP program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Driver code"
How can the above be solved in PHP?


"LCS ( Longest Common Subsequence ) of three strings | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver code"
How can the above be solved in PHP?


"Lagrange 's four square theorem | Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; loops checking the sum of squares ; if sum of four squares equals the given no . ; printing the numbers ; Driver Code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6"
How can the above be solved in PHP?


"Larger of a ^ b or b ^ a ( a raised to power b or b raised to power a ) | Function to find the greater value ; Driver code"
How can the above be solved in PHP?

 < ? php function findGreater ( $ a , $ b ) { $ x = ( double ) $ a * ( double ) ( log ( ( double ) ( $ b ) ) ) ; $ y = ( double ) $ b * ( double ) ( log ( ( double ) ( $ a ) ) ) ; if ( $ y > $ x ) { echo " a ^ b ▁ is ▁ greater " , "
 " ; } else if ( $ y < $ x ) { echo " b ^ a ▁ is ▁ greater " , "
 " ; } else { echo " Both ▁ are ▁ equal " , "
 " ; } } $ a = 3 ; $ b = 5 ; $ c = 2 ; $ d = 4 ; findGreater ( $ a , $ b ) ; findGreater ( $ c , $ d ) ; ? >


"Largest Divisor of a Number not divisible by a perfect square | Function to find the largest divisor not divisible by any perfect square greater than 1 ; If the number is divisible by i * i , then remove one i ; Now all squares are removed from n ; Driver Code"
How can the above be solved in PHP?

 < ? php function findLargestDivisor ( $ n ) { for ( $ i = 2 ; $ i < sqrt ( $ n ) + 1 ; $ i ++ ) { while ( $ n % ( $ i * $ i ) == 0 ) { $ n = $ n / $ i ; } } return $ n ; } $ n = 12 ; echo ( findLargestDivisor ( $ n ) ) ; echo ( "
 " ) ; $ n = 97 ; echo ( findLargestDivisor ( $ n ) ) ; ? >


"Largest Even and Odd N | Function to print the largest n - digit even and odd numbers ; Driver code"
How can the above be solved in PHP?

 < ? php function findNumbers ( $ n ) { $ odd = pow ( 10 , $ n ) - 1 ; $ even = $ odd - 1 ; echo " Even ▁ = ▁ $ even ▁
 " ; echo " Odd ▁ = ▁ $ odd " ; } $ n = 4 ; findNumbers ( $ n ) ; ? >


"Largest Square that can be inscribed within a hexagon | Function to find the area of the square ; Side cannot be negative ; Area of the square ; Driver code"
How can the above be solved in PHP?

 < ? php function squareArea ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = pow ( 1.268 , 2 ) * pow ( $ a , 2 ) ; return $ area ; } $ a = 6 ; echo squareArea ( $ a ) , "
 " ; ? >


"Largest Sum Contiguous Subarray |  ; Do not compare for all elements . Compare only when max_ending_here > 0"
How can the above be solved in PHP?

< ? php < ? php function maxSubArraySum ( & $ a , $ size ) { $ max_so_far = $ a [ 0 ] ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; else if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; } return $ max_so_far ; ? >


"Largest Sum Contiguous Subarray |  ; Driver Code"
How can the above be solved in PHP?

< ? php < ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = $ a [ 0 ] ; $ curr_max = $ a [ 0 ] ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) { $ curr_max = max ( $ a [ $ i ] , $ curr_max + $ a [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; } return $ max_so_far ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_sum ; ? >


"Largest Sum Contiguous Subarray | PHP program to print largest contiguous array sum ; Driver Code"
How can the above be solved in PHP?

 < ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; $ start = 0 ; $ end = 0 ; $ s = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here += $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) { $ max_so_far = $ max_ending_here ; $ start = $ s ; $ end = $ i ; } if ( $ max_ending_here < 0 ) { $ max_ending_here = 0 ; $ s = $ i + 1 ; } } echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_so_far . "
 " ; echo " Starting ▁ index ▁ " . $ start . " " .
 	 	 	 " Ending index " ▁ . ▁ $ end ▁ . ▁ " " } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; ? >


"Largest Sum Contiguous Subarray | PHP program to print largest contiguous array sum ; Driver code"
How can the above be solved in PHP?

< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = count ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , $ max_sum ; ? >


"Largest cone that can be inscribed within a cube | Function to find the radius of the cone ; side cannot be negative ; radius of the cone ; Function to find the height of the cone ; side cannot be negative ; height of the cone ; Driver code"
How can the above be solved in PHP?

< ? php function coneRadius ( $ a ) { if ( $ a < 0 ) return -1 ; $ r = $ a / sqrt ( 2 ) ; return $ r ; } function coneHeight ( $ a ) { if ( $ a < 0 ) return -1 ; $ h = $ a ; return $ h ; } $ a = 6 ; echo ( " r ▁ = ▁ " ) ; echo coneRadius ( $ a ) ; echo ( " , ▁ " ) ; echo ( " h ▁ = ▁ " ) ; echo ( coneHeight ( $ a ) ) ; ? >


"Largest cube that can be inscribed within a right circular cone | Function to find the side of the cube ; height and radius cannot be negative ; side of the cube ; Driver code"
How can the above be solved in PHP?

< ? php function cubeSide ( $ h , $ r ) { if ( $ h < 0 && $ r < 0 ) return -1 ; $ a = ( $ h * $ r * sqrt ( 2 ) ) / ( $ h + sqrt ( 2 ) * $ r ) ; return $ a ; } $ h = 5 ; $ r = 6 ; echo cubeSide ( $ h , $ r ) ; ? >


"Largest cube that can be inscribed within a right circular cylinder | Function to find the volume of the cube ; height and radius cannot be negative ; volume of the cube ; Driver code"
How can the above be solved in PHP?

< ? php function cube ( $ h , $ r ) { if ( $ h < 0 && $ r < 0 ) return -1 ; $ a = pow ( $ h , 3 ) ; return $ a ; } $ h = 5 ; $ r = 4 ; echo cube ( $ h , $ r ) ; ? >


"Largest cube that can be inscribed within the sphere | Function to find the side of the cube ; radius cannot be negative ; side of the cube ; Driver code"
How can the above be solved in PHP?

< ? php function largestCube ( $ r ) { if ( $ r < 0 ) return -1 ; $ a = ( float ) ( ( 2 * $ r ) / sqrt ( 3 ) ) ; return $ a ; } $ r = 5 ; echo largestCube ( $ r ) ; ? >


"Largest divisible pairs subset | function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements . ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; Driver Code"
How can the above be solved in PHP?


"Largest element in the array that is repeated exactly k times | Function that finds the largest element which is repeated ' k ' times ; sort the array ; if the value of ' k ' is 1 and the largest appears only once in the array ; counter to count the repeated elements ; check if the element at index ' i ' is equal to the element at index ' i + 1' then increase the count ; else set the count to 1 to start counting the frequency of the new number ; if the count is equal to k and the previous element is not equal to this element ; if there is no such element ; Driver code ; find the largest element that is repeated K times"
How can the above be solved in PHP?


"Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle | Function to find the area of the biggest ellipse ; the radius cannot be negative ; area of the ellipse ; Driver code"
How can the above be solved in PHP?

 < ? php function ellipsearea ( $ r ) { if ( $ r < 0 ) return -1 ; $ a = ( 3.14 * $ r * $ r ) / 4 ; return $ a ; } $ r = 5 ; echo ellipsearea ( $ r ) . "
 " ; ? >


"Largest even digit number not greater than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the largest number with all digits even ; Iterate till we find a number with all digits even ; Driver Code"
How can the above be solved in PHP?

< ? php function checkDigits ( $ n ) { while ( $ n ) { if ( ( $ n % 10 ) % 2 ) return 0 ; $ n /= 10 ; } return 1 ; } function largestNumber ( $ n ) { for ( $ i = $ n ; ; $ i -- ) if ( checkDigits ( $ i ) ) return $ i ; } $ N = 23 ; echo ( largestNumber ( $ N ) ) ; ? >


"Largest even digit number not greater than N | function to return the largest number with all digits even ; convert the number to a string for easy operations ; find first odd digit ; if no digit , then N is the answer ; till first odd digit , add all even numbers ; decrease 1 from the odd digit ; add 0 in the rest of the digits ; Driver Code"
How can the above be solved in PHP?


"Largest factor of a given number which is a perfect square | Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code"
How can the above be solved in PHP?

< ? php function largestSquareFactor ( $ num ) { $ answer = 1 ; for ( $ i = 2 ; $ i < sqrt ( $ num ) ; ++ $ i ) { $ cnt = 0 ; $ j = $ i ; while ( $ num % $ j == 0 ) { $ cnt ++ ; $ j *= $ i ; } if ( $ cnt & 1 ) { $ cnt -- ; $ answer *= pow ( $ i , $ cnt ) ; } else { $ answer *= pow ( $ i , $ cnt ) ; } } return $ answer ; } $ N = 420 ; echo largestSquareFactor ( $ N ) ; ? >


"Largest hexagon that can be inscribed within a square | Function to return the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code"
How can the above be solved in PHP?

< ? php function hexagonside ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = 0.5176 * $ a ; return $ x ; } $ a = 6 ; echo hexagonside ( $ a ) ; ? >


"Largest hexagon that can be inscribed within an equilateral triangle | Function to find the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code"
How can the above be solved in PHP?

< ? php function hexagonside ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = $ a / 3 ; return $ x ; } $ a = 6 ; echo hexagonside ( $ a ) ; ? >


"Largest number N which can be reduced to 0 in K steps | Utility function to return the first digit of a number . ; Remove last digit from number till only one digit is left ; return the first digit ; Utility function that returns the count of numbers written down when starting from n ; Function to find the largest number N which can be reduced to 0 in K steps ; Get the sequence length of the mid point ; Until k sequence length is reached ; Update mid point ; Get count of the new mid point ; Update right to mid ; Update left to mid ; Increment mid point by one while count is equal to k to get the maximum value of mid point ; Driver Code"
How can the above be solved in PHP?

< ? php function firstDigit ( $ n ) { while ( $ n >= 10 ) { $ n = ( int ) ( $ n / 10 ) ; } return $ n ; } function getCount ( $ n ) { $ count = 1 ; while ( $ n != 0 ) { $ leadDigit = firstDigit ( $ n ) ; $ n -= $ leadDigit ; $ count ++ ; } return $ count ; } function getLargestNumber ( $ k ) { $ left = $ k ; $ right = $ k * 10 ; $ mid = ( int ) ( ( $ left + $ right ) / 2 ) ; $ len = getCount ( $ mid ) ; while ( $ len != $ k ) { $ mid = ( int ) ( ( $ left + $ right ) / 2 ) ; $ len = getCount ( $ mid ) ; if ( $ len > $ k ) { $ right = $ mid ; } else { $ left = $ mid ; } } while ( $ len == $ k ) { if ( $ len != getCount ( $ mid + 1 ) ) { break ; } $ mid ++ ; } return ( $ mid ) ; } $ k = 3 ; echo ( getLargestNumber ( $ k ) ) ; ? >


"Largest number by which given 3 numbers should be divided such that they leaves same remainder | __gcd function ; function return number which divides these three number and leaves same remainder . ; We find the differences of all three pairs ; Return GCD of three differences . ; Driver Code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function sameRemainder ( $ a , $ b , $ c ) { $ a1 = ( $ b - $ a ) ; $ b1 = ( $ c - $ b ) ; $ c1 = ( $ c - $ a ) ; return gcd ( $ a1 , gcd ( $ b1 , $ c1 ) ) ; } $ a = 62 ; $ b = 132 ; $ c = 237 ; echo sameRemainder ( $ a , $ b , $ c ) ; ? >


"Largest number in [ 2 , 3 , . . n ] which is co | Returns true if i is co - prime with numbers in set [ 2 , 3 , ... m ] ; Running the loop till square root of n to reduce the time complexity from n ; Find the minimum of square root of n and m to run the loop until the smaller one ; Check from 2 to min ( m , sqrt ( n ) ) ; Function to find the largest number less than n which is Co - prime with all numbers from 2 to m ; Iterating from n to m + 1 to find the number ; checking every number for the given conditions ; The first number which satisfy the conditions is the answer ; If there is no number which satisfy the conditions , then print number does not exist . ; Driver Code"
How can the above be solved in PHP?

 < ? php function isValid ( $ i , $ m ) { $ sq_i = sqrt ( $ i ) ; $ sq = min ( $ m , $ sq_i ) ; for ( $ j = 2 ; $ j <= $ sq ; $ j ++ ) if ( $ i % $ j == 0 ) return false ; return true ; } function findLargestNum ( $ n , $ m ) { for ( $ i = $ n ; $ i > $ m ; $ i -- ) { if ( isValid ( $ i , $ m ) ) { echo $ i , "
 " ; return ; } } echo " Number ▁ Doesn ' t ▁ Exists
 " ; } $ n = 16 ; $ m = 3 ; findLargestNum ( $ n , $ m ) ; ? >


"Largest number in an array that is not a perfect cube | Function to check if a number is perfect cube number or not ; takes the sqrt of the number ; checks if it is a perfect cube number ; Function to find the largest non perfect cube number in the array ; stores the maximum of all perfect cube numbers ; Traverse all elements in the array ; store the maximum if current element is a non perfect cube ; Driver Code"
How can the above be solved in PHP?

< ? php function checkPerfectcube ( $ n ) { $ d = ( int ) round ( pow ( $ n , 1 / 3 ) ) ; if ( $ d * $ d * $ d == $ n ) return true ; return false ; } function largestNonPerfectcubeNumber ( $ a , $ n ) { $ maxi = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ! checkPerfectcube ( $ a [ $ i ] ) ) $ maxi = max ( $ a [ $ i ] , $ maxi ) ; } return $ maxi ; } $ a = array ( 16 , 64 , 25 , 2 , 3 , 10 ) ; $ n = count ( $ a ) ; echo largestNonPerfectcubeNumber ( $ a , $ n ) ; ? >


"Largest number less than N with digit sum greater than the digit sum of N | Function to return the sum of the digits of n ; Loop for each digit of the number ; Function to return the greatest number less than n such that the sum of its digits is greater than the sum of the digits of n ; Starting from n - 1 ; Check until 1 ; If i satisfies the given condition ; If the condition is not satisfied ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfDigits ( $ n ) { $ res = 0 ; while ( $ n > 0 ) { $ res += $ n % 10 ; $ n /= 10 ; } return $ res ; } function findNumber ( $ n ) { $ i = $ n - 1 ; while ( $ i > 0 ) { if ( sumOfDigits ( $ i ) > sumOfDigits ( $ n ) ) return $ i ; $ i -- ; } return -1 ; } $ n = 824 ; echo findNumber ( $ n ) ; ? >


"Largest number less than or equal to N / 2 which is coprime to N | Function to calculate gcd of two number ; Function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; Function to find largest integer less than or equal to N / 2 and coprime with N ; Check one by one all numbers less than or equal to N / 2 ; Driver code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; else return gcd ( $ b , $ a % $ b ) ; } function coPrime ( $ n1 , $ n2 ) { if ( gcd ( $ n1 , $ n2 ) == 1 ) return true ; else return false ; } function largestCoprime ( $ N ) { $ half = floor ( $ N / 2 ) ; while ( coPrime ( $ N , $ half ) == false ) $ half -- ; return $ half ; } $ n = 50 ; echo largestCoprime ( $ n ) ;


"Largest number less than or equal to N / 2 which is coprime to N | Function to find largest integer less than or equal to N / 2 and is coprime with N ; Handle the case for N = 6 ; Driver code"
How can the above be solved in PHP?

< ? php function largestCoprime ( $ N ) { if ( $ N == 6 ) return 1 ; else if ( $ N % 4 == 0 ) return ( $ N / 2 ) - 1 ; else if ( $ N % 2 == 0 ) return ( $ N / 2 ) - 2 ; else return ( ( $ N - 1 ) / 2 ) ; } $ n = 50 ; echo largestCoprime ( $ n ) ; ? >


"Largest number not greater than N all the digits of which are odd | Function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code"
How can the above be solved in PHP?

< ? php function allOddDigits ( $ n ) { while ( $ n > 1 ) { if ( ( $ n % 10 ) % 2 == 0 ) return false ; $ n = ( int ) $ n / 10 ; } return true ; } function largestNumber ( $ n ) { if ( $ n % 2 == 0 ) $ n -- ; for ( $ i = $ n ; ; $ i = ( $ i - 2 ) ) if ( allOddDigits ( $ i ) ) return $ i ; } $ N = 23 ; echo largestNumber ( $ N ) ; ? >


"Largest number smaller than or equal to N divisible by K | Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code"
How can the above be solved in PHP?

< ? php function findNum ( $ N , $ K ) { $ rem = $ N % $ K ; if ( $ rem == 0 ) return $ N ; else return $ N - $ rem ; } $ N = 45 ; $ K = 6 ; echo " Largest ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ " , $ N , " that is divisible by " , ▁ $ K , ▁ " is " findNum ( $ N , $ K ) ; ? >


"Largest number smaller than or equal to n and digits in non | Prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to print it ; Driver Code"
How can the above be solved in PHP?


"Largest number smaller than or equal to n and digits in non | Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver Code"
How can the above be solved in PHP?

< ? php function nondecdigits ( $ n ) { $ x = 0 ; for ( $ x = $ n ; $ x >= 1 ; $ x -- ) { $ no = $ x ; $ prev_dig = 11 ; $ flag = true ; while ( $ no != 0 ) { if ( $ prev_dig < $ no % 10 ) { $ flag = false ; break ; } $ prev_dig = $ no % 10 ; $ no /= 10 ; } if ( $ flag == true ) break ; } return $ x ; } $ n = 200 ; echo nondecdigits ( $ n ) ; ? >


"Largest number that is not a perfect square | PHP program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; Driver Code ; function call"
How can the above be solved in PHP?

< ? php function check ( $ n ) { $ d = sqrt ( $ n ) ; if ( $ d * $ d == $ n ) return true ; return false ; } function largestNonPerfectSquareNumber ( $ a , $ n ) { $ maxi = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ! check ( $ a [ $ i ] ) ) $ maxi = max ( $ a [ $ i ] , $ maxi ) ; } return $ maxi ; } $ a = array ( 16 , 20 , 25 , 2 , 3 , 10 ) ; $ n = count ( $ a ) ; echo largestNonPerfectSquareNumber ( $ a , $ n ) ; ? >


"Largest number with maximum trailing nines which is less than N and greater than N | function to count no of digits ; function to implement above approach ; if difference between power and n doesn 't exceed d ; loop to build a number from the appropriate no of digits containing only 9 ; if the build number is same as original number ( n ) ; observation ; Driver Code ; variable that stores no of digits in n"
How can the above be solved in PHP?

< ? php function dig ( $ a ) { $ count = 0 ; while ( $ a > 0 ) { $ a = ( int ) ( $ a / 10 ) ; $ count ++ ; } return $ count ; } function required_number ( $ num , $ n , $ d ) { $ flag = 0 ; for ( $ i = $ num ; $ i >= 1 ; $ i -- ) { $ power = pow ( 10 , $ i ) ; $ a = $ n % $ power ; if ( $ d > $ a ) { $ flag = 1 ; break ; } } if ( $ flag ) { $ t = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { $ t += 9 * pow ( 10 , $ j ) ; } if ( $ n % $ power == $ t ) echo $ n ; else { echo ( $ n - ( $ n % $ power ) - 1 ) ; } } else echo $ n ; } $ n = 1029 ; $ d = 102 ; $ num = dig ( $ n ) ; required_number ( $ num , $ n , $ d ) ; ? >


"Largest number with prime digits | check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code"
How can the above be solved in PHP?


"Largest of two distinct numbers without using any conditional statements or operators | Function to find the largest number ; Drivers code"
How can the above be solved in PHP?

< ? php function largestNum ( $ a , $ b ) { return ( $ a * ( boolean ) floor ( ( $ a / $ b ) ) ) + ( $ b * ( boolean ) floor ( ( $ b / $ a ) ) ) ; } $ a = 22 ; $ b = 1231 ; echo ( largestNum ( $ a , $ b ) ) ;


"Largest perfect square number in an Array | Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code"
How can the above be solved in PHP?

< ? php function checkPerfectSquare ( $ n ) { $ d = sqrt ( $ n ) ; if ( $ d * $ d == $ n ) return true ; return false ; } function largestPerfectSquareNumber ( $ a , $ n ) { $ maxi = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( checkPerfectSquare ( $ a [ $ i ] ) ) $ maxi = max ( $ a [ $ i ] , $ maxi ) ; } return $ maxi ; } $ a = array ( 16 , 20 , 25 , 2 , 3 , 10 ) ; $ n = count ( $ a ) ; echo largestPerfectSquareNumber ( $ a , $ n ) ; ? >


"Largest proper fraction with sum of numerator and denominator equal to a given number | PHP program to find the largest fraction a / b such that a + b is equal to given number and a < b . ; Calculate N / 2 ; ; Check if N is odd or even ; If N is odd answer will be ceil ( n / 2 ) - 1 and floor ( n / 2 ) + 1 ; If N is even check if N / 2 i . e a is even or odd ; If N / 2 is even apply the previous formula ; If N / 2 is odd answer will be ceil ( N / 2 ) - 2 and floor ( N / 2 ) + 2 ; driver function"
How can the above be solved in PHP?


"Largest rectangle that can be inscribed in a semicircle | Function to find the area of the biggest rectangle ; the radius cannot be negative ; area of the rectangle ; Driver code"
How can the above be solved in PHP?

 < ? php function rectanglearea ( $ r ) { if ( $ r < 0 ) return -1 ; $ a = $ r * $ r ; return $ a ; } $ r = 5 ; echo rectanglearea ( $ r ) . "
 " ; ? >


"Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube | Function to find the biggest right circular cone ; side cannot be negative ; radius of right circular cone ; height of right circular cone ; volume of right circular cone ; Driver code"
How can the above be solved in PHP?


"Largest right circular cone that can be inscribed within a sphere | Function to find the radius of the cone ; radius cannot be negative ; radius of the cone ; Function to find the height of the cone ; side cannot be negative ; height of the cone ; Driver code"
How can the above be solved in PHP?

< ? php function coner ( $ R ) { if ( $ R < 0 ) return -1 ; $ r = ( 2 * sqrt ( 2 ) * $ R ) / 3 ; return $ r ; } function coneh ( $ R ) { if ( $ R < 0 ) return -1 ; $ h = ( 4 * $ R ) / 3 ; return $ h ; } $ R = 10 ; echo ( " r ▁ = ▁ " ) ; echo coner ( $ R ) ; echo ( " , ▁ " ) ; echo ( " h ▁ = ▁ " ) ; echo ( coneh ( $ R ) ) ; ? >


"Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube | Function to find the biggest right circular cylinder ; side cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code"
How can the above be solved in PHP?


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


"Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum | Function to find the biggest sphere ; the radii and height cannot be negative ; radius of the sphere ; volume of the sphere ; Driver code"
How can the above be solved in PHP?

 < ? php function sph ( $ r , $ R , $ h ) { if ( $ r < 0 && $ R < 0 && $ h < 0 ) return -1 ; $ x = $ r ; $ V = ( 4 * 3.14 * pow ( $ r , 3 ) ) / 3 ; return $ V ; } $ r = 5 ; $ R = 8 ; $ h = 11 ; echo sph ( $ r , $ R , $ h ) ; #This  Code is contributed by ajit..
? >


"Largest sphere that can be inscribed inside a cube | Function to find the radius of the sphere ; side cannot be negative ; radius of the sphere ; Driver code"
How can the above be solved in PHP?

< ? php function sphere ( $ a ) { if ( $ a < 0 ) return -1 ; $ r = ( $ a / 2 ) ; return $ r ; } $ a = 5 ; echo sphere ( $ a ) ; ? >


"Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | Function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; Driver code"
How can the above be solved in PHP?

< ? php function sphereSide ( $ h , $ r ) { if ( $ h < 0 && $ r < 0 ) return -1 ; $ R = ( ( $ h * $ r * sqrt ( 2 ) ) / ( $ h + sqrt ( 2 ) * $ r ) ) / 2 ; return $ R ; } $ h = 5 ; $ r = 6 ; echo ( sphereSide ( $ h , $ r ) ) ; ? >


"Largest square that can be inscribed in a semicircle | Function to find the area of the square ; the radius cannot be negative ; area of the square ; Driver code"
How can the above be solved in PHP?

< ? php function squarearea ( $ r ) { if ( $ r < 0 ) return -1 ; $ a = 4 * ( pow ( $ r , 2 ) / 5 ) ; return $ a ; } $ r = 5 ; echo squarearea ( $ r ) ; ? >


"Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle | Function to find the side of the square ; Side cannot be negative ; side of the square ; Driver code"
How can the above be solved in PHP?

< ? php function squareSide ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = 0.423 * $ a ; return $ x ; } $ a = 8 ; echo squareSide ( $ a ) ; ? >


"Largest sub | Function to return the size of the required sub - set ; Sort the array ; Set to store the contents of the required sub - set ; Insert the elements satisfying the conditions ; Return the size of the set ; Driver code"
How can the above be solved in PHP?

< ? php function sizeSubSet ( $ a , $ k , $ n ) { sort ( $ a ) ; $ s = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % $ k != 0 or ! in_array ( floor ( $ a [ $ i ] / $ k ) , $ s ) ) array_push ( $ s , $ a [ $ i ] ) ; } return sizeof ( $ s ) ; } $ a = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = sizeof ( $ a ) ; $ k = 2 ; echo sizeSubSet ( $ a , $ k , $ n ) ; ? >


"Largest sub | function to return the length of the largest sub - array of an array every element of whose is a perfect square ; if both a and b are equal then arr [ i ] is a perfect square ; Driver code"
How can the above be solved in PHP?

< ? php function contiguousPerfectSquare ( $ arr , $ n ) { $ current_length = 0 ; $ max_length = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ b = ( float ) sqrt ( $ arr [ $ i ] ) ; $ a = ( int ) $ b ; if ( $ a == $ b ) $ current_length = $ current_length + 1 ; else $ current_length = 0 ; $ max_length = max ( $ max_length , $ current_length ) ; } return $ max_length ; } $ arr = array ( 9 , 75 , 4 , 64 , 121 , 25 ) ; $ n = sizeof ( $ arr ) ; echo contiguousPerfectSquare ( $ arr , $ n ) ; ? >


"Largest subsequence having GCD greater than 1 | Efficient PHP program to find length of the largest subsequence with GCD greater than 1. ; prime [ ] for storing smallest prime divisor of element count [ ] for storing the number of times a particular divisor occurs in a subsequence ; Simple sieve to find smallest prime factors of numbers smaller than MAX ; Prime number will have same divisor ; Returns length of the largest subsequence with GCD more than 1. ; Fetch total unique prime divisor of element ; Increment count [ ] of Every unique divisor we get till now ; Find maximum frequency of divisor ; Pre - compute smallest divisor of all numbers"
How can the above be solved in PHP?

< ? php $ MAX = 10001 ; $ prime = array_fill ( 0 , $ MAX , 0 ) ; $ countdiv = array_fill ( 0 , $ MAX , 0 ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; for ( $ i = 2 ; $ i * $ i <= $ MAX ; ++ $ i ) { if ( $ prime [ $ i ] == 0 ) for ( $ j = $ i * 2 ; $ j <= $ MAX ; $ j += $ i ) $ prime [ $ j ] = $ i ; } for ( $ i = 1 ; $ i < $ MAX ; ++ $ i ) if ( $ prime [ $ i ] == 0 ) $ prime [ $ i ] = $ i ; } function largestGCDSubsequence ( $ arr , $ n ) { global $ countdiv , $ prime ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ element = $ arr [ $ i ] ; while ( $ element > 1 ) { $ div = $ prime [ $ element ] ; ++ $ countdiv [ $ div ] ; $ ans = max ( $ ans , $ countdiv [ $ div ] ) ; while ( $ element % $ div == 0 ) $ element = ( int ) ( $ element / $ div ) ; } } return $ ans ; } SieveOfEratosthenes ( ) ; $ arr = array ( 10 , 15 , 7 , 25 , 9 , 35 ) ; $ size = count ( $ arr ) ; echo largestGCDSubsequence ( $ arr , $ size ) ; ? >


"Largest subsequence having GCD greater than 1 | Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr [ ] ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver code"
How can the above be solved in PHP?

< ? php function largestGCDSubsequence ( $ arr , $ n ) { $ ans = 0 ; $ maxele = max ( $ arr ) ; for ( $ i = 2 ; $ i <= $ maxele ; ++ $ i ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; ++ $ j ) { if ( $ arr [ $ j ] % $ i == 0 ) ++ $ count ; } $ ans = max ( $ ans , $ count ) ; } return $ ans ; } $ arr = array ( 3 , 6 , 2 , 5 , 4 ) ; $ size = count ( $ arr ) ; echo largestGCDSubsequence ( $ arr , $ size ) ; ? >


"Largest substring with same Characters | Function to find largest sub string with same characters ; Traverse the string ; If character is same as previous increment temp value ; Return the required answer ; Driver code ; Function call"
How can the above be solved in PHP?

< ? php function Substring ( $ s ) { $ ans = 1 ; $ temp = 1 ; for ( $ i = 1 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i - 1 ] ) { ++ $ temp ; } else { $ ans = max ( $ ans , $ temp ) ; $ temp = 1 ; } } $ ans = max ( $ ans , $ temp ) ; return $ ans ; } $ s = " abcdddddeff " ; echo Substring ( $ s ) ; ? >


"Largest sum Zigzag sequence in a matrix | PHP program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function largestZigZagSumRec ( $ mat , $ i , $ j , $ n ) { if ( $ i == $ n - 1 ) return $ mat [ $ i ] [ $ j ] ; $ zzs = 0 ; for ( $ k = 0 ; $ k < $ n ; $ k ++ ) if ( $ k != $ j ) $ zzs = max ( $ zzs , largestZigZagSumRec ( $ mat , $ i + 1 , $ k , $ n ) ) ; return $ zzs + $ mat [ $ i ] [ $ j ] ; } function largestZigZag ( $ mat , $ n ) { $ res = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ res = max ( $ res , largestZigZagSumRec ( $ mat , 0 , $ j , $ n ) ) ; return $ res ; } $ n = 3 ; $ mat = array ( array ( 4 , 2 , 1 ) , array ( 3 , 9 , 6 ) , array ( 11 , 3 , 15 ) ) ; echo " Largest ▁ zigzag ▁ sum : ▁ " , largestZigZag ( $ mat , $ n ) ; ? >


"Largest sum subarray with at | Returns maximum sum of a subarray with at - least k elements . ; maxSum [ i ] is going to store maximum sum till index i such that a [ i ] is part of the sum . ; We use Kadane 's algorithm  to fill maxSum[] ; Sum of first k elements ; Use the concept of sliding window ; Compute sum of k elements ending with a [ i ] . ; Update result if required ; Include maximum sum till [ i - k ] also if it increases overall max . ; Driver code"
How can the above be solved in PHP?

< ? php function maxSumWithK ( $ a , $ n , $ k ) { $ maxSum [ 0 ] = $ a [ 0 ] ; $ curr_max = $ a [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr_max = max ( $ a [ $ i ] , $ curr_max + $ a [ $ i ] ) ; $ maxSum [ $ i ] = $ curr_max ; } $ sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ sum += $ a [ $ i ] ; $ result = $ sum ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + $ a [ $ i ] - $ a [ $ i - $ k ] ; $ result = max ( $ result , $ sum ) ; $ result = max ( $ result , $ sum + $ maxSum [ $ i - $ k ] ) ; } return $ result ; } $ a = array ( 1 , 2 , 3 , -10 , -3 ) ; $ k = 4 ; $ n = sizeof ( $ a ) ; echo maxSumWithK ( $ a , $ n , $ k ) ; ? >


"Largest trapezoid that can be inscribed in a semicircle | Function to find the area of the biggest trapezoid ; the radius cannot be negative ; area of the trapezoid ; Driver code"
How can the above be solved in PHP?

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


"Largest triangle that can be inscribed in a semicircle | Function to find the area of the triangle ; the radius cannot be negative ; area of the triangle ; Driver code"
How can the above be solved in PHP?

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


"Largest triangle that can be inscribed in an ellipse | Function to find the area of the triangle ; a and b cannot be negative ; area of the triangle ; Driver code"
How can the above be solved in PHP?

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


"Last digit of Product of two Large or Small numbers ( a * b ) | Function to print last digit of product a * b ; Driver code"
How can the above be solved in PHP?


"Last duplicate element in a sorted array | PHP program to print last duplicate element and its index in a sorted array ; if array is null or size is less than equal to 0 return ; compare elements and return last duplicate and its index ; If we reach here , then no duplicate found . ; Driver Code"
How can the above be solved in PHP?

 < ? php function dupLastIndex ( $ arr , $ n ) { if ( $ arr == null or $ n <= 0 ) return ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ arr [ $ i ] == $ arr [ $ i - 1 ] ) { echo " Last ▁ index : " , $ i , "
 " ; echo " Last ▁ duplicate ▁ item : " , $ arr [ $ i ] ; return ; } } echo " no ▁ duplicate ▁ found " ; } $ arr = array ( 1 , 5 , 5 , 6 , 6 , 7 , 9 ) ; $ n = count ( $ arr ) ; dupLastIndex ( $ arr , $ n ) ; ? >


"Last non | Initialize values of last non - zero digit of numbers from 0 to 9 ; Check whether tens ( or second last ) digit is odd or even If n = 375 , So n / 10 = 37 and ( n / 10 ) % 10 = 7 Applying formula for even and odd cases . ; Driver code"
How can the above be solved in PHP?

< ? php $ dig = array ( 1 , 1 , 2 , 6 , 4 , 2 , 2 , 4 , 2 , 8 ) ; function lastNon0Digit ( $ n ) { global $ dig ; if ( $ n < 10 ) return $ dig [ $ n ] ; if ( ( ( $ n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( $ n / 5 ) * $ dig [ $ n % 10 ] ) % 10 ; else return ( 4 * lastNon0Digit ( $ n / 5 ) * $ dig [ $ n % 10 ] ) % 10 ; } $ n = 14 ; echo ( lastNon0Digit ( $ n ) ) ; ? >


"Latin alphabet cipher | function for calculating the encryption ; Driver Code"
How can the above be solved in PHP?

 < ? php function cipher ( $ str ) { if ( ! ctype_alpha ( $ str ) ) { printf ( " Enter ▁ only ▁ " + " alphabets ▁ and ▁ space
 " ) ; return ; } printf ( " Encrypted ▁ Code ▁ using ▁ " ) ; printf ( " Latin ▁ Alphabet
 " ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) echo ( ord ( $ str [ $ i ] ) - 65 + 1 ) . " ▁ " ; else if ( $ str [ $ i ] >= ' a ' && $ str [ $ i ] <= ' z ' ) echo ( ord ( $ str [ $ i ] ) - 97 + 1 ) . " ▁ " ; } echo "
 " ; } $ str = " geeksforgeeks " ; cipher ( $ str ) ; ? >


"Leaders in an array | PHP Function to print leaders in an array ; Rightmost element is always leader ; Driver Code"
How can the above be solved in PHP?

< ? php function printLeaders ( & $ arr , $ size ) { $ max_from_right = $ arr [ $ size - 1 ] ; echo ( $ max_from_right ) ; echo ( " ▁ " ) ; for ( $ i = $ size - 2 ; $ i >= 0 ; $ i -- ) { if ( $ max_from_right < $ arr [ $ i ] ) { $ max_from_right = $ arr [ $ i ] ; echo ( $ max_from_right ) ; echo ( " ▁ " ) ; } } } $ arr = array ( 16 , 17 , 4 , 3 , 5 , 2 ) ; $ n = sizeof ( $ arr ) ; printLeaders ( $ arr , $ n ) ; ? >


"Leaders in an array | PHP Function to print leaders in an array ; the loop didn 't break ; Driver Code"
How can the above be solved in PHP?

< ? php function printLeaders ( $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ size ; $ j ++ ) { if ( $ arr [ $ i ] <= $ arr [ $ j ] ) break ; } if ( $ j == $ size ) echo ( $ arr [ $ i ] . " ▁ " ) ; } } $ arr = array ( 16 , 17 , 4 , 3 , 5 , 2 ) ; $ n = sizeof ( $ arr ) ; printLeaders ( $ arr , $ n ) ; ? >


"Leaf nodes from Preorder of a Binary Search Tree ( Using Recursion ) | Print the leaf node from the given preorder of BST . ; Driver code"
How can the above be solved in PHP?

< ? php function isLeaf ( $ pre , & $ i , $ n , $ min , $ max ) { if ( $ i >= $ n ) return false ; if ( $ pre [ $ i ] > $ min && $ pre [ $ i ] < $ max ) { $ i ++ ; $ left = isLeaf ( $ pre , $ i , $ n , $ min , $ pre [ $ i - 1 ] ) ; $ right = isLeaf ( $ pre , $ i , $ n , $ pre [ $ i - 1 ] , $ max ) ; if ( ! $ left && ! $ right ) echo $ pre [ $ i - 1 ] , " ▁ " ; return true ; } return false ; } function printLeaves ( $ preorder , $ n ) { $ i = 0 ; isLeaf ( $ preorder , $ i , $ n , PHP_INT_MIN , PHP_INT_MAX ) ; } $ preorder = array ( 890 , 325 , 290 , 530 , 965 ) ; $ n = sizeof ( $ preorder ) ; printLeaves ( $ preorder , $ n ) ; ? >


"Least frequent element in an array | PHP program to find the least frequent element in an array . ; Sort the array ; find the min frequency using linear traversal ; If last element is least frequent ; Driver Code"
How can the above be solved in PHP?

< ? php function leastFrequent ( $ arr , $ n ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ min_count = $ n + 1 ; $ res = -1 ; $ curr_count = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ arr [ $ i - 1 ] ) $ curr_count ++ ; else { if ( $ curr_count < $ min_count ) { $ min_count = $ curr_count ; $ res = $ arr [ $ i - 1 ] ; } $ curr_count = 1 ; } } if ( $ curr_count < $ min_count ) { $ min_count = $ curr_count ; $ res = $ arr [ $ n - 1 ] ; } return $ res ; } { $ arr = array ( 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo leastFrequent ( $ arr , $ n ) ; return 0 ; } ? >


"Leftmost and rightmost indices of the maximum and the minimum element of an array | Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; Driver code ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array"
How can the above be solved in PHP?

 < ? php function getRightMin ( $ arr , $ n ) { $ min = $ arr [ 0 ] ; $ rightMin = 0 ; $ i = 1 ; while ( $ i < $ n ) { if ( $ arr [ $ i ] == $ min ) $ rightMin = $ i ; $ i *= 2 ; } $ i = $ rightMin + 1 ; while ( $ i < $ n && $ arr [ $ i ] == $ min ) { $ rightMin = $ i ; $ i ++ ; } return $ rightMin ; } function getLeftMax ( $ arr , $ n ) { $ max = $ arr [ $ n - 1 ] ; $ leftMax = $ n - 1 ; $ i = $ n - 2 ; while ( $ i > 0 ) { if ( $ arr [ $ i ] == $ max ) $ leftMax = $ i ; $ i /= 2 ; } $ i = $ leftMax - 1 ; while ( $ i >= 0 && $ arr [ $ i ] == $ max ) { $ leftMax = $ i ; $ i -- ; } return $ leftMax ; } $ arr = array ( 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ left ▁ : ▁ " , 0 , "
 " ; echo " Minimum ▁ right ▁ : ▁ " , getRightMin ( $ arr , $ n ) , "
 " ; echo " Maximum ▁ left ▁ : ▁ " , getLeftMax ( $ arr , $ n ) , "
 " ; echo " Maximum ▁ right ▁ : ▁ " , ( $ n - 1 ) , "
 " ; ? >


"Leftmost and rightmost indices of the maximum and the minimum element of an array | PHP implementation of the approach ; If found new minimum ; If arr [ i ] = min then rightmost index for min will change ; If found new maximum ; If arr [ i ] = max then rightmost index for max will change ; Driver code"
How can the above be solved in PHP?

 < ? php function findIndices ( $ arr , $ n ) { $ leftMin = 0 ; $ rightMin = 0 ; $ leftMax = 0 ; $ rightMax = 0 ; $ min = $ arr [ 0 ] ; $ max = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < $ min ) { $ leftMin = $ rightMin = $ i ; $ min = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] == $ min ) $ rightMin = $ i ; if ( $ arr [ $ i ] > $ max ) { $ leftMax = $ rightMax = $ i ; $ max = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] == $ max ) $ rightMax = $ i ; } echo " Minimum ▁ left ▁ : ▁ " , $ leftMin , "
 " ; echo " Minimum ▁ right ▁ : ▁ " , $ rightMin , "
 " ; echo " Maximum ▁ left ▁ : ▁ " , $ leftMax , "
 " ; echo " Maximum ▁ right ▁ : ▁ " , $ rightMax , "
 " ; } $ arr = array ( 2 , 1 , 1 , 2 , 1 , 5 , 6 , 5 ) ; $ n = sizeof ( $ arr ) ; findIndices ( $ arr , $ n ) ; ? >


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


"Length of Longest Balanced Subsequence | PHP program to find length of the longest balanced subsequence ; Considering all balanced substrings of length 2 ; Considering all other substrings ; Driver Code"
How can the above be solved in PHP?


"Length of longest balanced parentheses prefix | Return the length of longest balanced parentheses prefix . ; Traversing the string . ; If open bracket add 1 to sum . ; If closed bracket subtract 1 from sum ; if first bracket is closing bracket then this condition would help ; If sum is 0 , store the index value . ; Driver Code"
How can the above be solved in PHP?


"Length of longest common subsequence containing vowels | function to check whether ' ch ' is a vowel or not ; function to find the length of longest common subsequence which contains all vowel characters ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] which contains all vowel characters ; Driver Code"
How can the above be solved in PHP?


"Length of longest consecutive ones by at most one swap in a Binary String | Function to calculate the length of the longest consecutive 1 's ; To count all 1 's in the string ; To store cumulative 1 's  $left[$n]; $right[$n]; ; Counting cumulative 1 's from left ; If 0 then start new cumulative one from that i ; perform step 3 of the approach ; step 3 ; string"
How can the above be solved in PHP?


"Length of rope tied around three equal circles touching each other | PHP program to find the length of rope ; Function to find the length of rope ; Driver code"
How can the above be solved in PHP?

< ? php $ PI = 3.14159265 ; function length_rope ( $ r ) { global $ PI ; return ( ( 2 * $ PI * $ r ) + 6 * $ r ) ; } $ r = 7 ; echo ( length_rope ( $ r ) ) ; ? >


"Length of the Diagonal of the Octagon | Function to find the diagonal of the octagon ; side cannot be negative ; diagonal of the octagon ; Driver code"
How can the above be solved in PHP?

< ? php function octadiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; return $ a * sqrt ( 4 + ( 2 * sqrt ( 2 ) ) ) ; } $ a = 4 ; echo octadiagonal ( $ a ) ; ? >


"Length of the direct common tangent between two externally touching circles | Function to find the length of the direct common tangent ; Driver code"
How can the above be solved in PHP?

< ? php function lengtang ( $ r1 , $ r2 ) { echo " The ▁ length ▁ of ▁ the ▁ " , " direct ▁ common ▁ tangent ▁ is ▁ " , 2 * sqrt ( $ r1 * $ r2 ) ; } $ r1 = 5 ; $ r2 = 9 ; lengtang ( $ r1 , $ r2 ) ; ? >


"Length of the largest subarray with contiguous elements | Set 1 | Utility functions to find minimum and maximum of two elements ; Returns length of the longest contiguous subarray ; Initialize result ; Initialize min and max for all subarrays starting with i ; Consider all subarrays starting with i and ending with j ; Update min and max in this subarray if needed ; If current subarray has all contiguous elements ; Return result ; Driver Code"
How can the above be solved in PHP?

< ? php function mins ( $ x , $ y ) { if ( $ x < $ y ) return $ x ; else return $ y ; } function maxi ( $ a , $ b ) { if ( $ a > $ b ) return $ a ; else return $ b ; } function findLength ( & $ arr , $ n ) { $ max_len = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ mn = $ arr [ $ i ] ; $ mx = $ arr [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ mn = mins ( $ mn , $ arr [ $ j ] ) ; $ mx = maxi ( $ mx , $ arr [ $ j ] ) ; if ( ( $ mx - $ mn ) == $ j - $ i ) $ max_len = maxi ( $ max_len , $ mx - $ mn + 1 ) ; } } return $ max_len ; } $ arr = array ( 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 ) ; $ n = sizeof ( $ arr ) ; echo ( " Length ▁ of ▁ the ▁ longest ▁ contiguous " . " ▁ subarray ▁ is ▁ " ) ; echo ( findLength ( $ arr , $ n ) ) ; ? >


"Length of the longest increasing subsequence such that no two adjacent elements are coprime | PHP program to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; Function to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; To store dp and d value ; To store required answer ; For all elements in the array ; Initially answer is one ; For all it 's divisors ; Update the dp value ; Update the divisor value ; Check for required answer ; Update divisor of a [ i ] ; Return required answer ; Driver code"
How can the above be solved in PHP?


"Length of the longest substring that do not contain any palindrome | Function to find the length of the longest substring ; initializing the variables ; checking palindrome of size 2 example : aa ; checking palindrome of size 3 example : aba ; else incrementing length of substring ; $max1 = max ( $max1 , $len + 1 ) ; finding maximum ; if there exits single character then it is always palindrome ; Driver Code"
How can the above be solved in PHP?

< ? php function lenoflongestnonpalindrome ( $ s ) { $ max1 = 1 ; $ len = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) - 1 ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ len = 0 ; else if ( $ s [ $ i + 1 ] == $ s [ $ i - 1 ] && $ i > 0 ) $ len = 1 ; $ len ++ ; } if ( $ max1 == 1 ) return 0 ; else return $ max1 ; } $ s = " synapse " ; echo lenoflongestnonpalindrome ( $ s ) , " " ; ? >


"Length of the longest substring with consecutive characters | Function to return the ending index for the largest valid sub - string / starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - string of consecutive characters from str ; Valid sub - string exists from index i to end ; Update the length ; Driver code"
How can the above be solved in PHP?

< ? php function getEndingIndex ( $ str , $ n , $ i ) { $ i ++ ; while ( $ i < $ n ) { $ curr = $ str [ $ i ] ; $ prev = $ str [ $ i - 1 ] ; if ( ( $ curr == ' a ' && $ prev == ' z ' ) || ( ord ( $ curr ) - ord ( $ prev ) == 1 ) ) $ i ++ ; else break ; } return $ i - 1 ; } function largestSubStr ( $ str , $ n ) { $ len = 0 ; $ i = 0 ; while ( $ i < $ n ) { $ end = getEndingIndex ( $ str , $ n , $ i ) ; $ len = max ( $ end - $ i + 1 , $ len ) ; $ i = $ end + 1 ; } return $ len ; } $ str = " abcabcdefabc " ; $ n = strlen ( $ str ) ; echo largestSubStr ( $ str , $ n ) ; ? >


"Length of the longest substring with no consecutive same letters | Function to return the length of the required sub - string ; Get the length of the string ; Iterate in the string ; Check for not consecutive ; If cnt greater than maxi ; Re - initialize ; Check after iteration is complete ; Driver code"
How can the above be solved in PHP?

< ? php function longestSubstring ( $ s ) { $ cnt = 1 ; $ maxi = 1 ; $ n = strlen ( $ s ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] != $ s [ $ i - 1 ] ) $ cnt ++ ; else { $ maxi = max ( $ cnt , $ maxi ) ; $ cnt = 1 ; } } $ maxi = max ( $ cnt , $ maxi ) ; return $ maxi ; } $ s = " ccccdeededff " ; echo longestSubstring ( $ s ) ; ? >


"Length of the normal from origin on a straight line whose intercepts are given | Function to find the normal of the straight line ; Length of the normal ; Driver code"
How can the above be solved in PHP?

< ? php function normal ( $ m , $ n ) { $ N = ( abs ( $ m ) * abs ( $ n ) ) / sqrt ( ( abs ( $ m ) * abs ( $ m ) ) + ( abs ( $ n ) * abs ( $ n ) ) ) ; return $ N ; } $ m = -5 ; $ n = 3 ; echo normal ( $ m , $ n ) ; ? >


"Length of the smallest number which is divisible by K and formed by using 1 's only | Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code"
How can the above be solved in PHP?

< ? php function numLen ( $ K ) { if ( $ K % 2 == 0 $ K % 5 == 0 ) return -1 ; $ number = 0 ; $ len = 1 ; for ( $ len = 1 ; $ len <= $ K ; $ len ++ ) { $ number = $ number * 10 + 1 ; if ( ( $ number % $ K == 0 ) ) return $ len ; } return -1 ; } $ K = 7 ; echo numLen ( $ K ) ; ? >


"Length of the smallest number which is divisible by K and formed by using 1 's only | Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Instead of generating all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's  Take remainder with K ; If number is divisible by k then remainder will be 0 ; Driver code"
How can the above be solved in PHP?

< ? php function numLen ( $ K ) { if ( $ K % 2 == 0 $ K % 5 == 0 ) return -1 ; $ number = 0 ; $ len = 1 ; for ( $ len = 1 ; $ len <= $ K ; $ len ++ ) { $ number = ( $ number * 10 + 1 ) % $ K ; if ( $ number == 0 ) return $ len ; } return -1 ; } $ K = 7 ; echo numLen ( $ K ) ; ? >


"Length of the smallest sub | PHP program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's count with 0 ; Increase the count in array if a character is found ; size of given string ; Find maximum distinct characters in any string ; result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Input String"
How can the above be solved in PHP?

< ? php $ NO_OF_CHARS = 256 ; function max_distinct_char ( $ str , $ n ) { global $ NO_OF_CHARS ; $ count = array_fill ( 0 , $ NO_OF_CHARS , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) ] ++ ; $ max_distinct = 0 ; for ( $ i = 0 ; $ i < $ NO_OF_CHARS ; $ i ++ ) if ( $ count [ $ i ] != 0 ) $ max_distinct ++ ; return $ max_distinct ; } function smallesteSubstr_maxDistictChar ( $ str ) { $ n = strlen ( $ str ) ; $ max_distinct = max_distinct_char ( $ str , $ n ) ; $ minl = $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ subs = substr ( $ str , $ i , $ j ) ; $ subs_lenght = strlen ( $ subs ) ; $ sub_distinct_char = max_distinct_char ( $ subs , $ subs_lenght ) ; if ( $ subs_lenght < $ minl && $ max_distinct == $ sub_distinct_char ) { $ minl = $ subs_lenght ; } } } return $ minl ; } $ str = " AABBBCBB " ; $ len = smallesteSubstr_maxDistictChar ( $ str ) ; echo " ▁ The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " . " ▁ consisting ▁ of ▁ maximum ▁ distinct ▁ characters ▁ : ▁ " . $ len ; ? >


"Length of the transverse common tangent between the two non intersecting circles | Function to find the length of the transverse common tangent ; Driver code"
How can the above be solved in PHP?

< ? php function lengthOfTangent ( $ r1 , $ r2 , $ d ) { echo " The ▁ length ▁ of ▁ the ▁ transverse ▁ common ▁ tangent ▁ is ▁ " , sqrt ( pow ( $ d , 2 ) - pow ( ( $ r1 + $ r2 ) , 2 ) ) ; } $ r1 = 4 ; $ r2 = 6 ; $ d = 12 ; lengthOfTangent ( $ r1 , $ r2 , $ d ) ; ? >


"Leonardo Number | A simple recursive program to find n - th leonardo number . ; Driver code"
How can the above be solved in PHP?