codeparrot/xlcost-text-to-code huggingface dataset:
Minimum jumps to reach last building in a matrix | Recursive PHP program to find minimum jumps to reach last building from first . ; Returns minimum jump path from ( 0 , 0 ) to ( m , n ) in height [ R ] [ C ] ; base case ; Find minimum jumps if we go through diagonal ; Find minimum jumps if we go through down ; Find minimum jumps if we go through right ; return minimum jumps ; Driver Code
< ? php $ R = 4 ; $ C = 3 ; function isSafe ( $ x , $ y ) { global $ R , $ C ; return ( $ x < $ R and $ y < $ C ) ; } function minJump ( $ height , $ x , $ y ) { global $ R , $ C ; if ( $ x == $ R - 1 and $ y == $ C - 1 ) return 0 ; $ diag = PHP_INT_MAX ; if ( isSafe ( $ x + 1 , $ y + 1 ) ) $ diag = minJump ( $ height , $ x + 1 , $ y + 1 ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x + 1 ] [ $ y + 1 ] ) ; $ down = PHP_INT_MAX ; if ( isSafe ( $ x + 1 , $ y ) ) $ down = minJump ( $ height , $ x + 1 , $ y ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x + 1 ] [ $ y ] ) ; $ right = PHP_INT_MAX ; if ( isSafe ( $ x , $ y + 1 ) ) $ right = minJump ( $ height , $ x , $ y + 1 ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x ] [ $ y + 1 ] ) ; return min ( $ down , min ( $ right , $ diag ) ) ; } $ height = array ( array ( 5 , 4 , 2 ) , array ( 9 , 2 , 1 ) , array ( 2 , 5 , 9 ) , array ( 1 , 3 , 11 ) ) ; echo minJump ( $ height , 0 , 0 ) ; ? >
Maximum sum subsequence with at | PHP program to find maximum sum subsequence such that elements are at least k distance away . ; We fill MS from right to left . ; Driver code
< ? php function maxSum ( $ arr , $ N , $ k ) { $ MS [ $ N - 1 ] = $ arr [ $ N - 1 ] ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { if ( $ i + $ k + 1 >= $ N ) $ MS [ $ i ] = max ( $ arr [ $ i ] , $ MS [ $ i + 1 ] ) ; else $ MS [ $ i ] = max ( $ arr [ $ i ] + $ MS [ $ i + $ k + 1 ] , $ MS [ $ i + 1 ] ) ; } return $ MS [ 0 ] ; } $ N = 10 ; $ k = 2 ; $ arr = array ( 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 ) ; echo ( maxSum ( $ arr , $ N , $ k ) ) ; ? >
Longest alternating sub | Function to calculate alternating sub - array for each index of array elements ; Initialize the base state of len [ ] ; Calculating value for each element ; If both elements are different then add 1 to next len [ i + 1 ] ; else initialize to 1 ; Print lengths of binary subarrays . ; Driver Code
< ? php function alternateSubarray ( & $ arr , $ n ) { $ len = array_fill ( 0 , $ n , NULL ) ; $ len [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; -- $ i ) { if ( $ arr [ $ i ] ^ $ arr [ $ i + 1 ] == 1 ) $ len [ $ i ] = $ len [ $ i + 1 ] + 1 ; else $ len [ $ i ] = 1 ; } for ( $ i = 0 ; $ i < $ n ; ++ $ i ) echo $ len [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 0 , 1 , 0 , 0 , 1 ) ; $ n = sizeof ( $ arr ) ; alternateSubarray ( $ arr , $ n ) ; ? >
Longest alternating sub | Function to calculate alternating sub - array for each index of array elements ; Initialize count variable for storing length of sub - array ; Initialize ' prev ' variable which indicates the previous element while traversing for index ' i ' ; If both elements are same print elements because alternate element is not found for current index ; print count and decrement it . ; Increment count for next element ; Re - initialize previous variable ; If elements are still available after traversing whole array , print it ; Driver Code
< ? php function alternateSubarray ( $ arr , $ n ) { $ count = 1 ; $ prev = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; ++ $ i ) { if ( ( $ arr [ $ i ] ^ $ prev ) == 0 ) { while ( $ count ) echo $ count -- , " ▁ " ; } ++ $ count ; $ prev = $ arr [ $ i ] ; } while ( $ count ) echo $ count -- , " ▁ " ; } $ arr = array ( 1 , 0 , 1 , 0 , 0 , 1 ) ; $ n = sizeof ( $ arr ) ; alternateSubarray ( $ arr , $ n ) ; ? >
Count ways to build street under given constraints | function to count ways of building a street of n rows ; base case ; ways of building houses in both the spots of ith row ; ways of building an office in one of the two spots of ith row ; total ways for n rows ; Driver Code
< ? php function countWays ( $ n ) { $ dp [ 0 ] [ 1 ] = 1 ; $ dp [ 1 ] [ 1 ] = 2 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ dp [ 0 ] [ $ i ] = $ dp [ 0 ] [ $ i - 1 ] + $ dp [ 1 ] [ $ i - 1 ] ; $ dp [ 1 ] [ $ i ] = $ dp [ 0 ] [ $ i - 1 ] * 2 + $ dp [ 1 ] [ $ i - 1 ] ; } return $ dp [ 0 ] [ $ n ] + $ dp [ 1 ] [ $ n ] ; } $ n = 5 ; echo " Total ▁ no ▁ of ▁ ways ▁ with ▁ n ▁ = ▁ " , $ n , " ▁ are : ▁ " , countWays ( $ n ) , " STRNEWLINE " ; ? >
Maximum length subsequence with difference between adjacent elements as either 0 or 1 | function to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; Initialize mls [ ] values for all indexes ; Compute optimized maximum length subsequence values in bottom up manner ; Store maximum of all ' mls ' values in ' max ' ; required maximum length subsequence ; Driver Code
< ? php function maxLenSub ( $ arr , $ n ) { $ mls = array ( ) ; $ max = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mls [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) <= 1 and $ mls [ $ i ] < $ mls [ $ j ] + 1 ) $ mls [ $ i ] = $ mls [ $ j ] + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max < $ mls [ $ i ] ) $ max = $ mls [ $ i ] ; return $ max ; } $ arr = array ( 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ length ▁ subsequence ▁ = ▁ " , maxLenSub ( $ arr , $ n ) ; ? >
Coin game winner where every player has three choices | To find winner of game ; To store results ; Initial values ; Computing other values . ; If A losses any of i - 1 or i - x or i - y game then he will definitely win game i ; Else A loses game . ; If dp [ n ] is true then A will game otherwise he losses ; Driver program to test findWinner ( ) ;
< ? php function findWinner ( $ x , $ y , $ n ) { $ dp = array ( ) ; $ dp [ 0 ] = false ; $ dp [ 1 ] = true ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ i - 1 >= 0 and ! $ dp [ $ i - 1 ] ) $ dp [ $ i ] = true ; else if ( $ i - $ x >= 0 and ! $ dp [ $ i - $ x ] ) $ dp [ $ i ] = true ; else if ( $ i - $ y >= 0 and ! $ dp [ $ i - $ y ] ) $ dp [ $ i ] = true ; else $ dp [ $ i ] = false ; } return $ dp [ $ n ] ; } $ x = 3 ; $ y = 4 ; $ n = 5 ; if ( findWinner ( $ x , $ y , $ n ) ) echo ' A ' ; else echo ' B ' ; ? >
Maximum games played by winner | Method returns maximum games a winner needs to play in N - player tournament ; for 0 games , 1 player is needed for 1 game , 2 players are required ; loop until i - th Fibonacci number is less than or equal to N ; result is ( i - 2 ) because i will be incremented one extra in while loop and we want the last value which is smaller than N , so one more decrement ; Driver Code
< ? php function maxGameByWinner ( $ N ) { $ dp [ $ N ] = 0 ; $ dp [ 0 ] = 1 ; $ dp [ 1 ] = 2 ; $ i = 2 ; do { $ dp [ $ i ] = $ dp [ $ i - 1 ] + $ dp [ $ i - 2 ] ; } while ( $ dp [ $ i ++ ] <= $ N ) ; return ( $ i - 2 ) ; } $ N = 10 ; echo maxGameByWinner ( $ N ) ; ? >
Convert to Strictly increasing integer array with minimum changes | To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; Driver Code
< ? php function minRemove ( $ arr , $ n ) { $ LIS = array ( ) ; $ len = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ LIS [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( $ arr [ $ i ] > $ arr [ $ j ] ) $ LIS [ $ i ] = max ( $ LIS [ $ i ] , $ LIS [ $ j ] + 1 ) ; } $ len = max ( $ len , $ LIS [ $ i ] ) ; } return $ n - $ len ; } $ arr = array ( 1 , 2 , 6 , 5 , 4 ) ; $ n = count ( $ arr ) ; echo minRemove ( $ arr , $ n ) ; ? >
Find length of longest subsequence of one string which is substring of another string | Return the maximum size of substring of X which is substring in Y . ; Initialize the dp [ ] [ ] to 0. ; Calculating value for each element . ; If alphabet of string X and Y are equal make dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; Else copy the previous value in the row i . e dp [ i - 1 ] [ j - 1 ] ; Finding the maximum length . ; Driver Code
< ? php function maxSubsequenceSubstring ( $ x , $ y , $ n , $ m ) { $ dp ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { if ( $ x [ $ j - 1 ] == $ y [ $ i - 1 ] ) $ dp [ $ i ] [ $ j ] = 1 + $ dp [ $ i - 1 ] [ $ j - 1 ] ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i ] [ $ j - 1 ] ; } } $ ans = 0 ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) $ ans = max ( $ ans , $ dp [ $ i ] [ $ n ] ) ; return $ ans ; } { $ x = " ABCD " ; $ y = " BACDBDCD " ; $ n = strlen ( $ x ) ; $ m = strlen ( $ y ) ; echo maxSubsequenceSubstring ( $ x , $ y , $ n , $ m ) ; return 0 ; } ? >
Maximum sum in a 2 x n grid such that no two elements are adjacent | Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't  to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver code
< ? php function maxSum ( $ grid , $ n ) { $ incl = max ( $ grid [ 0 ] [ 0 ] , $ grid [ 1 ] [ 0 ] ) ; $ excl = 0 ; $ excl_new ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ excl_new = max ( $ excl , $ incl ) ; $ incl = $ excl + max ( $ grid [ 0 ] [ $ i ] , $ grid [ 1 ] [ $ i ] ) ; $ excl = $ excl_new ; } return max ( $ excl , $ incl ) ; } $ grid = array ( array ( 1 , 2 , 3 , 4 , 5 ) , array ( 6 , 7 , 8 , 9 , 10 ) ) ; $ n = 5 ; echo maxSum ( $ grid , $ n ) ; ? >
Minimum insertions to sort an array | method returns min steps of insertion we need to perform to sort array ' arr ' ; lis [ i ] is going to store length of lis that ends with i . ; Initialize lis values for all indexes ; Compute optimized lis values in bottom up manner ; The overall LIS must end with of the array elements . Pick maximum of all lis values ; return size of array minus length of LIS as final result ; Driver code
< ? php function minInsertionStepToSortArray ( $ arr , $ N ) { $ lis [ $ N ] = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ lis [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] >= $ arr [ $ j ] && $ lis [ $ i ] < $ lis [ $ j ] + 1 ) $ lis [ $ i ] = $ lis [ $ j ] + 1 ; $ max = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( $ max < $ lis [ $ i ] ) $ max = $ lis [ $ i ] ; return ( $ N - $ max ) ; } $ arr = array ( 2 , 3 , 5 , 1 , 4 , 7 , 6 ) ; $ N = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo minInsertionStepToSortArray ( $ arr , $ N ) ; ? >
Shortest Uncommon Subsequence | A dynamic programming based PHP program to find shortest uncommon subsequence . ; Returns length of shortest common subsequence ; declaring 2D array of m + 1 rows and n + 1 columns dynamically ; T string is empty ; S string is empty ; char not present in T ; Driver Code
< ? php $ GLOBALS [ ' MAX ' ] = 1005 ; function shortestSeq ( $ S , $ T ) { $ m = strlen ( $ S ) ; $ n = strlen ( $ T ) ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ 0 ] [ $ i ] = $ GLOBALS [ ' MAX ' ] ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { $ ch = $ S [ $ i - 1 ] ; for ( $ k = $ j - 1 ; $ k >= 0 ; $ k -- ) if ( $ T [ $ k ] == $ ch ) break ; if ( $ k == -1 ) $ dp [ $ i ] [ $ j ] = 1 ; else $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i - 1 ] [ $ j ] , $ dp [ $ i - 1 ] [ $ k ] + 1 ) ; } } $ ans = $ dp [ $ m ] [ $ n ] ; if ( $ ans >= $ GLOBALS [ ' MAX ' ] ) $ ans = -1 ; return $ ans ; } $ S = " babab " ; $ T = " babba " ; $ m = strlen ( $ S ) ; $ n = strlen ( $ T ) ; echo " Length ▁ of ▁ shortest ▁ subsequence ▁ is ▁ : ▁ " , shortestSeq ( $ S , $ T ) ; ? >
Count number of ways to jump to reach end | function to count ways to jump to reach end for each array element ; count_jump [ i ] store number of ways arr [ i ] can reach to the end ; Last element does not require to jump . Count ways to jump for remaining elements ; if the element can directly jump to the end ; add the count of all the elements that can reach to end and arr [ i ] can reach to them ; if element can reach to end then add its count to count_jump [ i ] ; if arr [ i ] cannot reach to the end ; print count_jump for each array element ; Driver Code
< ? php function countWaysToJump ( $ arr , $ n ) { $ count_jump ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count_jump [ $ i ] = 0 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] >= $ n - $ i - 1 ) $ count_jump [ $ i ] ++ ; for ( $ j = $ i + 1 ; $ j < $ n - 1 && $ j <= $ arr [ $ i ] + $ i ; $ j ++ ) if ( $ count_jump [ $ j ] != -1 ) $ count_jump [ $ i ] += $ count_jump [ $ j ] ; if ( $ count_jump [ $ i ] == 0 ) $ count_jump [ $ i ] = -1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ count_jump [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 3 , 5 , 8 , 9 , 1 , 0 , 7 , 6 , 8 , 9 ) ; $ n = count ( $ arr ) ; countWaysToJump ( $ arr , $ n ) ; ? >
Minimum steps to delete a string after repeated deletion of palindrome substrings | method returns minimum step for deleting the string , where in one step a palindrome is removed ; declare dp array and initialize it with 0 s ; loop for substring length we are considering ; loop with two variables i and j , denoting starting and ending of substrings ; If substring length is 1 , then 1 step will be needed ; delete the ith char individually and assign result for subproblem ( i + 1 , j ) ; if current and next char are same , choose min from current and subproblem ( i + 2 , j ) ; loop over all right characters and suppose Kth char is same as ith character then choose minimum from current and two substring after ignoring ith and Kth char ; Uncomment below snippet to print actual dp tablex for ( int i = 0 ; i < N ; i ++ , cout << endl ) for ( int j = 0 ; j < N ; j ++ ) cout << dp [ i ] [ j ] << " ▁ " ; ; Driver code
< ? php function minStepToDeleteString ( $ str ) { $ N = strlen ( $ str ) ; $ dp [ $ N + 1 ] [ $ N + 1 ] = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ N ; $ i ++ ) for ( $ j = 0 ; $ j <= $ N ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ len = 1 ; $ len <= $ N ; $ len ++ ) { for ( $ i = 0 , $ j = $ len - 1 ; $ j < $ N ; $ i ++ , $ j ++ ) { if ( $ len == 1 ) $ dp [ $ i ] [ $ j ] = 1 ; else { $ dp [ $ i ] [ $ j ] = 1 + $ dp [ $ i + 1 ] [ $ j ] ; if ( $ str [ $ i ] == $ str [ $ i + 1 ] ) $ dp [ $ i ] [ $ j ] = min ( 1 + $ dp [ $ i + 2 ] [ $ j ] , $ dp [ $ i ] [ $ j ] ) ; for ( $ K = $ i + 2 ; $ K <= $ j ; $ K ++ ) if ( $ str [ $ i ] == $ str [ $ K ] ) $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i + 1 ] [ $ K - 1 ] + $ dp [ $ K + 1 ] [ $ j ] , $ dp [ $ i ] [ $ j ] ) ; } } } return $ dp [ 0 ] [ $ N - 1 ] ; } $ str = "2553432" ; echo minStepToDeleteString ( $ str ) , " STRNEWLINE " ; ? >
Clustering / Partitioning an array such that sum of square differences is minimum | Initialize answer as infinite . ; function to generate all possible answers . and compute minimum of all costs . i -- > is index of previous partition par -- > is current number of partitions a [ ] and n -- > Input array and its size current_ans -- > Cost of partitions made so far . ; If number of partitions is more than k ; If we have mad k partitions and have reached last element ; 1 ) Partition array at different points 2 ) For every point , increase count of partitions , " par " by 1. 3 ) Before recursive call , add cost of the partition to current_ans ; Driver code
< ? php $ inf = 1000000000 ; $ ans = $ inf ; function solve ( $ i , $ par , & $ a , $ n , $ k , $ current_ans ) { global $ inf , $ ans ; if ( $ par > $ k ) return ; if ( $ par == $ k && $ i == $ n - 1 ) { $ ans = min ( $ ans , $ current_ans ) ; return ; } for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) solve ( $ j , $ par + 1 , $ a , $ n , $ k , $ current_ans + ( $ a [ $ j ] - $ a [ $ i + 1 ] ) * ( $ a [ $ j ] - $ a [ $ i + 1 ] ) ) ; } $ k = 2 ; $ a = array ( 1 , 5 , 8 , 10 ) ; $ n = sizeof ( $ a ) ; solve ( -1 , 0 , $ a , $ n , $ k , 0 ) ; echo $ ans . " STRNEWLINE " ; ? >
Minimum steps to minimize n as per given condition | function to calculate min steps ; base case ; store temp value for n as min ( f ( n - 1 ) , f ( n / 2 ) , f ( n / 3 ) ) + 1 ; store memo [ n ] and return ; This function mainly initializes memo [ ] and calls getMinSteps ( n , memo ) ; initialize memoized array ; Driver Code
< ? php function getMinSteps ( $ n , $ memo ) { if ( $ n == 1 ) return 0 ; if ( $ memo [ $ n ] != -1 ) return $ memo [ $ n ] ; $ res = getMinSteps ( $ n - 1 , $ memo ) ; if ( $ n % 2 == 0 ) $ res = min ( $ res , getMinSteps ( $ n / 2 , $ memo ) ) ; if ( $ n % 3 == 0 ) $ res = min ( $ res , getMinSteps ( $ n / 3 , $ memo ) ) ; $ memo [ $ n ] = 1 + $ res ; return $ memo [ $ n ] ; } function g_etMinSteps ( $ n ) { $ memo = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ memo [ $ i ] = -1 ; return getMinSteps ( $ n , $ memo ) ; } $ n = 10 ; echo g_etMinSteps ( $ n ) ; ? >
Smallest length string with repeated replacement of two distinct adjacent | Returns smallest possible length with given operation allowed . ; Counint occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; If all characters are same . ; If all characters are present even number of times or all are present odd number of times . ; Answer is 1 for all other cases . ; Driver code
< ? php function stringReduction ( $ str ) { $ n = strlen ( $ str ) ; $ count = array_fill ( 0 , 3 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; if ( $ count [ 0 ] == $ n $ count [ 1 ] == $ n $ count [ 2 ] == $ n ) return $ n ; if ( ( $ count [ 0 ] % 2 ) == ( $ count [ 1 ] % 2 ) && ( $ count [ 1 ] % 2 ) == ( $ count [ 2 ] % 2 ) ) return 2 ; return 1 ; } $ str = " abcbbaacb " ; print ( stringReduction ( $ str ) ) ; ? >
Number of subsequences of the form a ^ i b ^ j c ^ k | Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of ' a ' ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of ' b ' ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' . ; Traverse all characters of given string ; If current character is ' a ' , then there are following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences . c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not part of  bCount subsequences.  ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not part of  cCount subsequences.  ; Driver Code
< ? php function countSubsequences ( $ s ) { $ aCount = 0 ; $ bCount = 0 ; $ cCount = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' ) $ aCount = ( 1 + 2 * $ aCount ) ; else if ( $ s [ $ i ] == ' b ' ) $ bCount = ( $ aCount + 2 * $ bCount ) ; else if ( $ s [ $ i ] == ' c ' ) $ cCount = ( $ bCount + 2 * $ cCount ) ; } return $ cCount ; } $ s = " abbc " ; echo countSubsequences ( $ s ) ; ? >
Highway Billboard Problem | PHP program to find maximum revenue by placing billboard on the highway with given constraints . ; Array to store maximum revenue at each miles . ; actual minimum distance between 2 billboards . ; check if all billboards are already placed . ; check if we have billboard for that particular mile . If not , copy the previous maximum revenue . ; we do have billboard for this mile . ; If current position is less than or equal to t , then we can have only one billboard . ; Else we may have to remove previously placed billboard ; Driver Code
< ? php function maxRevenue ( $ m , $ x , $ revenue , $ n , $ t ) { $ maxRev = array_fill ( 0 , $ m + 1 , false ) ; $ nxtbb = 0 ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { if ( $ nxtbb < $ n ) { if ( $ x [ $ nxtbb ] != $ i ) $ maxRev [ $ i ] = $ maxRev [ $ i - 1 ] ; else { if ( $ i <= $ t ) $ maxRev [ $ i ] = max ( $ maxRev [ $ i - 1 ] , $ revenue [ $ nxtbb ] ) ; else $ maxRev [ $ i ] = max ( $ maxRev [ $ i - $ t - 1 ] + $ revenue [ $ nxtbb ] , $ maxRev [ $ i - 1 ] ) ; $ nxtbb ++ ; } } else $ maxRev [ $ i ] = $ maxRev [ $ i - 1 ] ; } return $ maxRev [ $ m ] ; } $ m = 20 ; $ x = array ( 6 , 7 , 12 , 13 , 14 ) ; $ revenue = array ( 5 , 6 , 5 , 3 , 1 ) ; $ n = sizeof ( $ x ) ; $ t = 5 ; echo maxRevenue ( $ m , $ x , $ revenue , $ n , $ t ) ; ? >
Finding the maximum square sub | Java program to find maximum K such that K x K is a submatrix with equal elements . ; Returns size of the largest square sub - matrix with all same elements . ; If elements is at top row or first column , it wont form a square matrix 's   bottom-right ; Check if adjacent elements are equal ; If not equal , then it will form a 1 x1 submatrix ; Update result at each ( i , j ) ; Driver Code
< ? php $ Row = 6 ; $ Col = 6 ; function largestKSubmatrix ( & $ a ) { global $ Row , $ Col ; $ result = 0 ; for ( $ i = 0 ; $ i < $ Row ; $ i ++ ) { for ( $ j = 0 ; $ j < $ Col ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) $ dp [ $ i ] [ $ j ] = 1 ; else { if ( $ a [ $ i ] [ $ j ] == $ a [ $ i - 1 ] [ $ j ] && $ a [ $ i ] [ $ j ] == $ a [ $ i ] [ $ j - 1 ] && $ a [ $ i ] [ $ j ] == $ a [ $ i - 1 ] [ $ j - 1 ] ) $ dp [ $ i ] [ $ j ] = min ( min ( $ dp [ $ i - 1 ] [ $ j ] , $ dp [ $ i ] [ $ j - 1 ] ) , $ dp [ $ i - 1 ] [ $ j - 1 ] ) + 1 ; else $ dp [ $ i ] [ $ j ] = 1 ; } $ result = max ( $ result , $ dp [ $ i ] [ $ j ] ) ; } } return $ result ; } $ a = array ( array ( 2 , 2 , 3 , 3 , 4 , 4 ) , array ( 5 , 5 , 7 , 7 , 7 , 4 ) , array ( 1 , 2 , 7 , 7 , 7 , 4 ) , array ( 4 , 4 , 7 , 7 , 7 , 4 ) , array ( 5 , 5 , 5 , 1 , 2 , 7 ) , array ( 8 , 7 , 9 , 4 , 4 , 4 ) ) ; echo largestKSubmatrix ( $ a ) ; ? >
Probability of getting at least K heads in N tosses of Coins | Naive approach in PHP to find probability of at least k heads ; Returns probability of getting at least k heads in n tosses . ; Probability of getting exactly i heads out of n heads ; Note : 1 << n = pow ( 2 , n ) ; Preprocess all factorial only upto 19 , as after that it will overflow ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 12 head out of 18 coins
< ? php $ MAX = 21 ; $ fact = array_fill ( 0 , $ MAX , 0 ) ; function probability ( $ k , $ n ) { global $ fact ; $ ans = 0 ; for ( $ i = $ k ; $ i <= $ n ; ++ $ i ) $ ans += $ fact [ $ n ] / ( $ fact [ $ i ] * $ fact [ $ n - $ i ] ) ; $ ans = $ ans / ( 1 << $ n ) ; return $ ans ; } function precompute ( ) { global $ fact ; $ fact [ 0 ] = $ fact [ 1 ] = 1 ; for ( $ i = 2 ; $ i < 20 ; ++ $ i ) $ fact [ $ i ] = $ fact [ $ i - 1 ] * $ i ; } precompute ( ) ; echo number_format ( probability ( 2 , 3 ) , 6 ) . " STRNEWLINE " ; echo number_format ( probability ( 3 , 6 ) , 6 ) . " STRNEWLINE " ; echo number_format ( probability ( 12 , 18 ) , 6 ) ; ? >