codeparrot/xlcost-text-to-code huggingface dataset:
Number of n digit stepping numbers | function that calculates the answer ; if n is 1 then answer will be 10. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; Driver Code
< ? php function answer ( $ n ) { if ( $ n == 1 ) return 10 ; for ( $ j = 0 ; $ j <= 9 ; $ j ++ ) $ dp [ 1 ] [ $ j ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= 9 ; $ j ++ ) { if ( $ j == 0 ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j + 1 ] ; else if ( $ j == 9 ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j - 1 ] ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j - 1 ] + $ dp [ $ i - 1 ] [ $ j + 1 ] ; } } $ sum = 0 ; for ( $ j = 1 ; $ j <= 9 ; $ j ++ ) $ sum += $ dp [ $ n ] [ $ j ] ; return $ sum ; } $ n = 2 ; echo answer ( $ n ) ; ? >
Find the element that appears once in an array where every other element appears twice | Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code
< ? php function findSingle ( $ ar , $ ar_size ) { $ res = $ ar [ 0 ] ; for ( $ i = 1 ; $ i < $ ar_size ; $ i ++ ) $ res = $ res ^ $ ar [ $ i ] ; return $ res ; } $ ar = array ( 2 , 3 , 5 , 4 , 5 , 3 , 4 ) ; $ n = count ( $ ar ) ; echo " Element ▁ occurring ▁ once ▁ is ▁ " , findSingle ( $ ar , $ n ) ; ? >
Count all triplets whose sum is equal to a perfect cube | PHP program to calculate all triplets whose sum is perfect cube . ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver code
< ? php $ dp = array_fill ( 0 , 1001 , array_fill ( 0 , 15001 , NULL ) ) ; function computeDpArray ( & $ arr , $ n ) { global $ dp ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { for ( $ j = 1 ; $ j <= 15000 ; ++ $ j ) { if ( $ i == 0 ) $ dp [ $ i ] [ $ j ] = ( $ j == $ arr [ $ i ] ) ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + ( $ arr [ $ i ] == $ j ) ; } } } function countTripletSum ( & $ arr , $ n ) { global $ dp ; computeDpArray ( $ arr , $ n ) ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; ++ $ i ) { for ( $ j = $ i + 1 ; $ j < $ n - 1 ; ++ $ j ) { for ( $ k = 1 ; $ k <= 24 ; ++ $ k ) { $ cube = $ k * $ k * $ k ; $ rem = $ cube - ( $ arr [ $ i ] + $ arr [ $ j ] ) ; if ( $ rem > 0 ) $ ans += $ dp [ $ n - 1 ] [ $ rem ] - $ dp [ $ j ] [ $ rem ] ; } } } return $ ans ; } $ arr = array ( 2 , 5 , 1 , 20 , 6 ) ; $ n = sizeof ( $ arr ) ; echo countTripletSum ( $ arr , $ n ) ; ? >
Maximum Subarray Sum Excluding Certain Elements | Function to check the element present in array B ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call
< ? php function isPresent ( $ B , $ m , $ x ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ B [ $ i ] == $ x ) return true ; return false ; } function findMaxSubarraySumUtil ( $ A , $ B , $ n , $ m ) { $ max_so_far = PHP_INT_MIN ; $ curr_max = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPresent ( $ B , $ m , $ A [ $ i ] ) ) { $ curr_max = 0 ; continue ; } $ curr_max = max ( $ A [ $ i ] , $ curr_max + $ A [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; } return $ max_so_far ; } function findMaxSubarraySum ( $ A , $ B , $ n , $ m ) { $ maxSubarraySum = findMaxSubarraySumUtil ( $ A , $ B , $ n , $ m ) ; if ( $ maxSubarraySum == PHP_INT_MIN ) { echo ( " Maximum ▁ Subarray ▁ " . " Sum ▁ cant ▁ be ▁ found STRNEWLINE " ) ; } else { echo ( " The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ▁ " . $ maxSubarraySum . " STRNEWLINE " ) ; } } $ A = array ( 3 , 4 , 5 , -4 , 6 ) ; $ B = array ( 1 , 8 , 5 ) ; $ n = count ( $ A ) ; $ m = count ( $ B ) ; findMaxSubarraySum ( $ A , $ B , $ n , $ m ) ; ? >
Number of n | Returns count of non - decreasing numbers with n digits . ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; Driver Code
< ? php function nonDecNums ( $ n ) { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) $ a [ 0 ] [ $ i ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ a [ $ i ] [ 9 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 8 ; $ j >= 0 ; $ j -- ) $ a [ $ i ] [ $ j ] = $ a [ $ i - 1 ] [ $ j ] + $ a [ $ i ] [ $ j + 1 ] ; return $ a [ $ n ] [ 0 ] ; } $ n = 2 ; echo " Non - decreasing ▁ digits ▁ = ▁ " , nonDecNums ( $ n ) , " STRNEWLINE " ; ? >
Count Balanced Binary Trees of Height h | PHP program to count number of balanced ; base cases ; Driver Code
< ? php $ mod = 1000000007 ; function countBT ( $ h ) { global $ mod ; $ dp [ 0 ] = $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ h ; $ i ++ ) { $ dp [ $ i ] = ( $ dp [ $ i - 1 ] * ( ( 2 * $ dp [ $ i - 2 ] ) % $ mod + $ dp [ $ i - 1 ] ) % $ mod ) % $ mod ; } return $ dp [ $ h ] ; } $ h = 3 ; echo " No . ▁ of ▁ balanced ▁ binary ▁ trees " , " ▁ of ▁ height ▁ h ▁ is : ▁ " , countBT ( $ h ) , " STRNEWLINE " ; ? >
Equilibrium index of an array | function to find the equilibrium index ; Check for indexes one by one until an equilibrium index is found ; get left sum ; get right sum ; if leftsum and rightsum are same , then we are done ; return - 1 if no equilibrium index is found ; Driver code
< ? php function equilibrium ( $ arr , $ n ) { $ i ; $ j ; $ leftsum ; $ rightsum ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ leftsum = 0 ; $ rightsum = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ leftsum += $ arr [ $ j ] ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ rightsum += $ arr [ $ j ] ; if ( $ leftsum == $ rightsum ) return $ i ; } return -1 ; } $ arr = array ( -7 , 1 , 5 , 2 , -4 , 3 , 0 ) ; $ arr_size = sizeof ( $ arr ) ; echo equilibrium ( $ arr , $ arr_size ) ; ? >
Find number of endless points | Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage or column after this point , increment result . ; Driver code
< ? php function countEndless ( $ input , $ n ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ isEndless = 1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ input [ $ i ] [ $ j ] == 0 ) $ isEndless = 0 ; $ col [ $ i ] [ $ j ] = $ isEndless ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ isEndless = 1 ; for ( $ j = $ n - 1 ; $ j >= 0 ; $ j -- ) { if ( $ input [ $ i ] [ $ j ] == 0 ) $ isEndless = 0 ; $ row [ $ i ] [ $ j ] = $ isEndless ; } } $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 1 ; $ j < $ n ; $ j ++ ) if ( $ row [ $ i ] [ $ j ] && $ col [ $ i ] [ $ j ] ) $ ans ++ ; return $ ans ; } $ input = array ( array ( 1 , 0 , 1 , 1 ) , array ( 0 , 1 , 1 , 1 ) , array ( 1 , 1 , 1 , 1 ) , array ( 0 , 1 , 1 , 0 ) ) ; $ n = 4 ; echo countEndless ( $ input , $ n ) ; ? >
Equilibrium index of an array | function to find the equilibrium index ; initialize sum of whole array ; initialize leftsum ; Find sum of the whole array ; sum is now right sum for index i ; If no equilibrium index found , then return 0 ; Driver code
< ? php function equilibrium ( $ arr , $ n ) { $ sum = 0 ; $ leftsum = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ sum += $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ sum -= $ arr [ $ i ] ; if ( $ leftsum == $ sum ) return $ i ; $ leftsum += $ arr [ $ i ] ; } return -1 ; } $ arr = array ( -7 , 1 , 5 , 2 , -4 , 3 , 0 ) ; $ arr_size = sizeof ( $ arr ) ; echo " First ▁ equilibrium ▁ index ▁ is ▁ " , equilibrium ( $ arr , $ arr_size ) ; ? >
Sum of all substrings of a string representing a number | Set 1 | Method to convert character digit to integer digit ; Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver Code
< ? php function toDigit ( $ ch ) { return ( $ ch - '0' ) ; } function sumOfSubstrings ( $ num ) { $ n = strlen ( $ num ) ; $ sumofdigit [ $ n ] = 0 ; $ sumofdigit [ 0 ] = toDigit ( $ num [ 0 ] ) ; $ res = $ sumofdigit [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ numi = toDigit ( $ num [ $ i ] ) ; $ sumofdigit [ $ i ] = ( $ i + 1 ) * $ numi + 10 * $ sumofdigit [ $ i - 1 ] ; $ res += $ sumofdigit [ $ i ] ; } return $ res ; } $ num = "1234" ; echo sumOfSubstrings ( $ num ) ; ? >
Leaders in an array | PHP Function to print leaders in an array ; the loop didn 't break ; Driver Code
< ? 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 ) ; ? >
Leaders in an array | PHP Function to print leaders in an array ; Rightmost element is always leader ; Driver Code
< ? 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 ) ; ? >
Unbounded Knapsack ( Repetition of items allowed ) | Returns the maximum value with knapsack of W capacity ; dp [ i ] is going to store maximum value with knapsack capacity i . ; Fill dp [ ] using above recursive formula ; Driver Code
< ? php function unboundedKnapsack ( $ W , $ n , $ val , $ wt ) { for ( $ i = 0 ; $ i <= $ W ; $ i ++ ) $ dp [ $ i ] = 0 ; $ ans = 0 ; for ( $ i = 0 ; $ i <= $ W ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ wt [ $ j ] <= $ i ) $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ i - $ wt [ $ j ] ] + $ val [ $ j ] ) ; return $ dp [ $ W ] ; } $ W = 100 ; $ val = array ( 10 , 30 , 20 ) ; $ wt = array ( 5 , 10 , 15 ) ; $ n = count ( $ val ) ; sizeof ( $ val ) / sizeof ( $ val [ 0 ] ) ; echo unboundedKnapsack ( $ W , $ n , $ val , $ wt ) ; ? >
Ceiling in a sorted array | Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to first element , then return the first element ; Otherwise , linearly search for ceil value ; if x lies between arr [ i ] and arr [ i + 1 ] including arr [ i + 1 ] , then return arr [ i + 1 ] ; If we reach here then x is greater than the last element of the array , return - 1 in this case ; Driver Code
< ? php function ceilSearch ( $ arr , $ low , $ high , $ x ) { if ( $ x <= $ arr [ $ low ] ) return $ low ; for ( $ i = $ low ; $ i < $ high ; $ i ++ ) { if ( $ arr [ $ i ] == $ x ) return $ i ; if ( $ arr [ $ i ] < $ x && $ arr [ $ i + 1 ] >= $ x ) return $ i + 1 ; } return -1 ; } $ arr = array ( 1 , 2 , 8 , 10 , 10 , 12 , 19 ) ; $ n = sizeof ( $ arr ) ; $ x = 3 ; $ index = ceilSearch ( $ arr , 0 , $ n - 1 , $ x ) ; if ( $ index == -1 ) echo ( " Ceiling ▁ of ▁ " . $ x . " ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " ) ; else echo ( " ceiling ▁ of ▁ " . $ x . " ▁ is ▁ " . $ arr [ $ index ] ) ; ? >
Maximum sum subarray removing at most one element | Method returns maximum sum of all subarray where removing one element is also allowed ; Maximum sum subarrays in forward and backward directions ; Initialize current max and max so far . ; calculating maximum sum subarrays in forward direction ; storing current maximum till ith , in forward array ; calculating maximum sum subarrays in backward direction ; storing current maximum from ith , in backward array ; Initializing final ans by max_so_far so that , case when no element is removed to get max sum subarray is also handled ; choosing maximum ignoring ith element ; Driver Code
< ? php function maxSumSubarrayRemovingOneEle ( $ arr , $ n ) { $ fw = array ( ) ; $ bw = array ( ) ; $ cur_max = $ arr [ 0 ] ; $ max_so_far = $ arr [ 0 ] ; $ fw [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ cur_max = max ( $ arr [ $ i ] , $ cur_max + $ arr [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ cur_max ) ; $ fw [ $ i ] = $ cur_max ; } $ cur_max = $ max_so_far = $ bw [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ cur_max = max ( $ arr [ $ i ] , $ cur_max + $ arr [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ cur_max ) ; $ bw [ $ i ] = $ cur_max ; } $ fans = $ max_so_far ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) $ fans = max ( $ fans , $ fw [ $ i - 1 ] + $ bw [ $ i + 1 ] ) ; return $ fans ; } $ arr = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = count ( $ arr ) ; echo maxSumSubarrayRemovingOneEle ( $ arr , $ n ) ; ? >
Ceiling in a sorted array | Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to the first element , then return the first element ; If x is greater than the last element , then return - 1 ; get the index of middle element of arr [ low . . high ] ; If x is same as middle element , then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; If x is smaller than arr [ mid ] , then either arr [ mid ] is ceiling of x or ceiling lies in arr [ low ... . mid - 1 ] ; Driver Code
< ? php function ceilSearch ( $ arr , $ low , $ high , $ x ) { $ mid ; if ( $ x <= $ arr [ $ low ] ) return $ low ; if ( $ x > $ arr [ $ high ] ) return -1 ; $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; else if ( $ arr [ $ mid ] < $ x ) { if ( $ mid + 1 <= $ high && $ x <= $ arr [ $ mid + 1 ] ) return $ mid + 1 ; else return ceilSearch ( $ arr , $ mid + 1 , $ high , $ x ) ; } else { if ( $ mid - 1 >= $ low && $ x > $ arr [ $ mid - 1 ] ) return $ mid ; else return ceilSearch ( $ arr , $ low , $ mid - 1 , $ x ) ; } } $ arr = array ( 1 , 2 , 8 , 10 , 10 , 12 , 19 ) ; $ n = sizeof ( $ arr ) ; $ x = 20 ; $ index = ceilSearch ( $ arr , 0 , $ n - 1 , $ x ) ; if ( $ index == -1 ) echo ( " Ceiling ▁ of ▁ $ x ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " ) ; else echo ( " ceiling ▁ of ▁ $ x ▁ is " ) ; echo ( isset ( $ arr [ $ index ] ) ) ; ? >
Path with maximum average value | method returns maximum average of all path of cost matrix ; Initialize first column of total cost ( dp ) array ; Initialize first row of dp array ; Construct rest of the dp array ; divide maximum sum by constant path length : ( 2 N - 1 ) for getting average ; Driver code
< ? php function maxAverageOfPath ( $ cost , $ N ) { $ dp = array ( array ( ) ) ; $ dp [ 0 ] [ 0 ] = $ cost [ 0 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ dp [ $ i ] [ 0 ] = $ dp [ $ i - 1 ] [ 0 ] + $ cost [ $ i ] [ 0 ] ; for ( $ j = 1 ; $ j < $ N ; $ j ++ ) $ dp [ 0 ] [ $ j ] = $ dp [ 0 ] [ $ j - 1 ] + $ cost [ 0 ] [ $ j ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ N ; $ j ++ ) $ dp [ $ i ] [ $ j ] = max ( $ dp [ $ i - 1 ] [ $ j ] , $ dp [ $ i ] [ $ j - 1 ] ) + $ cost [ $ i ] [ $ j ] ; } return $ dp [ $ N - 1 ] [ $ N - 1 ] / ( 2 * $ N - 1 ) ; } $ cost = array ( array ( 1 , 2 , 3 ) , array ( 6 , 5 , 4 ) , array ( 7 , 3 , 9 ) ) ; echo maxAverageOfPath ( $ cost , 3 ) ; ? >
Maximum weight path ending at any element of last row in a matrix | Function which return the maximum weight path sum ; creat 2D matrix to store the sum of the path ; Initialize first column of total weight array ( dp [ i to N ] [ 0 ] ) ; Calculate rest path sum of weight matrix ; find the max weight path sum to rech the last row ; return maximum weight path sum ; Driver program
< ? php function maxCost ( $ mat , $ N ) { $ dp = array ( array ( ) ) ; memset ( dp , 0 , sizeof ( dp ) ) ; $ dp [ 0 ] [ 0 ] = $ mat [ 0 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ dp [ $ i ] [ 0 ] = $ mat [ $ i ] [ 0 ] + $ dp [ $ i - 1 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { for ( $ j = 1 ; $ j < $ i + 1 && $ j < $ N ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ mat [ $ i ] [ $ j ] + max ( $ dp [ $ i - 1 ] [ $ j - 1 ] , $ dp [ $ i - 1 ] [ $ j ] ) ; } $ result = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( $ result < $ dp [ $ N - 1 ] [ $ i ] ) $ result = $ dp [ $ N - 1 ] [ $ i ] ; return $ result ; } $ mat = array ( array ( 4 , 1 , 5 , 6 , 1 ) , array ( 2 , 9 , 2 , 11 , 10 ) , array ( 15 , 1 , 3 , 15 , 2 ) , array ( 16 , 92 , 41 , 4 , 3 ) , array ( 8 , 142 , 6 , 4 , 8 ) ) ; $ N = 5 ; echo " Maximum ▁ Path ▁ Sum ▁ : ▁ " , maxCost ( $ mat , $ N ) ; ? >
Maximum profit by buying and selling a share at most k times | Function to find out maximum profit by buying & selling a share atmost k times given stock price of n days ; table to store results of subproblems profit [ t ] [ i ] stores maximum profit using atmost t transactions up to day i ( including day i ) ; For day 0 , you can 't  earn money irrespective  of how many times you trade ; profit is 0 if we don 't  do any transaction  (i.e. k =0) ; fill the table in bottom - up fashion ; Driver Code
< ? php function maxProfit ( $ price , $ n , $ k ) { $ profit [ $ k + 1 ] [ $ n + 1 ] = 0 ; for ( $ i = 0 ; $ i <= $ k ; $ i ++ ) $ profit [ $ i ] [ 0 ] = 0 ; for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ profit [ 0 ] [ $ j ] = 0 ; $ prevDiff = NULL ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ prevDiff = max ( $ prevDiff , $ profit [ $ i - 1 ] [ $ j - 1 ] - $ price [ $ j - 1 ] ) ; $ profit [ $ i ] [ $ j ] = max ( $ profit [ $ i ] [ $ j - 1 ] , $ price [ $ j ] + $ prevDiff ) ; } } return $ profit [ $ k ] [ $ n - 1 ] ; } $ k = 3 ; $ price = array ( 12 , 14 , 17 , 10 , 14 , 13 , 12 , 15 ) ; $ n = sizeof ( $ price ) ; echo " Maximum ▁ profit ▁ is : ▁ " , maxProfit ( $ price , $ n , $ k ) ; ? >
Count even length binary sequences with same sum of first and second half bits | diff is difference between sums first n bits and last n bits respectively ; We can 't cover difference of  more than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Driver Code
< ? php function countSeq ( $ n , $ diff ) { if ( abs ( $ diff ) > $ n ) return 0 ; if ( $ n == 1 && $ diff == 0 ) return 2 ; if ( $ n == 1 && abs ( $ diff ) == 1 ) return 1 ; $ res = countSeq ( $ n - 1 , $ diff + 1 ) + 2 * countSeq ( $ n - 1 , $ diff ) + countSeq ( $ n - 1 , $ diff - 1 ) ; return $ res ; } $ n = 2 ; echo " Count ▁ of ▁ sequences ▁ is ▁ " , countSeq ( $ n , 0 ) ; ? >
Count even length binary sequences with same sum of first and second half bits | A memoization based PHP program to count even length binary sequences such that the sum of first and second half bits is same ; A lookup table to store the results of subproblems ; dif is difference between sums of first n bits and last n bits i . e . , dif = ( Sum of first n bits ) - ( Sum of last n bits ) ; We can 't cover difference of more  than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; Check if this subproblem is already solved n is added to dif to make sure index becomes positive ; $res = First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Store result in lookup table and return the result ; A Wrapper over countSeqUtil ( ) . It mainly initializes lookup table , then calls countSeqUtil ( ) ; call countSeqUtil ( ) ; Driver Code
< ? php $ MAX = 1000 ; $ lookup = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , -1 ) ) ; function countSeqUtil ( $ n , $ dif ) { global $ lookup ; if ( abs ( $ dif ) > $ n ) return 0 ; if ( $ n == 1 && $ dif == 0 ) return 2 ; if ( $ n == 1 && abs ( $ dif ) == 1 ) return 1 ; if ( $ lookup [ $ n ] [ $ n + $ dif ] != -1 ) return $ lookup [ $ n ] [ $ n + $ dif ] ; countSeqUtil ( $ n - 1 , $ dif + 1 ) + 2 * countSeqUtil ( $ n - 1 , $ dif ) + countSeqUtil ( $ n - 1 , $ dif - 1 ) ; return $ lookup [ $ n ] [ $ n + $ dif ] = $ res ; } function countSeq ( $ n ) { return countSeqUtil ( $ n , 0 ) ; } $ n = 2 ; echo " Count ▁ of ▁ sequences ▁ is ▁ " . countSeq ( $ n ) ; ? >
Remove minimum elements from either side such that 2 * min becomes more than max | A utility function to find minimum in arr [ l . . h ] ; A utility function to find maximum in arr [ l . . h ] ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; If there is 1 or less elements , return 0. For a single element , 2 * min > max . ( Assumption : All elements are positive in arr [ ] ) ; 1 ) Find minimum and maximum in arr [ l . . h ] ; If the property is followed , no removals needed ; Otherwise remove a character from left end and recur , then remove a character from right end and recur , take the minimum of two is returned ; Driver Code
< ? php function min_1 ( & $ arr , $ l , $ h ) { $ mn = $ arr [ $ l ] ; for ( $ i = $ l + 1 ; $ i <= $ h ; $ i ++ ) if ( $ mn > $ arr [ $ i ] ) $ mn = $ arr [ $ i ] ; return $ mn ; } function max_1 ( & $ arr , $ l , $ h ) { $ mx = $ arr [ $ l ] ; for ( $ i = $ l + 1 ; $ i <= $ h ; $ i ++ ) if ( $ mx < $ arr [ $ i ] ) $ mx = $ arr [ $ i ] ; return $ mx ; } function minRemovals ( & $ arr , $ l , $ h ) { if ( $ l >= $ h ) return 0 ; $ mn = min_1 ( $ arr , $ l , $ h ) ; $ mx = max_1 ( $ arr , $ l , $ h ) ; if ( 2 * $ mn > $ mx ) return 0 ; return min ( minRemovals ( $ arr , $ l + 1 , $ h ) , minRemovals ( $ arr , $ l , $ h - 1 ) ) + 1 ; } $ arr = array ( 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ) ; $ n = sizeof ( $ arr ) ; echo minRemovals ( $ arr , 0 , $ n - 1 ) ; ? >
Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 2D table to store results of subproblems ; Count of paths to reach any cell in first column is 1 ; Count of paths to reach any cell in first column is 1 ; Calculate count of paths for other cells in bottom - up manner using the recursive solution ; By uncommenting the last part the code calculated the total possible paths if the diagonal Movements are allowed ; Driver Code
< ? php function numberOfPaths ( $ m , $ n ) { $ count = array ( ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ count [ $ i ] [ 0 ] = 1 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ count [ 0 ] [ $ j ] = 1 ; for ( $ i = 1 ; $ i < $ m ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) $ count [ $ i ] [ $ j ] = $ count [ $ i - 1 ] [ $ j ] + $ count [ $ i ] [ $ j - 1 ] + count [ i - 1 ] [ j - 1 ] ; } return $ count [ $ m - 1 ] [ $ n - 1 ] ; } echo numberOfPaths ( 3 , 3 ) ; ? >
Maximum Product Cutting | DP | Utility function to get the maximum of two and three integers ; The main function that returns maximum product obtainable from a rope of length n ; Base cases ; Make a cut at different places and take the maximum of all ; Return the maximum of all values ; Driver Code
< ? php function max_1 ( $ a , $ b , $ c ) { return max ( $ a , max ( $ b , $ c ) ) ; } function maxProd ( $ n ) { if ( $ n == 0 $ n == 1 ) return 0 ; $ max_val = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ max_val = max_1 ( $ max_val , $ i * ( $ n - $ i ) , maxProd ( $ n - $ i ) * $ i ) ; return $ max_val ; } echo " Maximum ▁ Product ▁ is ▁ " . maxProd ( 10 ) ; ? >