codeparrot/xlcost-text-to-code huggingface dataset:
Find the maximum number of composite summands of a number | PHP implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array
< ? php $ maxn = 16 ; function precompute ( ) { $ dp = array_fill ( 0 , $ GLOBALS [ ' axn ' , -1 ) ; $ dp [ 0 ] = 0 ; $ v = array ( 4 , 6 , 9 ) ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' axn ' ; ++ $ i ) { for ( $ k = 0 ; $ k < 3 ; $ k ++ ) { $ j = $ v [ $ k ] ; if ( $ i >= $ j && $ dp [ $ i - $ j ] != -1 ) { $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ i - $ j ] + 1 ) ; } } } return $ dp ; } function Maximum_Summands ( $ dp , $ n ) { if ( $ n < $ GLOBALS [ ' axn ' ) return $ dp [ $ n ] ; else { $ t = ( $ n - $ GLOBALS [ ' axn ' ) / 4 + 1 ; return $ t + $ dp [ $ n - 4 * $ t ] ; } } $ n = 12 ; $ dp = precompute ( ) ; echo Maximum_Summands ( $ dp , $ n ) ; ? >
Find sub | PHP implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ ] [ ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call
< ? php $ GLOBALS [ ' N ' ] = 4 ; function getSum ( $ r1 , $ r2 , $ c1 , $ c2 , $ dp ) { return $ dp [ $ r2 ] [ $ c2 ] - $ dp [ $ r2 ] [ $ c1 ] - $ dp [ $ r1 ] [ $ c2 ] + $ dp [ $ r1 ] [ $ c1 ] ; } function sumFound ( $ K , $ S , $ grid ) { $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) $ dp [ $ i + 1 ] [ $ j + 1 ] = $ dp [ $ i + 1 ] [ $ j ] + $ dp [ $ i ] [ $ j + 1 ] - $ dp [ $ i ] [ $ j ] + $ grid [ $ i ] [ $ j ] ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) { $ sum = getSum ( $ i , $ i + $ K , $ j , $ j + $ K , $ dp ) ; if ( $ sum == $ S ) return true ; } return false ; } $ grid = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 9 , 10 , 11 , 12 ) , array ( 13 , 14 , 15 , 16 ) ) ; $ K = 2 ; $ S = 14 ; if ( sumFound ( $ K , $ S , $ grid ) ) echo " Yes " ; else echo " No " ; ? >
Minimum number of operations required to sum to binary string S | Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 ] [ 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i ] [ 1 ] ; Transition from dp [ i - 1 ] [ 0 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 ] [ 1 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of adding 2 ^ i ; Driver Code
< ? php function findMinOperations ( $ S ) { $ p = strrev ( $ S ) ; $ n = strlen ( $ p ) ; $ dp = array_fill ( 0 , $ n + 1 , array_fill ( 0 , 2 , NULL ) ) ; if ( $ p [ 0 ] == '0' ) { $ dp [ 0 ] [ 0 ] = 0 ; } else { $ dp [ 0 ] [ 0 ] = 1 ; } $ dp [ 0 ] [ 1 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ p [ $ i ] == '0' ) { $ dp [ $ i ] [ 0 ] = $ dp [ $ i - 1 ] [ 0 ] ; $ dp [ $ i ] [ 1 ] = 1 + min ( $ dp [ $ i - 1 ] [ 1 ] , $ dp [ $ i - 1 ] [ 0 ] ) ; } else { $ dp [ $ i ] [ 1 ] = $ dp [ $ i - 1 ] [ 1 ] ; $ dp [ $ i ] [ 0 ] = 1 + min ( $ dp [ $ i - 1 ] [ 0 ] , $ dp [ $ i - 1 ] [ 1 ] ) ; } } return $ dp [ $ n - 1 ] [ 0 ] ; } $ S = "100" ; echo findMinOperations ( $ S ) . " STRNEWLINE " ; $ S = "111" ; echo findMinOperations ( $ S ) . " STRNEWLINE " ; return 0 ; ? >
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
< ? php function solve ( $ N , $ K ) { $ Array = array_fill ( 0 , $ N + 1 , NULL ) ; if ( $ N <= $ K ) { echo "1" . " STRNEWLINE " ; return ; } $ i = 0 ; $ sum = $ K ; for ( $ i = 1 ; $ i <= $ K ; ++ $ i ) { $ Array [ $ i ] = 1 ; } $ Array [ $ i ] = $ sum ; for ( $ i = $ K + 2 ; $ i <= $ N ; ++ $ i ) { $ Array [ $ i ] = $ sum - $ Array [ $ i - $ K - 1 ] + $ Array [ $ i - 1 ] ; $ sum = $ Array [ $ i ] ; } echo $ Array [ $ N ] . " STRNEWLINE " ; } $ N = 4 ; $ K = 2 ; solve ( $ N , $ K ) ; ? >
Minimum sum possible of any bracket sequence of length N | PHP program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; / Not a proper bracket expression ; If reaches at end ; / If proper bracket expression ; else if not , return max ; If already visited ; To find out minimum sum ; Driver Code
< ? php $ MAX_VAL = 10000000 ; $ dp = array_fill ( 0 , 100 , array_fill ( 0 , 100 , -1 ) ) ; function find ( $ index , $ openbrk , $ n , $ adj ) { global $ MAX_VAL ; global $ dp ; if ( $ openbrk < 0 ) return $ MAX_VAL ; if ( $ index == $ n ) { if ( $ openbrk == 0 ) { return 0 ; } return $ MAX_VAL ; } if ( $ dp [ $ index ] [ $ openbrk ] != -1 ) return $ dp [ $ index ] [ $ openbrk ] ; $ dp [ $ index ] [ $ openbrk ] = min ( $ adj [ $ index ] [ 1 ] + find ( $ index + 1 , $ openbrk + 1 , $ n , $ adj ) , $ adj [ $ index ] [ 0 ] + find ( $ index + 1 , $ openbrk - 1 , $ n , $ adj ) ) ; return $ dp [ $ index ] [ $ openbrk ] ; } $ n = 4 ; $ adj = array ( array ( 5000 , 3000 ) , array ( 6000 , 2000 ) , array ( 8000 , 1000 ) , array ( 9000 , 6000 ) ) ; echo find ( 1 , 1 , $ n , $ adj ) + $ adj [ 0 ] [ 1 ] ; ? >
Maximum length of segments of 0 ' s ▁ and ▁ 1' s | Recursive Function to find total length of the array where 1 is greater than zero ; If reaches till end ; If $dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for $start index ; Driver Code ; Size of string ; Calling the function to find the value of function
< ? php function find ( $ start , $ adj , $ n , $ dp ) { if ( $ start == $ n ) return 0 ; if ( $ dp [ $ start ] != -1 ) return $ dp [ $ start ] ; $ dp [ $ start ] = 0 ; $ one = 0 ; $ zero = 0 ; for ( $ k = $ start ; $ k < $ n ; $ k ++ ) { if ( $ adj [ $ k ] == '1' ) $ one ++ ; else $ zero ++ ; if ( $ one > $ zero ) $ dp [ $ start ] = max ( $ dp [ $ start ] , find ( $ k + 1 , $ adj , $ n , $ dp ) + $ k - $ start + 1 ) ; else $ dp [ $ start ] = max ( $ dp [ $ start ] , find ( $ k + 1 , $ adj , $ n , $ dp ) ) ; } return $ dp [ $ start ] ; } $ adj = "100110001010001" ; $ n = strlen ( $ adj ) ; $ dp = array_fill ( 0 , $ n + 1 , -1 ) ; echo find ( 0 , $ adj , $ n , $ dp ) ; ? >
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
< ? php function isVowel ( $ ch ) { if ( $ ch == ' a ' $ ch == ' e ' $ ch == ' i ' $ ch == ' o ' $ ch == ' u ' ) return true ; return false ; } function lcs ( $ X , $ Y , $ m , $ n ) { $ L = array_fill ( 0 , $ m + 1 , array_fill ( 0 , $ n + 1 , NULL ) ) ; 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 ] ) && isVowel ( $ X [ $ i - 1 ] ) ) $ L [ $ i ] [ $ j ] = $ L [ $ i - 1 ] [ $ j - 1 ] + 1 ; else $ L [ $ i ] [ $ j ] = max ( $ L [ $ i - 1 ] [ $ j ] , $ L [ $ i ] [ $ j - 1 ] ) ; } } return $ L [ $ m ] [ $ n ] ; } $ X = " aieef " ; $ Y = " klaief " ; $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; echo " Length ▁ of ▁ LCS ▁ = ▁ " . lcs ( $ X , $ Y , $ m , $ n ) ; ? >
Minimum number of single digit primes required whose sum is equal to N | function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number N . ; Not possible ; Driver Code
< ? php function check ( $ i , $ val ) { if ( $ i - $ val < 0 ) return false ; return true ; } function MinimumPrimes ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = 1e9 ; $ dp [ 0 ] = $ dp [ 2 ] = $ dp [ 3 ] = $ dp [ 5 ] = $ dp [ 7 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( check ( $ i , 2 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 2 ] ) ; if ( check ( $ i , 3 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 3 ] ) ; if ( check ( $ i , 5 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 5 ] ) ; if ( check ( $ i , 7 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 7 ] ) ; } if ( $ dp [ $ n ] == ( 1e9 ) ) return -1 ; else return $ dp [ $ n ] ; } $ n = 12 ; $ minimal = MinimumPrimes ( $ n ) ; if ( $ minimal != -1 ) { echo ( " Minimum ▁ number ▁ of ▁ single ▁ " . " digit ▁ primes ▁ required ▁ : " ) ; echo ( $ minimal ) ; } else { echo ( " Not ▁ possible " ) ; } ? >
Number of Unique BST with a given key | Dynamic Programming | Function to find number of unique BST ; DP to store the number of unique BST with key i ; Base case ; fill the dp table in top - down approach . ; n - i in right * i - 1 in left ; Driver Code
< ? php function numberOfBST ( $ n ) { $ dp = array ( $ n + 1 ) ; for ( $ i = 0 ; $ i <= $ n + 1 ; $ i ++ ) $ dp [ $ i ] = 0 ; $ dp [ 0 ] = 1 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) { $ dp [ $ i ] += ( ( $ dp [ $ i - $ j ] ) * ( $ dp [ $ j - 1 ] ) ) ; } } return $ dp [ $ n ] ; } $ n = 3 ; echo " Number ▁ of ▁ structurally ▁ " . " Unique ▁ BST ▁ with ▁ " , $ n , " ▁ keys ▁ are ▁ : ▁ " , numberOfBST ( $ n ) ; ? >
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Return summation of r * nCr ; Driver Code
< ? php function summation ( $ n ) { return $ n << ( $ n - 1 ) ; } $ n = 2 ; echo summation ( $ n ) ; ? >
Maximum Product Subarray | Added negative product case | Function to find maximum subarray product . ; As maximum product can be negative , so initialize ans with minimum integer value . ; Variable to store maximum product until current value . ; Variable to store minimum product until current value . ; Variable used during updation of maximum product and minimum product . is prevMax ; If current element is positive , update maxval . Update minval if it is negative . ; If current element is zero , maximum product cannot end at current element . Update minval with 1 and maxval with 0. maxval is updated to 0 as in case all other elements are negative , then maxval is 0. ; If current element is negative , then new value of maxval is previous minval * arr [ i ] and new value of minval is previous maxval * arr [ i ] . Before updating maxval , store its previous value in prevMax to be used to update minval . ; Update ans if necessary . ; If maxval is zero , then to calculate product for next iteration , it should be set to 1 as maximum product subarray does not include 0. The minimum possible value to be considered in maximum product subarray is already stored in minval , so when maxval is negative it is set to 1. ; Driver Code
< ? php function findMaxProduct ( & $ arr , $ n ) { $ ans = 0 ; $ maxval = 1 ; $ minval = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) { $ maxval = $ maxval * $ arr [ i ] ; $ minval = min ( 1 , $ minval * $ arr [ $ i ] ) ; } else if ( $ arr [ $ i ] == 0 ) { $ minval = 1 ; $ maxval = 0 ; } else if ( $ arr [ $ i ] < 0 ) { $ prevMax = $ maxval ; $ maxval = $ minval * $ arr [ $ i ] ; $ minval = $ prevMax * $ arr [ $ i ] ; } $ ans = max ( $ ans , $ maxval ) ; if ( $ maxval <= 0 ) { $ maxval = 1 ; } } return $ ans ; } $ arr = array ( 0 , -4 , 0 , -2 ) ; $ n = sizeof ( $ arr ) ; echo findMaxProduct ( $ arr , $ n ) ; ? >
Maximum and Minimum Values of an Algebraic Expression | PHP program to find the maximum and minimum values of an Algebraic expression of given form ; Finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by 50 to avoid negative indexing in array ; Driver Code
< ? php function minMaxValues ( $ arr , $ n , $ m ) { $ sum = 0 ; $ INF = 1000000000 ; $ MAX = 50 ; for ( $ i = 0 ; $ i < ( $ n + $ m ) ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ arr [ $ i ] += 50 ; } $ dp = array ( ) ; for ( $ i = 0 ; $ i < $ MAX + 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ MAX * $ MAX + 1 ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; } $ dp [ 0 ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i < ( $ n + $ m ) ; $ i ++ ) { for ( $ k = min ( $ n , $ i + 1 ) ; $ k >= 1 ; $ k -- ) { for ( $ j = 0 ; $ j < $ MAX * $ MAX + 1 ; $ j ++ ) { if ( $ dp [ $ k - 1 ] [ $ j ] ) $ dp [ $ k ] [ $ j + $ arr [ $ i ] ] = 1 ; } } } $ max_value = -1 * $ INF ; $ min_value = $ INF ; for ( $ i = 0 ; $ i < $ MAX * $ MAX + 1 ; $ i ++ ) { if ( $ dp [ $ n ] [ $ i ] ) { $ temp = $ i - 50 * $ n ; $ max_value = max ( $ max_value , $ temp * ( $ sum - $ temp ) ) ; $ min_value = min ( $ min_value , $ temp * ( $ sum - $ temp ) ) ; } } echo ( " Maximum ▁ Value : ▁ " . $ max_value . " " . ▁ " Minimum Value : " . ▁ $ min _ value . ▁ " " } $ n = 2 ; $ m = 2 ; $ arr = [ 1 , 2 , 3 , 4 ] ; minMaxValues ( $ arr , $ n , $ m ) ; ? >
Golomb sequence | Return the nth element of Golomb sequence ; base case ; Recursive Step ; Print the first n term of Golomb Sequence ; Finding first n terms of Golomb Sequence . ; Driver Code
< ? php function findGolomb ( $ n ) { if ( $ n == 1 ) return 1 ; return 1 + findGolomb ( $ n - findGolomb ( findGolomb ( $ n - 1 ) ) ) ; } function printGolomb ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo findGolomb ( $ i ) , " ▁ " ; } $ n = 9 ; printGolomb ( $ n ) ; ? >
Printing Items in 0 / 1 Knapsack | Prints the items which are kept in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; stores the result of Knapsack ; either the result comes from the top ( K [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in Knapsack table . If it comes from the latter one / it means the item is included . ; This item is included . ; Since this weight is included its value is deducted ; Driver code
< ? php function printknapSack ( $ W , & $ wt , & $ val , $ n ) { $ K = array_fill ( 0 , $ n + 1 , array_fill ( 0 , $ W + 1 , NULL ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ w = 0 ; $ w <= $ W ; $ w ++ ) { if ( $ i == 0 $ w == 0 ) $ K [ $ i ] [ $ w ] = 0 ; else if ( $ wt [ $ i - 1 ] <= $ w ) $ K [ $ i ] [ $ w ] = max ( $ val [ $ i - 1 ] + $ K [ $ i - 1 ] [ $ w - $ wt [ $ i - 1 ] ] , $ K [ $ i - 1 ] [ $ w ] ) ; else $ K [ $ i ] [ $ w ] = $ K [ $ i - 1 ] [ $ w ] ; } } $ res = $ K [ $ n ] [ $ W ] ; echo $ res . " STRNEWLINE " ; $ w = $ W ; for ( $ i = $ n ; $ i > 0 && $ res > 0 ; $ i -- ) { if ( $ res == $ K [ $ i - 1 ] [ $ w ] ) continue ; else { echo $ wt [ $ i - 1 ] . " " ; $ res = $ res - $ val [ $ i - 1 ] ; $ w = $ w - $ wt [ $ i - 1 ] ; } } } $ val = array ( 60 , 100 , 120 ) ; $ wt = array ( 10 , 20 , 30 ) ; $ W = 50 ; $ n = sizeof ( $ val ) ; printknapSack ( $ W , $ wt , $ val , $ n ) ; ? >
Maximum number of segments of lengths a , b and c | function to find the maximum number of segments ; stores the maximum number of segments each index can have ; initialize with - 1 ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( $i + $a <= $n ) avoid buffer overflow ; if ( $i + $b <= $n ) avoid buffer overflow ; if ( $i + $c <= $n ) avoid buffer overflow ; Driver code
< ? php function maximumSegments ( $ n , $ a , $ b , $ c ) { $ dp = array ( ) ; for ( $ i = 0 ; $ i < $ n + 10 ; $ i ++ ) $ dp [ $ i ] = -1 ; $ dp [ 0 ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ dp [ $ i ] != -1 ) { $ dp [ $ i + $ a ] = max ( $ dp [ $ i ] + 1 , $ dp [ $ i + $ a ] ) ; $ dp [ $ i + $ b ] = max ( $ dp [ $ i ] + 1 , $ dp [ $ i + $ b ] ) ; $ dp [ $ i + $ c ] = max ( $ dp [ $ i ] + 1 , $ dp [ $ i + $ c ] ) ; } } return $ dp [ $ n ] ; } $ n = 7 ; $ a = 5 ; $ b = 2 ; $ c = 5 ; echo ( maximumSegments ( $ n , $ a , $ b , $ c ) ) ; ? >
Print n terms of Newman | Function to find the n - th element ; Declare array to store sequence ; Driver Code
< ? php function sequence ( $ n ) { $ f = array ( 0 ) ; $ f [ 0 ] = 0 ; $ f [ 1 ] = 1 ; $ f [ 2 ] = 1 ; echo $ f [ 1 ] , " " ▁ , ▁ $ f [ 2 ] ▁ , ▁ " " for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ f [ $ i ] = $ f [ $ f [ $ i - 1 ] ] + $ f [ $ i - $ f [ $ i - 1 ] ] ; echo $ f [ $ i ] , " " ; } } { $ n = 13 ; sequence ( $ n ) ; return 0 ; } ? >
Entringer Number | Return Entringer Number E ( n , k ) ; Base cases ; Finding dp [ i ] [ j ] ; Driven Code
< ? php function zigzag ( $ n , $ k ) { $ dp = array ( array ( ) ) ; $ dp [ 0 ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i ] [ $ j - 1 ] + $ dp [ $ i - 1 ] [ $ i - $ j ] ; } return $ dp [ $ n ] [ $ k ] ; } $ n = 4 ; $ k = 3 ; echo zigzag ( $ n , $ k ) ; ? >
Lobb Number | PHP Program to find Ln , m Lobb Number . ; Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using p reviously stored values ; Return the Lm , n Lobb Number . ; Driven Code
< ? php $ MAXN = 109 ; function binomialCoeff ( $ n , $ k ) { $ C = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= min ( $ i , $ k ) ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ C [ $ i ] [ $ j ] = 1 ; else $ C [ $ i ] [ $ j ] = $ C [ $ i - 1 ] [ $ j - 1 ] + $ C [ $ i - 1 ] [ $ j ] ; } } return $ C [ $ n ] [ $ k ] ; } function lobb ( $ n , int $ m ) { return ( ( 2 * $ m + 1 ) * binomialCoeff ( 2 * $ n , $ m + $ n ) ) / ( $ m + $ n + 1 ) ; } $ n = 5 ; $ m = 3 ; echo lobb ( $ n , $ m ) ; ? >
Count of arrays having consecutive element with different values | PHP Program to find count of arrays . ; Return the number of arrays with given constartints . ; Initialising dp [ 0 ] and dp [ 1 ] . ; Computing f ( i ) for each 2 <= i <= n . ; Driven Code
< ? php $ MAXN = 109 ; function countarray ( $ n , $ k , $ x ) { $ dp = array ( 0 ) ; $ dp [ 0 ] = 0 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) $ dp [ $ i ] = ( $ k - 2 ) * $ dp [ $ i - 1 ] + ( $ k - 1 ) * $ dp [ $ i - 2 ] ; return ( $ x == 1 ? ( $ k - 1 ) * $ dp [ $ n - 2 ] : $ dp [ $ n - 1 ] ) ; } $ n = 4 ; $ k = 3 ; $ x = 2 ; echo countarray ( $ n , $ k , $ x ) ; ? >
Number of palindromic subsequences of length k where k <= 3 | PHP program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driver Code
< ? php $ MAX = 100 ; $ MAX_CHAR = 26 ; function precompute ( $ s , $ n , & $ l , & $ r ) { global $ MAX , $ MAX_CHAR ; $ l [ ord ( $ s [ 0 ] ) - ord ( ' a ' ) ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) $ l [ $ j ] [ $ i ] += $ l [ $ j ] [ $ i - 1 ] ; $ l [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] [ $ i ] ++ ; } $ r [ ord ( $ s [ $ n - 1 ] ) - ord ( ' a ' ) ] [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) $ r [ $ j ] [ $ i ] += $ r [ $ j ] [ $ i + 1 ] ; $ r [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] [ $ i ] ++ ; } } function countPalindromes ( $ k , $ n , & $ l , & $ r ) { global $ MAX , $ MAX_CHAR ; $ ans = 0 ; if ( $ k == 1 ) { for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) $ ans += $ l [ $ i ] [ $ n - 1 ] ; return $ ans ; } if ( $ k == 2 ) { for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) $ ans += ( ( $ l [ $ i ] [ $ n - 1 ] * ( $ l [ $ i ] [ $ n - 1 ] - 1 ) ) / 2 ) ; return $ ans ; } for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) $ ans += $ l [ $ j ] [ $ i - 1 ] * $ r [ $ j ] [ $ i + 1 ] ; return $ ans ; } $ s = " aabab " ; $ k = 2 ; $ n = strlen ( $ s ) ; $ l = array_fill ( 0 , $ MAX_CHAR , array_fill ( 0 , $ MAX , NULL ) ) ; $ r = array_fill ( 0 , $ MAX_CHAR , array_fill ( 0 , $ MAX , NULL ) ) ; precompute ( $ s , $ n , $ l , $ r ) ; echo countPalindromes ( $ k , $ n , $ l , $ r ) . " " ; ? >
Maximum sum path in a matrix from top to bottom | PHP implementation to find the maximum sum path in a matrix ; function to find the maximum sum path in a matric ; if there is a single element only ; dp [ ] [ ] matrix to store the results of each iteration ; base case , copying elements of last row ; building up the dp [ ] [ ] matrix from bottom to the top row ; finding the maximum diagonal element in the ( i + 1 ) th row if that cell exists ; adding that ' max ' element to the mat [ i ] [ j ] element ; finding the maximum value from the first row of dp [ ] [ ] ; required maximum sum ; Driver Code
< ? php $ SIZE = 10 ; function maxSum ( $ mat , $ n ) { if ( $ n == 1 ) return $ mat [ 0 ] [ 0 ] ; $ dp = array ( array ( ) ) ; $ maxSum = PHP_INT_MIN ; $ max ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ dp [ $ n - 1 ] [ $ j ] = $ mat [ $ n - 1 ] [ $ j ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ max = PHP_INT_MIN ; if ( ( ( $ j - 1 ) >= 0 ) and ( $ max < $ dp [ $ i + 1 ] [ $ j - 1 ] ) ) $ max = $ dp [ $ i + 1 ] [ $ j - 1 ] ; if ( ( ( $ j + 1 ) < $ n ) and ( $ max < $ dp [ $ i + 1 ] [ $ j + 1 ] ) ) $ max = $ dp [ $ i + 1 ] [ $ j + 1 ] ; $ dp [ $ i ] [ $ j ] = $ mat [ $ i ] [ $ j ] + $ max ; } } for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ maxSum < $ dp [ 0 ] [ $ j ] ) $ maxSum = $ dp [ 0 ] [ $ j ] ; return $ maxSum ; } $ mat = array ( array ( 5 , 6 , 1 , 7 ) , array ( -2 , 10 , 8 , -1 ) , array ( 3 , -7 , -9 , 11 ) , array ( 12 , -4 , 2 , 6 ) ) ; $ n = 4 ; echo " Maximum ▁ Sum ▁ = ▁ " , maxSum ( $ mat , $ n ) ; ? >