codeparrot/xlcost-text-to-code huggingface dataset:
Maximize array sum after K negations | Set 1 | This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; this the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver Code
< ? php function maximumSum ( $ arr , $ n , $ k ) { $ INT_MAX = 0 ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { $ min = $ INT_MAX ; $ index = -1 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] < $ min ) { $ min = $ arr [ $ j ] ; $ index = $ j ; } } if ( $ min == 0 ) break ; $ arr [ $ index ] = - $ arr [ $ index ] ; } $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; return $ sum ; } $ arr = array ( -2 , 0 , 5 , -1 , 2 ) ; $ k = 4 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo maximumSum ( $ arr , $ n , $ k ) ; ? >
Maximum sum such that no two elements are adjacent | Set 2 | PHP program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code
< ? php $ maxLen = 10 ; $ dp = array_fill ( 0 , $ GLOBALS [ ' axLen ' , 0 ) ; $ v = array_fill ( 0 , $ GLOBALS [ ' axLen ' , 0 ) ; function maxSum ( $ arr , $ i , $ n ) { if ( $ i >= $ n ) return 0 ; if ( $ GLOBALS [ ' v ' ] [ $ i ] ) return $ GLOBALS [ ' dp ' ] [ $ i ] ; $ GLOBALS [ ' v ' ] [ $ i ] = 1 ; $ GLOBALS [ ' dp ' ] [ $ i ] = max ( maxSum ( $ arr , $ i + 1 , $ n ) , $ arr [ $ i ] + maxSum ( $ arr , $ i + 2 , $ n ) ) ; return $ GLOBALS [ ' dp ' ] [ $ i ] ; } $ arr = array ( 12 , 9 , 7 , 33 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , 0 , $ n ) ; ? >
Maximize the subarray sum after multiplying all elements of any subarray with X | PHP implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code
< ? php $ N = 5 ; function func ( $ idx , $ cur , $ a , $ dp , $ n , $ x ) { if ( $ idx == $ n ) return 0 ; if ( $ dp [ $ idx ] [ $ cur ] != -1 ) return $ dp [ $ idx ] [ $ cur ] ; $ ans = 0 ; if ( $ cur == 0 ) { $ ans = max ( $ ans , $ a [ $ idx ] + func ( $ idx + 1 , 0 , $ a , $ dp , $ n , $ x ) ) ; $ ans = max ( $ ans , $ x * $ a [ $ idx ] + func ( $ idx + 1 , 1 , $ a , $ dp , $ n , $ x ) ) ; } else if ( $ cur == 1 ) { $ ans = max ( $ ans , $ x * $ a [ $ idx ] + func ( $ idx + 1 , 1 , $ a , $ dp , $ n , $ x ) ) ; $ ans = max ( $ ans , $ a [ $ idx ] + func ( $ idx + 1 , 2 , $ a , $ dp , $ n , $ x ) ) ; } else $ ans = max ( $ ans , $ a [ $ idx ] + func ( $ idx + 1 , 2 , $ a , $ dp , $ n , $ x ) ) ; return $ dp [ $ idx ] [ $ cur ] = $ ans ; } function getMaximumSum ( $ a , $ n , $ x ) { $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { $ dp [ $ i ] [ $ j ] = -1 ; } } $ maxi = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ maxi = max ( $ maxi , func ( $ i , 0 , $ a , $ dp , $ n , $ x ) ) ; return $ maxi ; } $ a = array ( -3 , 8 , -2 , 1 , -6 ) ; $ n = count ( $ a ) ; $ x = -1 ; echo getMaximumSum ( $ a , $ n , $ x ) ; ? >
Count pairs of non | PHP implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to return the number of pairs ; Create the dp table initially ; Declare the left array ; Declare the right array ; Initially left [ 0 ] is 1 ; Count the number of palindrome pairs to the left ; Initially right most as 1 ; Count the number of palindrome pairs to the right ; Count the number of pairs ; Driver code
< ? php $ N = 100 ; function pre_process ( $ dp , $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = false ; } for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { for ( $ i = 0 ; $ i <= $ n - $ j ; $ i ++ ) { if ( $ j <= 2 ) { if ( $ s [ $ i ] == $ s [ $ i + $ j - 1 ] ) $ dp [ $ i ] [ $ i + $ j - 1 ] = true ; } else if ( $ s [ $ i ] == $ s [ $ i + $ j - 1 ] ) $ dp [ $ i ] [ $ i + $ j - 1 ] = $ dp [ $ i + 1 ] [ $ i + $ j - 2 ] ; } } return $ dp ; } function countPairs ( $ s ) { $ dp = array ( array ( ) ) ; $ dp = pre_process ( $ dp , $ s ) ; $ n = strlen ( $ s ) ; $ left = array_fill ( 0 , $ n , 0 ) ; $ right = array_fill ( 0 , $ n , 0 ) ; $ left [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) { if ( $ dp [ $ j ] [ $ i ] == 1 ) $ left [ $ i ] ++ ; } } $ right [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ right [ $ i ] = $ right [ $ i + 1 ] ; for ( $ j = $ n - 1 ; $ j >= $ i ; $ j -- ) { if ( $ dp [ $ i ] [ $ j ] == 1 ) $ right [ $ i ] ++ ; } } $ ans = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) $ ans += $ left [ $ i ] * $ right [ $ i + 1 ] ; return $ ans ; } $ s = " abacaba " ; echo countPairs ( $ s ) ; ? >
Queries to check if substring [ L ... R ] is palindrome or not | PHP implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to answer every query in O ( 1 ) ; Driver code
< ? php $ N = 100 ; function pre_process ( $ dp , $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = false ; } for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { for ( $ i = 0 ; $ i <= $ n - $ j ; $ i ++ ) { if ( $ j <= 2 ) { if ( $ s [ $ i ] == $ s [ $ i + $ j - 1 ] ) $ dp [ $ i ] [ $ i + $ j - 1 ] = true ; } else if ( $ s [ $ i ] == $ s [ $ i + $ j - 1 ] ) $ dp [ $ i ] [ $ i + $ j - 1 ] = $ dp [ $ i + 1 ] [ $ i + $ j - 2 ] ; } } return $ dp ; } function answerQuery ( $ l , $ r , $ dp ) { if ( $ dp [ $ l ] [ $ r ] ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; } $ s = " abaaab " ; $ dp = array ( array ( ) ) ; $ dp = pre_process ( $ dp , $ s ) ; $ queries = array ( array ( 0 , 1 ) , array ( 1 , 5 ) ) ; $ q = count ( $ queries ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) answerQuery ( $ queries [ $ i ] [ 0 ] , $ queries [ $ i ] [ 1 ] , $ dp ) ; ? >
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
< ? php $ N = 100005 ; function LIS ( $ a , $ n ) { $ dp = array ( ) ; $ d = array ( ) ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ dp [ $ a [ $ i ] ] = 1 ; for ( $ j = 2 ; $ j * $ j <= $ a [ $ i ] ; $ j ++ ) { if ( $ a [ $ i ] % $ j == 0 ) { $ dp [ $ a [ $ i ] ] = max ( $ dp [ $ a [ $ i ] ] , $ dp [ $ d [ $ j ] ] + 1 ) ; $ dp [ $ a [ $ i ] ] = max ( $ dp [ $ a [ $ i ] ] , $ dp [ $ d [ $ a [ $ i ] / $ j ] ] + 1 ) ; $ d [ $ j ] = $ a [ $ i ] ; $ d [ $ a [ $ i ] / $ j ] = $ a [ $ i ] ; } } $ ans = max ( $ ans , $ dp [ $ a [ $ i ] ] ) ; $ d [ $ a [ $ i ] ] = $ a [ $ i ] ; } return $ ans ; } $ a = array ( 1 , 2 , 3 , 4 , 5 , 6 ) ; $ n = sizeof ( $ a ) ; echo LIS ( $ a , $ n ) ; ? >
Find the sum of the diagonal elements of the given N X N spiral matrix | Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code
< ? php function findSum ( $ n ) { $ dp = array ( ) ; $ dp [ 1 ] = 1 ; $ dp [ 0 ] = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] = ( 4 * ( $ i * $ i ) ) - 6 * ( $ i - 1 ) + $ dp [ $ i - 2 ] ; } return $ dp [ $ n ] ; } $ n = 4 ; echo findSum ( $ n ) ; ? >
Count number of ways to reach a given score in a Matrix | PHP implementation of the approach ; To store the states of dp ; To check whether a particular state of dp has been solved ; Function to find the ways using memoization ; Base cases ; If required score becomes negative ; If current state has been reached before ; Set current state to visited ; Driver code
< ? php $ n = 3 ; $ MAX = 30 ; $ dp = array ( $ n , $ n , $ MAX ) ; $ v = array ( $ n , $ n , $ MAX ) ; function findCount ( $ mat , $ i , $ j , $ m ) { if ( $ i == 0 && $ j == 0 ) { if ( $ m == $ mat [ 0 ] [ 0 ] ) return 1 ; else return 0 ; } if ( $ m < 0 ) return 0 ; if ( $ i < 0 $ j < 0 ) return 0 ; if ( $ v [ $ i ] [ $ j ] [ $ m ] ) return $ dp [ $ i ] [ $ j ] [ $ m ] ; $ v [ $ i ] [ $ j ] [ $ m ] = true ; $ dp [ $ i ] [ $ j ] [ $ m ] = findCount ( $ mat , $ i - 1 , $ j , $ m - $ mat [ $ i ] [ $ j ] ) + findCount ( $ mat , $ i , $ j - 1 , $ m - $ mat [ $ i ] [ $ j ] ) ; return $ dp [ $ i ] [ $ j ] [ $ m ] ; } $ mat = array ( array ( 1 , 1 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 1 , 1 ) ) ; $ m = 5 ; echo ( findCount ( $ mat , $ n - 1 , $ n - 1 , $ m ) ) ;
Minimum steps to delete a string by deleting substring comprising of same characters | PHP implementation of the approach ; Function to return the minimum number of delete operations ; When a single character is deleted ; When a group of consecutive characters are deleted if any of them matches ; When both the characters are same then delete the letters in between them ; Memoize ; Driver code
< ? php $ GLOBALS [ ' N ' ] = 10 ; function findMinimumDeletion ( $ l , $ r , $ dp , $ s ) { if ( $ l > $ r ) return 0 ; if ( $ l == $ r ) return 1 ; if ( $ dp [ $ l ] [ $ r ] != -1 ) return $ dp [ $ l ] [ $ r ] ; $ res = 1 + findMinimumDeletion ( $ l + 1 , $ r , $ dp , $ s ) ; for ( $ i = $ l + 1 ; $ i <= $ r ; ++ $ i ) { if ( $ s [ $ l ] == $ s [ $ i ] ) $ res = min ( $ res , findMinimumDeletion ( $ l + 1 , $ i - 1 , $ dp , $ s ) + findMinimumDeletion ( $ i , $ r , $ dp , $ s ) ) ; } return $ dp [ $ l ] [ $ r ] = $ res ; } $ s = " abcddcba " ; $ n = strlen ( $ s ) ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo findMinimumDeletion ( 0 , $ n - 1 , $ dp , $ s ) ; ? >
Find the number of distinct pairs of vertices which have a distance of exactly k in a tree | PHP implementation of the approach ; To store vertices and value of k ; To store number vertices at a level i ; To store the final answer ; Function to add an edge between two nodes ; Function to find the number of distinct pairs of the vertices which have a distance of exactly k in a tree ; At level zero vertex itself is counted ; Count the pair of vertices at distance k ; For all levels count vertices ; Driver code ; Add edges ; Function call ; Required answer
< ? php $ N = 5005 ; $ gr = array_fill ( 0 , $ N , array ( ) ) ; $ d = array_fill ( 0 , $ N , array_fill ( 0 , 505 , 0 ) ) ; $ ans = 0 ; function Add_edge ( $ x , $ y ) { global $ gr ; array_push ( $ gr [ $ x ] , $ y ) ; array_push ( $ gr [ $ y ] , $ x ) ; } function dfs ( $ v , $ par ) { global $ d , $ ans , $ k , $ gr ; $ d [ $ v ] [ 0 ] = 1 ; foreach ( $ gr [ $ v ] as & $ i ) { if ( $ i != $ par ) { dfs ( $ i , $ v ) ; for ( $ j = 1 ; $ j <= $ k ; $ j ++ ) $ ans += $ d [ $ i ] [ $ j - 1 ] * $ d [ $ v ] [ $ k - $ j ] ; for ( $ j = 1 ; $ j <= $ k ; $ j ++ ) $ d [ $ v ] [ $ j ] += $ d [ $ i ] [ $ j - 1 ] ; } } } $ n = 5 ; $ k = 2 ; Add_edge ( 1 , 2 ) ; Add_edge ( 2 , 3 ) ; Add_edge ( 3 , 4 ) ; Add_edge ( 2 , 5 ) ; dfs ( 1 , 0 ) ; echo $ ans ; ? >
Sum of XOR of all subarrays | Function to calculate the sum of XOR of all subarrays ; variable to store the final sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update sum ; updating the multiplier ; returning the sum ; Driver Code
< ? php function findXorSum ( $ arr , $ n ) { $ sum = 0 ; $ mul = 1 ; for ( $ i = 0 ; $ i < 30 ; $ i ++ ) { $ c_odd = 0 ; $ odd = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) > 0 ) $ odd = ( ! $ odd ) ; if ( $ odd ) $ c_odd ++ ; } for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum += ( $ mul * $ c_odd ) ; if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) > 0 ) $ c_odd = ( $ n - $ j - $ c_odd ) ; } $ mul *= 2 ; } return $ sum ; } $ arr = array ( 3 , 8 , 13 ) ; $ n = sizeof ( $ arr ) ; echo findXorSum ( $ arr , $ n ) ; ? >
Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; Check if jth bit in the counter is set If set then print jth element from set ; $sum += $a [ $j ] ; if set then consider as ' + ' ; $sum -= $a [ $j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver Code
< ? php function checkCombinations ( $ a , $ n ) { $ pow_set_size = pow ( 2 , $ n ) ; for ( $ counter = 0 ; $ counter < $ pow_set_size ; $ counter ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ counter & ( 1 << $ j ) ) else } if ( $ sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } $ a = array ( 60 , 60 , 120 ) ; $ n = sizeof ( $ a ) ; if ( checkCombinations ( $ a , $ n ) ) echo " YES " ; else echo " NO " ; ? >
Find probability of selecting element from kth column after N iterations | Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code
< ? php function calcProbability ( $ M , $ k ) { $ m = 4 ; $ n = 4 ; $ dp = array ( ) ; $ sum = array ( ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ dp [ 0 ] [ $ j ] = $ M [ 0 ] [ $ j ] ; $ sum [ 0 ] += $ dp [ 0 ] [ $ j ] ; } for ( $ i = 1 ; $ i < $ m ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ dp [ $ i ] [ $ j ] += $ dp [ $ i - 1 ] [ $ j ] / $ sum [ $ i - 1 ] + $ M [ $ i ] [ $ j ] ; $ sum [ $ i ] += $ dp [ $ i ] [ $ j ] ; } } return $ dp [ $ n - 1 ] [ $ k - 1 ] / $ sum [ $ n - 1 ] ; } $ M = array ( array ( 1 , 1 , 0 , 3 ) , array ( 2 , 3 , 2 , 3 ) , array ( 9 , 3 , 0 , 2 ) , array ( 2 , 3 , 2 , 2 ) ) ; $ k = 3 ; echo calcProbability ( $ M , $ k ) ; ? >
Possible cuts of a number such that maximum parts are divisible by 3 | Function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; Stores last index of a remainder ; last visited place of remainder zero is at 0. ; To store result from 0 to i ; get the remainder ; Get maximum res [ i ] value ; Driver Code
< ? php function MaximumNumbers ( $ s ) { $ n = strlen ( $ s ) ; $ remIndex = array_fill ( 0 , 3 , -1 ) ; $ remIndex [ 0 ] = 0 ; $ res = array ( ) ; $ r = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ r = ( $ r + $ s [ $ i - 1 ] - '0' ) % 3 ; $ res [ $ i ] = $ res [ $ i - 1 ] ; if ( $ remIndex [ $ r ] != -1 ) $ res [ $ i ] = max ( $ res [ $ i ] , $ res [ $ remIndex [ $ r ] ] + 1 ) ; $ remIndex [ $ r ] = $ i + 1 ; } return $ res [ $ n ] ; } $ s = "12345" ; print ( MaximumNumbers ( $ s ) ) # This  code is contributed by Ryuga NEW_LINE ? >
Count number of paths whose weight is exactly X and has at | PHP program to count the number of paths ; Function to find the number of paths ; If the summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; else Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths
< ? php $ max = 4 ; $ c = 2 ; function countPaths ( $ sum , $ get , $ m , $ n , & $ dp ) { global $ max , $ c ; if ( $ sum < 0 ) return 0 ; if ( $ sum == 0 ) return $ get ; if ( $ dp [ $ sum ] [ $ get ] != -1 ) return $ dp [ $ sum ] [ $ get ] ; $ res = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i == $ m ) $ res += countPaths ( $ sum - $ i , 1 , $ m , $ n , $ dp ) ; $ res += countPaths ( $ sum - $ i , $ get , $ m , $ n , $ dp ) ; } $ dp [ $ sum ] [ $ get ] = $ res ; return $ dp [ $ sum ] [ $ get ] ; } $ n = 3 ; $ m = 2 ; $ x = 3 ; $ dp = array_fill ( 0 , $ max + 1 , NULL ) ; for ( $ i = 0 ; $ i <= $ max ; $ i ++ ) for ( $ j = 0 ; $ j < 2 ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo countPaths ( $ x , 0 , $ m , $ n , $ dp ) ; ? >
Sum of kth powers of first n natural numbers | A global array to store factorial ; Function to calculate the factorials of all the numbers upto k ; Function to return the binomial coefficient ; nCr = ( n ! * ( n - r ) ! ) / r ! ; Function to return the sum of kth powers of n natural numbers ; When j is unity ; Calculating sum ( n ^ 1 ) of unity powers of n ; storing sum ( n ^ 1 ) for sum ( n ^ 2 ) ; If k = 1 then temp is the result ; For finding sum ( n ^ k ) removing 1 and n * kCk from ( n + 1 ) ^ k ; Removing all kC2 * sum ( n ^ ( k - 2 ) ) + ... + kCk - 1 * ( sum ( n ^ ( k - ( k - 1 ) ) ; Storing the result for next sum of next powers of k ; Driver code
< ? php $ MAX_K = 15 ; $ fac [ $ MAX_K ] = array ( ) ; function factorial ( $ k ) { global $ fac ; $ fac [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ k + 1 ; $ i ++ ) { $ fac [ $ i ] = ( $ i * $ fac [ $ i - 1 ] ) ; } } function bin ( $ a , $ b ) { global $ MAX_K ; global $ fac ; $ ans = ( ( $ fac [ $ a ] ) / ( $ fac [ $ a - $ b ] * $ fac [ $ b ] ) ) ; return $ ans ; } function sumofn ( $ n , $ k ) { $ p = 0 ; $ num1 ; $ temp ; $ arr [ 1000 ] = array ( ) ; for ( $ j = 1 ; $ j <= $ k ; $ j ++ ) { if ( $ j == 1 ) { $ num1 = ( $ n * ( $ n + 1 ) ) / 2 ; $ arr [ $ p ++ ] = $ num1 ; $ temp = $ num1 ; } else { $ temp = ( pow ( $ n + 1 , $ j + 1 ) - 1 - $ n ) ; for ( $ s = 1 ; $ s < $ j ; $ s ++ ) { $ temp = $ temp - ( $ arr [ $ j - $ s - 1 ] * bin ( $ j + 1 , $ s + 1 ) ) ; } $ temp = $ temp / ( $ j + 1 ) ; $ arr [ $ p ++ ] = $ temp ; } } $ temp = $ arr [ $ p - 1 ] ; return $ temp ; } $ n = 5 ; $ k = 2 ; factorial ( $ k ) ; echo sumofn ( $ n , $ k ) , " STRNEWLINE " ; ? >
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors | PHP implementation of the approach ; Recursive function to find the required number of ways ; When all positions are filled ; If adjacent pairs are exactly K ; If already calculated ; Next position filled with same color ; Next position filled with different color So there can be m - 1 different colors ; Driver Code
< ? php $ GLOBALS [ ' max ' ] = 4 ; function countWays ( $ index , $ cnt , $ dp , $ n , $ m , $ k ) { if ( $ index == $ n ) { if ( $ cnt == $ k ) return 1 ; else return 0 ; } if ( $ dp [ $ index ] [ $ cnt ] != -1 ) return $ dp [ $ index ] [ $ cnt ] ; $ ans = 0 ; $ ans += countWays ( $ index + 1 , $ cnt , $ dp , $ n , $ m , $ k ) ; $ ans += ( $ m - 1 ) * countWays ( $ index + 1 , $ cnt + 1 , $ dp , $ n , $ m , $ k ) ; $ dp [ $ index ] [ $ cnt ] = $ ans ; return $ dp [ $ index ] [ $ cnt ] ; } $ n = 3 ; $ m = 3 ; $ k = 2 ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' max ' ] ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo $ m * countWays ( 1 , 0 , $ dp , $ n , $ m , $ k ) ; ? >
Sum of Fibonacci numbers at even indexes upto N terms | Computes value of first fibonacci numbers and stores the even - indexed sum ; Initialize result ; Add remaining terms ; For even indices ; Return the alternting sum ; Get n ; Find the even - indiced sum
< ? php function calculateEvenSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo [ 2 * $ n + 1 ] = array ( ) ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = 0 ; for ( $ i = 2 ; $ i <= 2 * $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; if ( $ i % 2 == 0 ) $ sum += $ fibo [ $ i ] ; } return $ sum ; } $ n = 8 ; echo " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " . $ n . " ▁ terms : ▁ " . calculateEvenSum ( $ n ) . " STRNEWLINE " ; ? >
Gould 's Sequence | 32768 = 2 ^ 15 ; Array to store Sequence up to 2 ^ 16 = 65536 ; Utility function to pre - compute odd numbers in ith row of Pascals 's triangle ; First term of the Sequence is 1 ; Initialize i to 1 ; Initialize p to 1 ( i . e 2 ^ i ) in each iteration i will be pth power of 2 ; loop to generate gould 's Sequence ; i is pth power of 2 traverse the array from j = 0 to i i . e ( 2 ^ p ) ; double the value of arr [ j ] and store to arr [ i + j ] ; update i to next power of 2 ; increment p ; Function to print gould 's Sequence ; loop to generate gould 's  Sequence up to n ; Driver code ; Get n ; Function call
< ? php $ MAX = 32768 ; $ arr = array_fill ( 0 , 2 * $ MAX , 0 ) ; function gouldSequence ( ) { global $ MAX , $ arr ; $ arr [ 0 ] = 1 ; $ i = 1 ; $ p = 1 ; while ( $ i <= $ MAX ) { $ j = 0 ; while ( $ j < $ i ) { $ arr [ $ i + $ j ] = 2 * $ arr [ $ j ] ; $ j ++ ; } $ i = ( 1 << $ p ) ; $ p ++ ; } } function printSequence ( $ n ) { global $ MAX , $ arr ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ arr [ $ i ] . " " ; } } gouldSequence ( ) ; $ n = 16 ; printSequence ( $ n ) ; ? >
Matrix Chain Multiplication ( A O ( N ^ 2 ) Solution ) | Matrix Mi has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in dp [ ] [ ] . 0 th row and 0 th column of dp [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; Simply following above recursive formula . ; Driver code
< ? php function MatrixChainOrder ( $ p , $ n ) { $ dp = array ( ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ dp [ $ i ] [ $ i ] = 0 ; for ( $ L = 1 ; $ L < $ n - 1 ; $ L ++ ) for ( $ i = 1 ; $ i < $ n - $ L ; $ i ++ ) $ dp [ $ i ] [ $ i + $ L ] = min ( $ dp [ $ i + 1 ] [ $ i + $ L ] + $ p [ $ i - 1 ] * $ p [ $ i ] * $ p [ $ i + $ L ] , $ dp [ $ i ] [ $ i + $ L - 1 ] + $ p [ $ i - 1 ] * $ p [ $ i + $ L - 1 ] * $ p [ $ i + $ L ] ) ; return $ dp [ 1 ] [ $ n - 1 ] ; } $ arr = array ( 10 , 20 , 30 , 40 , 30 ) ; $ size = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " . MatrixChainOrder ( $ arr , $ size ) ; ? >
Count common subsequence in two strings | return the number of common subsequence in two strings ; for each character of S ; for each character in T ; if character are same in both the string ; Driver Code
< ? php function CommomSubsequencesCount ( $ s , $ t ) { $ n1 = strlen ( $ s ) ; $ n2 = strlen ( $ t ) ; $ dp = array ( ) ; for ( $ i = 0 ; $ i <= $ n1 ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n2 ; $ j ++ ) { $ dp [ $ i ] [ $ j ] = 0 ; } } for ( $ i = 1 ; $ i <= $ n1 ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n2 ; $ j ++ ) { if ( $ s [ $ i - 1 ] == $ t [ $ j - 1 ] ) $ dp [ $ i ] [ $ j ] = 1 + $ dp [ $ i ] [ $ j - 1 ] + $ dp [ $ i - 1 ] [ $ j ] ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i ] [ $ j - 1 ] + $ dp [ $ i - 1 ] [ $ j ] - $ dp [ $ i - 1 ] [ $ j - 1 ] ; } } return $ dp [ $ n1 ] [ $ n2 ] ; } $ s = " ajblqcpdz " ; $ t = " aefcnbtdi " ; echo CommomSubsequencesCount ( $ s , $ t ) . " " ; ? >