codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of special permutations | Function to return the number of ways to choose r objects out of n objects ; Function to return the number of derangements of n ; Function to return the required number of permutations ; Ways to choose i indices from n indices ; Dearangements of ( n - i ) indices ; Driver Code
< ? php function nCr ( $ n , $ r ) { $ ans = 1 ; if ( $ r > $ n - $ r ) $ r = $ n - $ r ; for ( $ i = 0 ; $ i < $ r ; $ i ++ ) { $ ans *= ( $ n - $ i ) ; $ ans /= ( $ i + 1 ) ; } return $ ans ; } function countDerangements ( $ n ) { $ der = array ( $ n + 1 ) ; $ der [ 0 ] = 1 ; $ der [ 1 ] = 0 ; $ der [ 2 ] = 1 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) $ der [ $ i ] = ( $ i - 1 ) * ( $ der [ $ i - 1 ] + $ der [ $ i - 2 ] ) ; return $ der [ $ n ] ; } function countPermutations ( $ n , $ k ) { $ ans = 0 ; for ( $ i = $ n - $ k ; $ i <= $ n ; $ i ++ ) { $ ways = nCr ( $ n , $ i ) ; $ ans += $ ways * countDerangements ( $ n - $ i ) ; } return $ ans ; } $ n = 5 ; $ k = 3 ; echo ( countPermutations ( $ n , $ k ) ) ; ? >
Ways to place K bishops on an NÃ — N chessboard so that no two attack | returns the number of squares in diagonal i ; returns the number of ways to fill a n * n chessboard with k bishops so that no two bishops attack each other . ; return 0 if the number of valid places to be filled is less than the number of bishops ; dp table to store the values ; Setting the base conditions ; calculate the required number of ways ; stores the answer ; Driver code
< ? php function squares ( $ i ) { if ( ( $ i & 1 ) == 1 ) return intval ( $ i / 4 ) * 2 + 1 ; else return intval ( ( $ i - 1 ) / 4 ) * 2 + 2 ; } function bishop_placements ( $ n , $ k ) { if ( $ k > 2 * $ n - 1 ) return 0 ; $ dp = array_fill ( 0 , $ n * 2 , array_fill ( 0 , $ k + 1 , NULL ) ) ; for ( $ i = 0 ; $ i < $ n * 2 ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; $ dp [ 1 ] [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ n * 2 ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ k ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 2 ] [ $ j ] + $ dp [ $ i - 2 ] [ $ j - 1 ] * ( squares ( $ i ) - $ j + 1 ) ; } $ ans = 0 ; for ( $ i = 0 ; $ i <= $ k ; $ i ++ ) { $ ans += $ dp [ $ n * 2 - 1 ] [ $ i ] * $ dp [ $ n * 2 - 2 ] [ $ k - $ i ] ; } return $ ans ; } $ n = 2 ; $ k = 2 ; $ ans = bishop_placements ( $ n , $ k ) ; echo $ ans ; ? >
Total number of different staircase that can made from N boxes | Function to find the total number of different staircase that can made from N boxes ; Initialize all the elements of the table to zero ; Base case ; When step is equal to 2 ; When step is greater than 2 ; Count the total staircase from all the steps ; Driver Code
< ? php function countStaircases ( $ N ) { for ( $ i = 0 ; $ i <= $ N ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ N ; $ j ++ ) { $ memo [ $ i ] [ $ j ] = 0 ; } } $ memo [ 3 ] [ 2 ] = $ memo [ 4 ] [ 2 ] = 1 ; for ( $ i = 5 ; $ i <= $ N ; $ i ++ ) { for ( $ j = 2 ; $ j <= $ i ; $ j ++ ) { if ( $ j == 2 ) { $ memo [ $ i ] [ $ j ] = $ memo [ $ i - $ j ] [ $ j ] + 1 ; } else { $ memo [ $ i ] [ $ j ] = $ memo [ $ i - $ j ] [ $ j ] + $ memo [ $ i - $ j ] [ $ j - 1 ] ; } } } $ answer = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ answer = $ answer + $ memo [ $ N ] [ $ i ] ; return $ answer ; } $ N = 7 ; echo countStaircases ( $ N ) ; ? >
Count the number of ways to traverse a Matrix | Returns The number of way from top - left to mat [ m - 1 ] [ n - 1 ] ; Return 1 if it is the first row or first column ; Recursively find the no of way to reach the last cell . ; Driver code
< ? php function countPaths ( $ m , $ n ) { if ( $ m == 1 $ n == 1 ) return 1 ; return countPaths ( $ m - 1 , $ n ) + countPaths ( $ m , $ n - 1 ) ; } $ n = 5 ; $ m = 5 ; echo countPaths ( $ n , $ m ) ; ? >
Count the number of ways to traverse a Matrix | Returns The number of way from top - left to mat [ m - 1 ] [ n - 1 ] ; Driver code
< ? php function countPaths ( $ m , $ n ) { $ dp ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 1 $ j == 1 ) $ dp [ $ i ] [ $ j ] = 1 ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i ] [ $ j - 1 ] ; } } return $ dp [ $ m ] [ $ n ] ; } $ n = 5 ; $ m = 5 ; echo countPaths ( $ n , $ m ) ; ? >
Number of ways a convex polygon of n + 2 sides can split into triangles by connecting vertices | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * $ n , $ n ) ; return $ c / ( $ n + 1 ) ; } $ n = 3 ; echo catalan ( $ n ) ; ? >
Alternate Fibonacci Numbers | Alternate Fibonacci Series using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Driver Code
< ? php function alternateFib ( $ n ) { if ( $ n < 0 ) return ; $ f1 = 0 ; $ f2 = 1 ; echo $ f1 . " " ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ f3 = $ f2 + $ f1 ; if ( $ i % 2 == 0 ) echo $ f3 . " " ; $ f1 = $ f2 ; $ f2 = $ f3 ; } } $ N = 15 ; alternateFib ( $ N ) ; ? >
Number of ways to form an array with distinct adjacent elements | Returns the total ways to form arrays such that every consecutive element is different and each element except the first and last can take values from 1 to M ; define the dp [ ] [ ] array ; if the first element is 1 ; there is only one way to place a 1 at the first index ; the value at first index needs to be 1 , thus there is no way to place a non - one integer ; if the first element was 1 then at index 1 , only non one integer can be placed thus there are M - 1 ways to place a non one integer at index 2 and 0 ways to place a 1 at the 2 nd index ; Else there is one way to place a one at index 2 and if a non one needs to be placed here , there are ( M - 2 ) options , i . e neither the element at this index should be 1 , neither should it be equal to the previous element ; Build the dp array in bottom up manner ; f ( i , one ) = f ( i - 1 , non - one ) ; f ( i , non - one ) = f ( i - 1 , one ) * ( M - 1 ) + f ( i - 1 , non - one ) * ( M - 2 ) ; last element needs to be one , so return dp [ n - 1 ] [ 0 ] ; Driver Code
< ? php function totalWays ( $ N , $ M , $ X ) { $ dp = array ( array ( ) ) ; if ( $ X == 1 ) { $ dp [ 0 ] [ 0 ] = 1 ; } else { $ dp [ 0 ] [ 1 ] = 0 ; } if ( $ X == 1 ) { $ dp [ 1 ] [ 0 ] = 0 ; $ dp [ 1 ] [ 1 ] = $ M - 1 ; } else { $ dp [ 1 ] [ 0 ] = 1 ; $ dp [ 1 ] [ 1 ] = ( $ M - 2 ) ; } for ( $ i = 2 ; $ i < $ N ; $ i ++ ) { $ dp [ $ i ] [ 0 ] = $ dp [ $ i - 1 ] [ 1 ] ; $ dp [ $ i ] [ 1 ] = $ dp [ $ i - 1 ] [ 0 ] * ( $ M - 1 ) + $ dp [ $ i - 1 ] [ 1 ] * ( $ M - 2 ) ; } return $ dp [ $ N - 1 ] [ 0 ] ; } $ N = 4 ; $ M = 3 ; $ X = 2 ; echo totalWays ( $ N , $ M , $ X ) ; ? >
Memoization ( 1D , 2D and 3D ) | Fibonacci Series using Recursion ; Base case ; recursive calls ; Driver Code
< ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; } $ n = 6 ; echo fib ( $ n ) ; ? >
Sum of product of consecutive Binomial Coefficients | PHP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient up to nth term ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; Driver Code
< ? php $ MAX = 100 ; function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , ( $ k + 1 ) , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function sumOfproduct ( $ n ) { return binomialCoeff ( 2 * $ n , $ n - 1 ) ; } $ n = 3 ; echo sumOfproduct ( $ n ) ; ? >
Print Fibonacci sequence using 2 variables | Simple php Program to print Fibonacci sequence ; Driver code
< ? php function fib ( $ n ) { $ a = 0 ; $ b = 1 ; $ c ; if ( $ n >= 0 ) echo $ a , " ▁ " ; if ( $ n >= 1 ) echo $ b , " ▁ " ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ c = $ a + $ b ; echo $ c , " " ; $ a = $ b ; $ b = $ c ; } } fib ( 9 ) ; ? >
Maximum sum increasing subsequence from a prefix and a given element after prefix is must | PHP program to find maximum sum increasing subsequence till i - th index and including k - th index . ; Initializing the first row of the dp [ ] [ ] . ; Creating the dp [ ] [ ] matrix . ; To calculate for i = 4 and k = 6. ; Driver Code
< ? php function pre_compute ( & $ a , $ n , $ index , $ k ) { $ dp = array_fill ( 0 , $ n , array_fill ( 0 , $ n , NULL ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ a [ 0 ] ) $ dp [ 0 ] [ $ i ] = $ a [ $ i ] + $ a [ 0 ] ; else $ dp [ 0 ] [ $ i ] = $ a [ $ i ] ; } for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ a [ $ j ] > $ a [ $ i ] && $ j > $ i ) { if ( ( $ dp [ $ i - 1 ] [ $ i ] + $ a [ $ j ] ) > $ dp [ $ i - 1 ] [ $ j ] ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ i ] + $ a [ $ j ] ; else $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] ; } else $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] ; } } return $ dp [ $ index ] [ $ k ] ; } $ a = array ( 1 , 101 , 2 , 3 , 100 , 4 , 5 ) ; $ n = sizeof ( $ a ) ; $ index = 4 ; $ k = 6 ; echo pre_compute ( $ a , $ n , $ index , $ k ) ; ? >
Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code
< ? php function gen ( $ n ) { $ S = array ( ) ; $ S [ 0 ] = 0 ; $ S [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ S [ $ i ] = 4 * $ S [ $ i / 2 ] ; else $ S [ $ i ] = 4 * $ S [ $ i / 2 ] + 1 ; } return $ S [ $ n ] ; } function moserDeBruijn ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo gen ( $ i ) , " ▁ " ; echo " STRNEWLINE " ; } $ n = 15 ; echo " First ▁ " , $ n , " ▁ terms ▁ of ▁ " , " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ STRNEWLINE " ; moserDeBruijn ( $ n ) ; ? >
Longest Common Substring ( Space optimized DP solution ) | Function to find longest common substring . ; Find length of both the strings . ; Variable to store length of longest common substring . ; Matrix to store result of two consecutive rows at a time . ; Variable to represent which row of matrix is current row . ; For a particular value of i and j , len [ currRow ] [ j ] stores length of longest common substring in string X [ 0. . i ] and Y [ 0. . j ] . ; Make current row as previous row and previous row as new current row . ; Driver Code
< ? php function LCSubStr ( $ X , $ Y ) { $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; $ result = 0 ; $ len = array ( array ( ) , array ( ) , ) ; $ currRow = 0 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) { $ len [ $ currRow ] [ $ j ] = 0 ; } else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) { $ len [ $ currRow ] [ $ j ] = $ len [ 1 - $ currRow ] [ $ j - 1 ] + 1 ; $ result = max ( $ result , $ len [ $ currRow ] [ $ j ] ) ; } else { $ len [ $ currRow ] [ $ j ] = 0 ; } } $ currRow = 1 - $ currRow ; } return $ result ; } $ X = " GeeksforGeeks " ; $ Y = " GeeksQuiz " ; print ( LCSubStr ( $ X , $ Y ) ) ; ? >
Minimal moves to form a string by adding characters or appending string itself | function to return the minimal number of moves ; initializing dp [ i ] to INT_MAX ; initialize both strings to null ; base case ; check if it can be appended ; addition of character takes one step ; appending takes 1 step , and we directly reach index i * 2 + 1 after appending so the number of steps is stord in i * 2 + 1 ; Driver Code ; function call to return minimal number of moves
< ? php function minimalSteps ( $ s , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ dp [ $ i ] = PHP_INT_MAX ; $ s1 = " " ; $ s2 = " " ; $ dp [ 0 ] = 1 ; $ s1 = $ s1 . $ s [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ s1 = $ s1 . $ s [ $ i ] ; $ s2 = substr ( $ s , $ i + 1 , $ i + 1 ) ; $ dp [ $ i ] = min ( $ dp [ $ i ] , $ dp [ $ i - 1 ] + 1 ) ; if ( $ s1 == $ s2 ) $ dp [ $ i * 2 + 1 ] = min ( $ dp [ $ i ] + 1 , $ dp [ $ i * 2 + 1 ] ) ; } return $ dp [ $ n - 1 ] ; } $ s = " aaaaaaaa " ; $ n = strlen ( $ s ) ; echo minimalSteps ( $ s , $ n ) ; ? >
Golomb sequence | Print the first n term of Golomb Sequence ; base cases ; Finding and printing first n terms of Golomb Sequence . ; Driver Code
< ? php function printGolomb ( $ n ) { $ dp [ 1 ] = 1 ; echo $ dp [ 1 ] , " " ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] = 1 + $ dp [ $ i - $ dp [ $ dp [ $ i - 1 ] ] ] ; echo $ dp [ $ i ] , " " ; } } $ n = 9 ; printGolomb ( $ n ) ; ? >
Maximum difference of zeros and ones in binary string | Set 2 ( O ( n ) time ) | Returns the length of substring with maximum difference of zeroes and ones in binary string ; traverse a binary string from left to right ; add current value to the current_sum according to the Character if it ' s ▁ ' 0 ' add 1 else -1 ; update maximum sum ; return - 1 if string does not contain any zero that means all ones otherwise max_sum ; Driver Code
< ? php function findLength ( $ str , $ n ) { $ current_sum = 0 ; $ max_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ current_sum += ( $ str [ $ i ] == '0' ? 1 : -1 ) ; if ( $ current_sum < 0 ) $ current_sum = 0 ; $ max_sum = max ( $ current_sum , $ max_sum ) ; } return $ max_sum == 0 ? -1 : $ max_sum ; } $ s = "11000010001" ; $ n = 11 ; echo findLength ( $ s , $ n ) , " STRNEWLINE " ; ? >
Number of decimal numbers of length k , that are strict monotone | PHP program to count numbers of k digits that are strictly monotone . ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits ( 1 , 2 , 3 , . .9 ) ; Unit length numbers ; Building dp [ ] in bottom up ; Driver code
< ? php $ DP_s = 9 ; function getNumStrictMonotone ( $ len ) { global $ DP_s ; $ DP = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = 0 ; $ j < $ DP_s ; $ j ++ ) $ DP [ $ i ] [ $ j ] = 0 ; } for ( $ i = 0 ; $ i < $ DP_s ; ++ $ i ) $ DP [ 0 ] [ $ i ] = $ i + 1 ; for ( $ i = 1 ; $ i < $ len ; ++ $ i ) for ( $ j = 1 ; $ j < $ DP_s ; ++ $ j ) $ DP [ $ i ] [ $ j ] = $ DP [ $ i - 1 ] [ $ j - 1 ] + $ DP [ $ i ] [ $ j - 1 ] ; return $ DP [ $ len - 1 ] [ $ DP_s - 1 ] ; } echo ( getNumStrictMonotone ( 2 ) ) ; ? >
Count ways to divide circle using N non | PHP code to count ways to divide circle using N non - intersecting chords . ; n = no of points required ; dp array containing the sum ; returning the required number ; Driver Code
< ? php function chordCnt ( $ A ) { $ n = 2 * $ A ; $ dpArray = array_fill ( 0 , $ n + 1 , 0 ) ; $ dpArray [ 0 ] = 1 ; $ dpArray [ 2 ] = 1 ; for ( $ i = 4 ; $ i <= $ n ; $ i += 2 ) { for ( $ j = 0 ; $ j < $ i - 1 ; $ j += 2 ) { $ dpArray [ $ i ] += ( $ dpArray [ $ j ] * $ dpArray [ $ i - 2 - $ j ] ) ; } } return $ dpArray [ $ n ] ; } $ N = 2 ; echo chordCnt ( $ N ) , " STRNEWLINE " ; $ N = 1 ; echo chordCnt ( $ N ) , " STRNEWLINE " ; $ N = 4 ; echo chordCnt ( $ N ) , " STRNEWLINE " ; ? >
Check for possible path in 2D matrix | PHP program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; set arr [ 0 ] [ 0 ] = 1 ; Mark reachable ( from top left ) nodes in first row and first column . ; Mark reachable nodes in remaining matrix . ; return yes if right bottom index is 1 ; Given array ; path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
< ? php $ row = 5 ; $ col = 5 ; function isPath ( $ arr ) { global $ row , $ col ; $ arr [ 0 ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ row ; $ i ++ ) if ( $ arr [ $ i ] [ 0 ] != -1 ) $ arr [ $ i ] [ 0 ] = $ arr [ $ i - 1 ] [ 0 ] ; for ( $ j = 1 ; $ j < $ col ; $ j ++ ) if ( $ arr [ 0 ] [ $ j ] != -1 ) $ arr [ 0 ] [ $ j ] = $ arr [ 0 ] [ $ j - 1 ] ; for ( $ i = 1 ; $ i < $ row ; $ i ++ ) for ( $ j = 1 ; $ j < $ col ; $ j ++ ) if ( $ arr [ $ i ] [ $ j ] != -1 ) $ arr [ $ i ] [ $ j ] = max ( $ arr [ $ i ] [ $ j - 1 ] , $ arr [ $ i - 1 ] [ $ j ] ) ; return ( $ arr [ $ row - 1 ] [ $ col - 1 ] == 1 ) ; } $ arr = array ( array ( 0 , 0 , 0 , 1 , 0 ) , array ( -1 , 0 , 0 , -1 , -1 ) , array ( 0 , 0 , 0 , -1 , 0 ) , array ( -1 , 0 , -1 , 0 , -1 ) , array ( 0 , 0 , -1 , 0 , 0 ) ) ; if ( isPath ( $ arr ) ) echo " Yes " ; else echo " No " ; ? >
Newmanâ €“ Shanksâ €“ Williams prime | return nth Newman a Shanks a Williams prime ; Base case ; Recursive step ; Driver Code
< ? php function nswp ( $ n ) { if ( $ n == 0 $ n == 1 ) return 1 ; return 2 * nswp ( $ n - 1 ) + nswp ( $ n - 2 ) ; } $ n = 3 ; echo ( nswp ( $ n ) ) ; ? >
Newman Shanks Williams prime | return nth Newman Shanks Williams prime ; Base case ; Finding nth Newman Shanks Williams prime ; Driver Code
< ? php function nswp ( $ n ) { $ dp [ 0 ] = $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = 2 * $ dp [ $ i - 1 ] + $ dp [ $ i - 2 ] ; return $ dp [ $ n ] ; } $ n = 3 ; echo ( nswp ( $ n ) ) ; ? >
Minimum cost to make two strings identical by deleting the digits | Function to returns cost of removing the identical characters in LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains cost of removing identical characters in LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; If both characters are same , add both of them ; Otherwise find the maximum cost among them ; Returns cost of making X [ ] and Y [ ] identical ; Find LCS of X [ ] and Y [ ] ; Initialize the cost variable ; Find cost of all characters in both strings ; Driver code
< ? php function lcs ( $ X , $ Y , $ m , $ n ) { $ L = array ( $ m + 1 , $ n + 1 ) ; for ( $ i = 0 ; $ i <= $ m ; ++ $ i ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) $ L [ $ i ] [ $ j ] = 0 ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) $ L [ $ i ] [ $ j ] = $ L [ $ i - 1 ] [ $ j - 1 ] + 2 * ( $ X [ $ i - 1 ] - '0' ) ; else $ L [ $ i ] [ $ j ] = $ L [ $ i - 1 ] [ $ j ] > $ L [ $ i ] [ $ j - 1 ] ? $ L [ $ i - 1 ] [ $ j ] : $ L [ $ i ] [ $ j - 1 ] ; } } return $ L [ $ m ] [ $ n ] ; } function findMinCost ( $ X , $ Y ) { $ m = sizeof ( $ X ) ; $ n = sizeof ( $ Y ) ; $ cost = 0 ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) $ cost += $ X [ $ i ] - '0' ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ cost += $ Y [ $ i ] - '0' ; return $ cost - lcs ( $ X , $ Y , $ m , $ n ) ; } $ X = str_split ( "3759" ) ; $ Y = str_split ( "9350" ) ; echo ( " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings " . " identical is = "
Given a large number , check if a subsequence of digits is divisible by 8 | Function takes in an array of numbers , dynamically goes on the location and makes combination of numbers . ; Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 ) ; If we consider the number in our combination , we add it to the previous combination ; If we exclude the number from our combination ; If at dp [ i ] [ 0 ] , we find value 1 / true , it shows that the number exists at the value of ' i ' ; Driver Code
< ? php function isSubSeqDivisible ( $ str ) { $ n = strlen ( $ str ) ; $ dp = array_fill ( 0 , $ n + 1 , array_fill ( 0 , 10 , NULL ) ) ; $ arr = array_fill ( 0 , ( $ n + 1 ) , NULL ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ arr [ $ i ] = $ str [ $ i - 1 ] - '0' ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] [ $ arr [ $ i ] % 8 ] = 1 ; for ( $ j = 0 ; $ j < 8 ; $ j ++ ) { if ( $ dp [ $ i - 1 ] [ $ j ] > $ dp [ $ i ] [ ( $ j * 10 + $ arr [ $ i ] ) % 8 ] ) $ dp [ $ i ] [ ( $ j * 10 + $ arr [ $ i ] ) % 8 ] = $ dp [ $ i - 1 ] [ $ j ] ; if ( $ dp [ $ i - 1 ] [ $ j ] > $ dp [ $ i ] [ $ j ] ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] ; } } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ dp [ $ i ] [ 0 ] == 1 ) return true ; } return false ; } $ str = "3144" ; if ( isSubSeqDivisible ( $ str ) ) echo " Yes " ; else echo " No " ; ? >
Length of Longest Balanced Subsequence | PHP program to find length of the longest balanced subsequence ; Considering all balanced substrings of length 2 ; Considering all other substrings ; Driver Code
< ? php function maxLength ( $ s , $ n ) { $ dp = array_fill ( 0 , $ n , array_fill ( 0 , $ n , NULL ) ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ s [ $ i ] == ' ( ' && $ s [ $ i + 1 ] == ' ) ' ) $ dp [ $ i ] [ $ i + 1 ] = 2 ; for ( $ l = 2 ; $ l < $ n ; $ l ++ ) { for ( $ i = 0 , $ j = $ l ; $ j < $ n ; $ i ++ , $ j ++ ) { if ( $ s [ $ i ] == ' ( ' && $ s [ $ j ] == ' ) ' ) $ dp [ $ i ] [ $ j ] = 2 + $ dp [ $ i + 1 ] [ $ j - 1 ] ; for ( $ k = $ i ; $ k < $ j ; $ k ++ ) $ dp [ $ i ] [ $ j ] = max ( $ dp [ $ i ] [ $ j ] , $ dp [ $ i ] [ $ k ] + $ dp [ $ k + 1 ] [ $ j ] ) ; } } return $ dp [ 0 ] [ $ n - 1 ] ; } $ s = " ( ) ( ( ( ( ( ( ) " ; $ n = strlen ( $ s ) ; echo maxLength ( $ s , $ n ) . " STRNEWLINE " ; ? >
Smallest sum contiguous subarray | function to find the smallest sum contiguous subarray ; to store the minimum value that is ending up to the current index ; to store the minimum value encountered so far ; traverse the array elements ; if min_ending_here > 0 , then it could not possibly contribute to the minimum sum further ; else add the value arr [ i ] to min_ending_here ; update min_so_far ; required smallest sum contiguous subarray value ; Driver Code
< ? php function smallestSumSubarr ( $ arr , $ n ) { $ min_ending_here = 999999 ; $ min_so_far = 999999 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ min_ending_here > 0 ) $ min_ending_here = $ arr [ $ i ] ; else $ min_ending_here += $ arr [ $ i ] ; $ min_so_far = min ( $ min_so_far , $ min_ending_here ) ; } return $ min_so_far ; } $ arr = array ( 3 , -4 , 2 , -3 , -1 , 7 , -5 ) ; $ n = count ( $ arr ) ; echo " Smallest ▁ sum : ▁ " . smallestSumSubarr ( $ arr , $ n ) ; ? >
Painting Fence Algorithm | Returns count of ways to color k posts using k colors ; There are k ways to color first post ; There are 0 ways for single post to violate ( same color_ and k ways to not violate ( different color ) ; Fill for 2 posts onwards ; Current same is same as previous diff ; We always have k - 1 choices for next post ; Total choices till i . ; Driver code
< ? php function countWays ( $ n , $ k ) { $ total = $ k ; $ mod = 1000000007 ; $ same = 0 ; $ diff = $ k ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ same = $ diff ; $ diff = $ total * ( $ k - 1 ) ; $ diff = $ diff % $ mod ; $ total = ( $ same + $ diff ) % $ mod ; } return $ total ; } $ n = 3 ; $ k = 2 ; echo countWays ( $ n , $ k ) . " STRNEWLINE " ; ? >
Sum of all substrings of a string representing a number | Set 2 ( Constant Extra Space ) | Returns sum of all substring of num ; Initialize result ; Here traversing the array in reverse order . Initializing loop from last element . mf is multiplying factor . ; Each time sum is added to its previous sum . Multiplying the three factors as explained above . s [ i ] - '0' is done to convert char to int . ; Making new multiplying factor as explained above . ; Driver Code
< ? php function sumOfSubstrings ( $ num ) { $ sum = 0 ; $ mf = 1 ; for ( $ i = strlen ( $ num ) - 1 ; $ i >= 0 ; $ i -- ) { $ sum += ( $ num [ $ i ] - '0' ) * ( $ i + 1 ) * $ mf ; $ mf = $ mf * 10 + 1 ; } return $ sum ; } $ num = "6759" ; echo sumOfSubstrings ( $ num ) , " STRNEWLINE " ; ? >