codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to represent a number as sum of k fibonacci numbers | To store fibonacci numbers 42 second number in fibonacci series largest possible integer ; Function to generate fibonacci series ; Recursive function to return the number of ways ; base condition ; for recursive function call ; Driver code
< ? php $ fib = array_fill ( 0 , 43 , 0 ) ; function fibonacci ( ) { global $ fib ; $ fib [ 0 ] = 1 ; $ fib [ 1 ] = 2 ; for ( $ i = 2 ; $ i < 43 ; $ i ++ ) $ fib [ $ i ] = $ fib [ $ i - 1 ] + $ fib [ $ i - 2 ] ; } function rec ( $ x , $ y , $ last ) { global $ fib ; if ( $ y == 0 ) { if ( $ x == 0 ) return 1 ; return 0 ; } $ sum = 0 ; for ( $ i = $ last ; $ i >= 0 and $ fib [ $ i ] * $ y >= $ x ; $ i -- ) { if ( $ fib [ $ i ] > $ x ) continue ; $ sum += rec ( $ x - $ fib [ $ i ] , $ y - 1 , $ i ) ; } return $ sum ; } fibonacci ( ) ; $ n = 13 ; $ k = 3 ; echo " Possible ▁ ways ▁ are : ▁ " . rec ( $ n , $ k , 42 ) ; ? >
Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code
< ? php function minimumCost ( & $ cost , $ n ) { if ( $ n == 1 ) return $ cost [ 0 ] ; $ dp [ 0 ] = $ cost [ 0 ] ; $ dp [ 1 ] = $ cost [ 1 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ dp [ $ i ] = min ( $ dp [ $ i - 1 ] , $ dp [ $ i - 2 ] ) + $ cost [ $ i ] ; } return min ( $ dp [ $ n - 2 ] , $ dp [ $ n - 1 ] ) ; } $ a = array ( 16 , 19 , 10 , 12 , 18 ) ; $ n = sizeof ( $ a ) ; echo ( minimumCost ( $ a , $ n ) ) ; ? >
Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code
< ? php function minimumCost ( & $ cost , $ n ) { $ dp1 = 0 ; $ dp2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ dp0 = $ cost [ $ i ] + min ( $ dp1 , $ dp2 ) ; $ dp2 = $ dp1 ; $ dp1 = $ dp0 ; } return min ( $ dp1 , $ dp2 ) ; } $ a = array ( 2 , 5 , 3 , 1 , 7 , 3 , 4 ) ; $ n = sizeof ( $ a ) ; echo ( minimumCost ( $ a , $ n ) ) ; ? >
Edit distance and LCS ( Longest Common Subsequence ) | PHP program to find Edit Distance ( when only two operations are allowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver Code
< ? php function editDistanceWith2Ops ( $ X , $ Y ) { $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; $ L [ $ m + 1 ] [ $ n + 1 ] ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) $ L [ $ i ] [ $ j ] = 0 ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) $ L [ $ i ] [ $ j ] = $ L [ $ i - 1 ] [ $ j - 1 ] + 1 ; else $ L [ $ i ] [ $ j ] = max ( $ L [ $ i - 1 ] [ $ j ] , $ L [ $ i ] [ $ j - 1 ] ) ; } } $ lcs = $ L [ $ m ] [ $ n ] ; return ( $ m - $ lcs ) + ( $ n - $ lcs ) ; } $ X = " abc " ; $ Y = " acd " ; echo editDistanceWith2Ops ( $ X , $ Y ) ; ? >
Longest Common Subsequence | DP using Memoization | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code ; Find the length of string
< ? php function lcs ( $ X , $ Y , $ m , $ n ) { if ( $ m == 0 $ n == 0 ) return 0 ; if ( $ X [ $ m - 1 ] == $ Y [ $ n - 1 ] ) return 1 + lcs ( $ X , $ Y , $ m - 1 , $ n - 1 ) ; else return max ( lcs ( $ X , $ Y , $ m , $ n - 1 ) , lcs ( $ X , $ Y , $ m - 1 , $ n ) ) ; } $ X = " AGGTAB " ; $ Y = " GXTXAYB " ; $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; echo " Length ▁ of ▁ LCS : ▁ " . lcs ( $ X , $ Y , $ m , $ n ) ; ? >
Number of different cyclic paths of length N in a tetrahedron | Function to count the number of steps in a tetrahedron ; initially coming to B is B -> B ; cannot reach A , D or C ; iterate for all steps ; recurrence relation ; memoize previous values ; returns steps ; Driver Code
< ? php function countPaths ( $ n ) { $ zB = 1 ; $ zADC = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ nzB = $ zADC * 3 ; $ nzADC = ( $ zADC * 2 + $ zB ) ; $ zB = $ nzB ; $ zADC = $ nzADC ; } return $ zB ; } $ n = 3 ; echo countPaths ( $ n ) ; ? >
Sequence Alignment problem | function to find out the minimum penalty ; table for storing optimal substructure answers ; initialising the table ; calculating the minimum penalty ; Reconstructing the solution $l = $n + $m ; maximum possible length ; Final answers for the respective strings $xans [ $l + 1 ] ; $yans [ $l + 1 ] ; ; Since we have assumed the answer to be n + m long , we need to remove the extra gaps in the starting id represents the index from which the arrays xans , yans are useful ; Printing the final answer ; input strings ; initialising penalties of different types ; calling the function to calculate the result
< ? php function getMinimumPenalty ( $ x , $ y , $ pxy , $ pgap ) { $ dp [ $ n + $ m + 1 ] [ $ n + $ m + 1 ] = array ( 0 ) ; for ( $ i = 0 ; $ i <= ( $ n + $ m ) ; $ i ++ ) { $ dp [ $ i ] [ 0 ] = $ i * $ pgap ; $ dp [ 0 ] [ $ i ] = $ i * $ pgap ; } for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { if ( $ x [ $ i - 1 ] == $ y [ $ j - 1 ] ) { $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j - 1 ] ; } else { $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i - 1 ] [ $ j - 1 ] + $ pxy , $ dp [ $ i - 1 ] [ $ j ] + $ pgap , $ dp [ $ i ] [ $ j - 1 ] + $ pgap ) ; } } } $ i = $ m ; $ j = $ n ; $ xpos = $ l ; $ ypos = $ l ; while ( ! ( $ i == 0 $ j == 0 ) ) { if ( $ x [ $ i - 1 ] == $ y [ $ j - 1 ] ) { $ xans [ $ xpos -- ] = $ x [ $ i - 1 ] ; $ yans [ $ ypos -- ] = $ y [ $ j - 1 ] ; $ i -- ; $ j -- ; } else if ( $ dp [ $ i - 1 ] [ $ j - 1 ] + $ pxy == $ dp [ $ i ] [ $ j ] ) { $ xans [ $ xpos -- ] = $ x [ $ i - 1 ] ; $ yans [ $ ypos -- ] = $ y [ $ j - 1 ] ; $ i -- ; $ j -- ; } else if ( $ dp [ $ i - 1 ] [ $ j ] + $ pgap == $ dp [ $ i ] [ $ j ] ) { $ xans [ $ xpos -- ] = $ x [ $ i - 1 ] ; $ yans [ $ ypos -- ] = ' _ ' ; $ i -- ; } else if ( $ dp [ $ i ] [ $ j - 1 ] + $ pgap == $ dp [ $ i ] [ $ j ] ) { $ xans [ $ xpos -- ] = ' _ ' ; $ yans [ $ ypos -- ] = $ y [ $ j - 1 ] ; $ j -- ; } } while ( $ xpos > 0 ) { if ( $ i > 0 ) $ xans [ $ xpos -- ] = $ x [ -- $ i ] ; else $ xans [ $ xpos -- ] = ' _ ' ; } while ( $ ypos > 0 ) { if ( $ j > 0 ) $ yans [ $ ypos -- ] = $ y [ -- $ j ] ; else $ yans [ $ ypos -- ] = ' _ ' ; } $ id = 1 ; for ( $ i = $ l ; $ i >= 1 ; $ i -- ) { if ( $ yans [ $ i ] == ' _ ' && $ xans [ $ i ] == ' _ ' ) { $ id = $ i + 1 ; break ; } } echo " Minimum ▁ Penalty ▁ in ▁ " . " aligning ▁ the ▁ genes ▁ = ▁ " ; echo $ dp [ $ m ] [ $ n ] . " STRNEWLINE " ; echo " The ▁ aligned ▁ genes ▁ are ▁ : STRNEWLINE " ; for ( $ i = $ id ; $ i <= $ l ; $ i ++ ) { echo $ xans [ $ i ] ; } echo " STRNEWLINE " ; for ( $ i = $ id ; $ i <= $ l ; $ i ++ ) { echo $ yans [ $ i ] ; } return ; } $ gene1 = " AGGGCT " ; $ gene2 = " AGGCA " ; $ misMatchPenalty = 3 ; $ gapPenalty = 2 ; getMinimumPenalty ( $ gene1 , $ gene2 , $ misMatchPenalty , $ gapPenalty ) ; ? >
Longest Decreasing Subsequence | Function that returns the length of the longest decreasing subsequence ; Initialize LDS with 1 for all index The minimum LDS starting with any element is always 1 ; Compute LDS from every index in bottom up manner ; Select the maximum of all the LDS values ; returns the length of the LDS ; Driver Code
< ? php function lds ( $ arr , $ n ) { $ lds = array ( ) ; $ i ; $ j ; $ max = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ lds [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] < $ arr [ $ j ] and $ lds [ $ i ] < $ lds [ $ j ] + 1 ) { $ lds [ $ i ] = $ lds [ $ j ] + 1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max < $ lds [ $ i ] ) $ max = $ lds [ $ i ] ; return $ max ; } $ arr = array ( 15 , 27 , 14 , 38 , 63 , 55 , 46 , 65 , 85 ) ; $ n = count ( $ arr ) ; echo " Length ▁ of ▁ LDS ▁ is ▁ " , lds ( $ arr , $ n ) ; ? >
Sum of product of consecutive Binomial Coefficients | PHP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; finding the sum of product of consecutive coefficient . ; Driver Code
< ? php $ MAX = 100 ; function binomialCoeff ( $ C , $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ n ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C ; } function sumOfproduct ( $ n ) { global $ MAX ; $ sum = 0 ; $ C = array_fill ( 0 , $ MAX , 0 ) ; $ C = binomialCoeff ( $ C , $ n ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += $ C [ $ i ] * $ C [ $ i + 1 ] ; return $ sum ; } $ n = 3 ; echo sumOfproduct ( $ n ) ; ? >
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | PHP Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driver Code
< ? php $ MAX = 100 ; function binomialCoeff ( $ n , & $ C ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ n ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } } function summation ( $ n ) { global $ MAX ; $ C = array_fill ( 0 , $ MAX , 0 ) ; binomialCoeff ( $ n , $ C ) ; $ sum = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ C [ $ i ] ) ; return $ sum ; } $ n = 2 ; echo summation ( $ n ) ; ? >
Number of arrays of size N whose elements are positive integers and sum is K | Return nCr ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , ( $ k + 1 ) , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function countArray ( $ N , $ K ) { return binomialCoeff ( $ K - 1 , $ N - 1 ) ; } $ N = 2 ; $ K = 3 ; echo countArray ( $ N , $ K ) ; ? >
Maximize array elements upto given number | Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index . 1. dp [ i ] [ j ] = 1 if value j can be obtained upto index i . 2. dp [ i ] [ j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is true , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code
< ? php function findMaxVal ( $ arr , $ n , $ num , $ maxLimit ) { $ ind ; $ val ; $ dp [ $ n ] [ $ maxLimit + 1 ] = array ( ) ; for ( $ ind = 0 ; $ ind < $ n ; $ ind ++ ) { for ( $ val = 0 ; $ val <= $ maxLimit ; $ val ++ ) { if ( $ ind == 0 ) { if ( $ num - $ arr [ $ ind ] == $ val $ num + $ arr [ $ ind ] == $ val ) { $ dp [ $ ind ] [ $ val ] = 1 ; } else { $ dp [ $ ind ] [ $ val ] = 0 ; } } else { if ( $ val - $ arr [ $ ind ] >= 0 && $ val + $ arr [ $ ind ] <= $ maxLimit ) { $ dp [ $ ind ] [ $ val ] = $ dp [ $ ind - 1 ] [ $ val - $ arr [ $ ind ] ] || $ dp [ $ ind - 1 ] [ $ val + $ arr [ $ ind ] ] ; } else if ( $ val - $ arr [ $ ind ] >= 0 ) { $ dp [ $ ind ] [ $ val ] = $ dp [ $ ind - 1 ] [ $ val - $ arr [ $ ind ] ] ; } else if ( $ val + $ arr [ $ ind ] <= $ maxLimit ) { $ dp [ $ ind ] [ $ val ] = $ dp [ $ ind - 1 ] [ $ val + $ arr [ $ ind ] ] ; } else { $ dp [ $ ind ] [ $ val ] = 0 ; } } } } for ( $ val = $ maxLimit ; $ val >= 0 ; $ val -- ) { if ( $ dp [ $ n - 1 ] [ $ val ] ) { return $ val ; } } return -1 ; } $ num = 1 ; $ arr = array ( 3 , 10 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; $ maxLimit = 15 ; echo findMaxVal ( $ arr , $ n , $ num , $ maxLimit ) ; ? >
Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code
< ? php function gen ( $ n ) { if ( $ n == 0 ) return 0 ; else if ( $ n == 1 ) return 1 ; else if ( $ n % 2 == 0 ) return 4 * gen ( $ n / 2 ) ; else if ( $ n % 2 == 1 ) return 4 * gen ( $ n / 2 ) + 1 ; } function moserDeBruijn ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( gen ( $ i ) . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } $ n = 15 ; echo ( " First ▁ " . $ n . " ▁ terms ▁ of ▁ " . " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ STRNEWLINE " ) ; echo ( moserDeBruijn ( $ n ) ) ; ? >
Minimum Sum Path in a Triangle | Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; return the top element ; Driver Code
< ? php function minSumPath ( & $ A ) { $ memo = array ( ) ; for ( $ i = 0 ; $ i < count ( $ A ) ; $ i ++ ) $ memo [ $ i ] = 0 ; $ n = count ( $ A ) - 1 ; for ( $ i = 0 ; $ i < count ( $ A [ $ n ] ) ; $ i ++ ) $ memo [ $ i ] = $ A [ $ n ] [ $ i ] ; for ( $ i = count ( $ A ) - 2 ; $ i >= 0 ; $ i -- ) for ( $ j = 0 ; $ j < count ( $ A [ $ i + 1 ] ) - 1 ; $ j ++ ) $ memo [ $ j ] = $ A [ $ i ] [ $ j ] + min ( $ memo [ $ j ] , $ memo [ $ j + 1 ] ) ; return $ memo [ 0 ] ; } $ A = array ( array ( 2 ) , array ( 3 , 9 ) , array ( 1 , 6 , 7 ) ) ; echo ( minSumPath ( $ A ) ) ; ? >
Minimum removals from array to make max | PHP program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver Code
< ? php $ MAX = 100 ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { for ( $ j = 0 ; $ j < $ MAX ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; } function countRemovals ( $ a , $ i , $ j , $ k ) { global $ dp ; if ( $ i >= $ j ) return 0 ; else if ( ( $ a [ $ j ] - $ a [ $ i ] ) <= $ k ) return 0 ; else if ( $ dp [ $ i ] [ $ j ] != -1 ) return $ dp [ $ i ] [ $ j ] ; else if ( ( $ a [ $ j ] - $ a [ $ i ] ) > $ k ) { $ dp [ $ i ] [ $ j ] = 1 + min ( countRemovals ( $ a , $ i + 1 , $ j , $ k ) , countRemovals ( $ a , $ i , $ j - 1 , $ k ) ) ; } return $ dp [ $ i ] [ $ j ] ; } function removals ( $ a , $ n , $ k ) { sort ( $ a ) ; if ( $ n == 1 ) return 0 ; else return countRemovals ( $ a , 0 , $ n - 1 , $ k ) ; } $ a = array ( 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ) ; $ n = count ( $ a ) ; $ k = 4 ; echo ( removals ( $ a , $ n , $ k ) ) ; ? >
Number of ordered pairs such that ( Ai & Aj ) = 0 | Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) are considered different ; Driver Code
< ? php function countPairs ( $ a , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( ( $ a [ $ i ] & $ a [ $ j ] ) == 0 ) $ count += 2 ; } return $ count ; } { $ a = array ( 3 , 4 , 2 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo countPairs ( $ a , $ n ) ; return 0 ; }
Minimum sum of multiplications of n numbers | Used in recursive memoized solution ; function to calculate the cumulative sum from a [ i ] to a [ j ] ; base case ; memoization , if the partition has been called before then return the stored value ; store a max value ; we break them into k partitions ; store the min of the formula thus obtained ; return the minimum ; Driver code
< ? php $ dp = array ( array ( ) ) ; function sum ( $ a , $ i , $ j ) { $ ans = 0 ; for ( $ m = $ i ; $ m <= $ j ; $ m ++ ) $ ans = ( $ ans + $ a [ $ m ] ) % 100 ; return $ ans ; } function solve ( $ a , $ i , $ j ) { global $ dp ; if ( $ i == $ j ) return 0 ; if ( $ dp [ $ i ] [ $ j ] != -1 ) return $ dp [ $ i ] [ $ j ] ; $ dp [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = $ i ; $ k < $ j ; $ k ++ ) { $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i ] [ $ j ] , ( solve ( $ a , $ i , $ k ) + solve ( $ a , $ k + 1 , $ j ) + ( sum ( $ a , $ i , $ k ) * sum ( $ a , $ k + 1 , $ j ) ) ) ) ; } return $ dp [ $ i ] [ $ j ] ; } function initialize ( $ n ) { global $ dp ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; } $ a = array ( 40 , 60 , 20 ) ; $ n = count ( $ a ) ; initialize ( $ n ) ; echo solve ( $ a , 0 , $ n - 1 ) ; ? >
Print Fibonacci Series in reverse order | PHP Program to print Fibonacci series in reverse order ; assigning first and second elements ; storing sum in the preceding location ; printing array in reverse order ; Driver COde
< ? php function reverseFibonacci ( $ n ) { $ a [ 0 ] = 0 ; $ a [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ a [ $ i ] = $ a [ $ i - 2 ] + $ a [ $ i - 1 ] ; } for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { echo ( $ a [ $ i ] . " " ) ; } } $ n = 5 ; reverseFibonacci ( $ n ) ; ? >
Probability of reaching a point with 2 or 3 steps at a time | Returns probability to reach N ; Driver code
< ? php function find_prob ( $ N , $ P ) { $ dp ; $ dp [ 0 ] = 1 ; $ dp [ 1 ] = 0 ; $ dp [ 2 ] = $ P ; $ dp [ 3 ] = 1 - $ P ; for ( $ i = 4 ; $ i <= $ N ; ++ $ i ) $ dp [ $ i ] = ( $ P ) * $ dp [ $ i - 2 ] + ( 1 - $ P ) * $ dp [ $ i - 3 ] ; return $ dp [ $ N ] ; } $ n = 5 ; $ p = 0.2 ; echo find_prob ( $ n , $ p ) ; ? >
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
< ? php $ N = 5 ; function printHosoya ( $ n ) { global $ N ; $ dp = array_fill ( 0 , $ N , array_fill ( 0 , $ N , 0 ) ) ; $ dp [ 0 ] [ 0 ] = $ dp [ 1 ] [ 0 ] = $ dp [ 1 ] [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i > $ j ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i - 2 ] [ $ j ] ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j - 1 ] + $ dp [ $ i - 2 ] [ $ j - 2 ] ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) echo $ dp [ $ i ] [ $ j ] . " ▁ " ; echo " STRNEWLINE " ; } } $ n = 5 ; printHosoya ( $ n ) ; ? >
Different ways to sum n using numbers greater than or equal to m | PHP Program to find number of ways to which numbers that are greater than given number can be added to get sum . ; Return number of ways to which numbers that are greater than given number can be added to get sum . ; Filling the table . k is for numbers greater than or equal that are allowed . ; i is for sum ; initializing dp [ i ] [ k ] to number ways to get sum using numbers greater than or equal k + 1 ; if i > k ; Driver Program
< ? php $ MAX = 100 ; function numberofways ( $ n , $ m ) { global $ MAX ; $ dp = array_fill ( 0 , $ n + 2 , array_fill ( 0 , $ n + 2 , NULL ) ) ; $ dp [ 0 ] [ $ n + 1 ] = 1 ; for ( $ k = $ n ; $ k >= $ m ; $ k -- ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] [ $ k ] = $ dp [ $ i ] [ $ k + 1 ] ; if ( $ i - $ k >= 0 ) $ dp [ $ i ] [ $ k ] = ( $ dp [ $ i ] [ $ k ] + $ dp [ $ i - $ k ] [ $ k ] ) ; } } return $ dp [ $ n ] [ $ m ] ; } $ n = 3 ; $ m = 1 ; echo numberofways ( $ n , $ m ) ; return 0 ; ? >
Entringer Number | Return Entringer Number E ( n , k ) ; Base Case ; Base Case ; Recursive step ; Driven Code
< ? php function zigzag ( $ n , $ k ) { if ( $ n == 0 and $ k == 0 ) return 1 ; if ( $ k == 0 ) return 0 ; return zigzag ( $ n , $ k - 1 ) + zigzag ( $ n - 1 , $ n - $ k ) ; } $ n = 4 ; $ k = 3 ; echo zigzag ( $ n , $ k ) ; ? >
Eulerian Number | Return euleriannumber A ( n , m ) ; For each row from 1 to n ; For each column from 0 to m ; If i is greater than j ; If j is 0 , then make that state as 1. ; basic recurrence relation . ; Driver Code
< ? php function eulerian ( $ n , $ m ) { $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) for ( $ j = 0 ; $ j < $ m + 1 ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ m ; $ j ++ ) { if ( $ i > $ j ) { if ( $ j == 0 ) $ dp [ $ i ] [ $ j ] = 1 ; else $ dp [ $ i ] [ $ j ] = ( ( $ i - $ j ) * $ dp [ $ i - 1 ] [ $ j - 1 ] ) + ( ( $ j + 1 ) * $ dp [ $ i - 1 ] [ $ j ] ) ; } } } return $ dp [ $ n ] [ $ m ] ; } $ n = 3 ; $ m = 1 ; echo eulerian ( $ n , $ m ) ; ? >
Delannoy Number | Return the nth Delannoy Number . ; Base case ; Recursive step . ; Driver Code
< ? php function dealnnoy ( $ n , $ m ) { if ( $ m == 0 or $ n == 0 ) return 1 ; return dealnnoy ( $ m - 1 , $ n ) + dealnnoy ( $ m - 1 , $ n - 1 ) + dealnnoy ( $ m , $ n - 1 ) ; } $ n = 3 ; $ m = 4 ; echo dealnnoy ( $ n , $ m ) ; ? >
Delannoy Number | Return the nth Delannoy Number . ; Base cases ; Driven Code
< ? php function dealnnoy ( $ n , $ m ) { $ dp [ $ m + 1 ] [ $ n + 1 ] = 0 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ 0 ] [ $ i ] = 1 ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i - 1 ] [ $ j - 1 ] + $ dp [ $ i ] [ $ j - 1 ] ; return $ dp [ $ m ] [ $ n ] ; } $ n = 3 ; $ m = 4 ; echo dealnnoy ( $ n , $ m ) ; ? >
Longest alternating ( positive and negative ) subarray starting at every index | PHP program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Print result ; Driver code
< ? php function longestAlternating ( $ arr , $ n ) { $ count = array ( ) ; $ count [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] * $ arr [ $ i + 1 ] < 0 ) $ count [ $ i ] = $ count [ $ i + 1 ] + 1 ; else $ count [ $ i ] = 1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ count [ $ i ] , " ▁ " ; } $ a = array ( -5 , -1 , -1 , 2 , -2 , -3 ) ; $ n = count ( $ a ) ; longestAlternating ( $ a , $ n ) ; ? >
Maximum value with the choice of either dividing or considering as it is | function for calculating max possible result ; Compute remaining values in bottom up manner . ; Driver Code
< ? php function maxDP ( $ n ) { $ res [ 0 ] = 0 ; $ res [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res [ $ i ] = max ( $ i , ( $ res [ $ i / 2 ] + $ res [ $ i / 3 ] + $ res [ $ i / 4 ] + $ res [ $ i / 5 ] ) ) ; return $ res [ $ n ] ; } $ n = 60 ; echo " MaxSum = " ? >