codeparrot/xlcost-text-to-code huggingface dataset:
Count palindromic characteristics of a String | PHP program which counts different palindromic characteristics of a string . ; function which checks whether a substr [ i . . j ] of a given is a palindrome or not . ; P [ i , j ] = true if substr [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill P table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; function which recursively counts if a str [ i . . j ] is a k - palindromic or not . ; terminating condition for a which is a k - palindrome . ; terminating condition for a which is not a k - palindrome . ; increases the counter for the if it is a k - palindrome . ; mid is middle pointer of the str [ i ... j ] . ; if length of which is ( j - i + 1 ) is odd than we have to subtract one from mid else if even then no change . ; if the is k - palindrome then we check if it is a ( k + 1 ) - palindrome or not by just sending any of one half of the to the Count_k_Palindrome function . ; Finding all palindromic substrings of given string ; counting k - palindromes for each and every sub of given string . . ; Output the number of K - palindromic substrings of a given string . ; Driver code
< ? php $ MAX_STR_LEN = 1000 ; $ P = array ( array ( ) ) ; $ Kpal = array_fill ( 0 , $ MAX_STR_LEN , 0 ) ; for ( $ i = 0 ; $ i < $ MAX_STR_LEN ; $ i ++ ) { for ( $ j = 0 ; $ j < $ MAX_STR_LEN ; $ j ++ ) $ P [ $ i ] [ $ j ] = false ; } function checkSubStrPal ( $ str , $ n ) { global $ P , $ Kpal , $ MAX_STR_LEN ; for ( $ i = 0 ; $ i < $ MAX_STR_LEN ; $ i ++ ) { for ( $ j = 0 ; $ j < $ MAX_STR_LEN ; $ j ++ ) $ P [ $ i ] [ $ j ] = false ; $ Kpal [ $ i ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ P [ $ i ] [ $ i ] = true ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ str [ $ i ] == $ str [ $ i + 1 ] ) $ P [ $ i ] [ $ i + 1 ] = true ; for ( $ gap = 2 ; $ gap < $ n ; $ gap ++ ) { for ( $ i = 0 ; $ i < $ n - $ gap ; $ i ++ ) { $ j = $ gap + $ i ; if ( $ str [ $ i ] == $ str [ $ j ] && $ P [ $ i + 1 ] [ $ j - 1 ] ) $ P [ $ i ] [ $ j ] = true ; } } } function countKPalindromes ( $ i , $ j , $ k ) { global $ Kpal , $ P ; if ( $ i == $ j ) { $ Kpal [ $ k ] ++ ; return ; } if ( $ P [ $ i ] [ $ j ] == false ) return ; $ Kpal [ $ k ] ++ ; $ mid = ( $ i + $ j ) / 2 ; if ( ( $ j - $ i + 1 ) % 2 == 1 ) $ mid -- ; countKPalindromes ( $ i , $ mid , $ k + 1 ) ; } function printKPalindromes ( $ s ) { global $ P , $ Kpal , $ MAX_STR_LEN ; $ n = strlen ( $ s ) ; checkSubStrPal ( $ s , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n - $ i ; $ j ++ ) countKPalindromes ( $ j , $ j + $ i , 1 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo ( $ Kpal [ $ i ] . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } $ s = " abacaba " ; printKPalindromes ( $ s ) ; ? >
Grouping Countries | PHP program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; someone gives different answer ; check next person ; one valid country group has been found ; Driver Code
< ? php function countCountries ( $ ans , $ N ) { $ total_countries = 0 ; $ i = 0 ; $ invalid = false ; while ( $ i < $ N ) { $ curr_size = $ ans [ $ i ] ; $ num = $ ans [ $ i ] ; while ( $ num > 0 ) { if ( $ ans [ $ i ] != $ curr_size ) { echo " Invalid ▁ Answer STRNEWLINE " ; return ; } else $ num -- ; $ i ++ ; } $ total_countries ++ ; } echo " There ▁ are ▁ " , $ total_countries , " ▁ distinct ▁ companies ▁ in ▁ the ▁ group . STRNEWLINE " ; } $ ans = array ( 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ) ; $ n = sizeof ( $ ans ) ; countCountries ( $ ans , $ n ) ; ? >
Check if an array contains all elements of a given range | Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present
< ? php function check_elements ( $ arr , $ n , $ A , $ B ) { $ range = $ B - $ A ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( abs ( $ arr [ $ i ] ) >= $ z = abs ( $ arr [ $ i ] ) - $ A ; if ( $ arr [ $ z ] > 0 ) { $ arr [ $ z ] = $ arr [ $ z ] * -1 ; } } } $ A && abs ( $ arr [ $ i ] ) <= $ B ) { $ count = 0 ; for ( $ i = 0 ; $ i <= $ range && $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) return -1 ; else $ count ++ ; } if ( $ count != ( $ range + 1 ) ) return -1 ; return true ; } $ arr = array ( 1 , 4 , 5 , 2 , 7 , 8 , 3 ) ; $ n = sizeof ( $ arr ) ; $ A = 2 ; $ B = 5 ; if ( ( check_elements ( $ arr , $ n , $ A , $ B ) ) == true ) echo " Yes " ; else echo " No " ; ? >
Recursive Programs to find Minimum and Maximum elements of array | function to print Minimum element using recursion ; if size = 0 means whole array has been traversed ; Driver Code
< ? php function findMinRec ( $ A , $ n ) { if ( $ n == 1 ) return $ A [ 0 ] ; return min ( $ A [ $ n - 1 ] , findMinRec ( $ A , $ n - 1 ) ) ; } $ A = array ( 1 , 4 , 45 , 6 , -50 , 10 , 2 ) ; $ n = sizeof ( $ A ) ; echo findMinRec ( $ A , $ n ) ; ? >
Allocate minimum number of pages | Utility function to check if current minimum value is feasible or not . ; iterate over all books ; check if current number of pages are greater than curr_min that means we will get the result after mid no . of pages ; count how many students are required to distribute curr_min pages ; increment student count ; update curr_sum ; if students required becomes greater than given no . of students , return false ; else update curr_sum ; function to find minimum pages ; return - 1 if no . of books is less than no . of students ; Count total number of pages ; initialize start as 0 pages and end as total pages ; traverse until start <= end ; check if it is possible to distribute books by using mid as current minimum ; update result to current distribution as it 's the best we have found till now ; as we are finding minimum and books are sorted so reduce end = mid - 1 that means ; if not possible means pages should be increased so update start = mid + 1 ; at - last return minimum no . of pages ; Number of pages in books ; $m = 2 ; No . of students
< ? php function isPossible ( $ arr , $ n , $ m , $ curr_min ) { $ studentsRequired = 1 ; $ curr_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ curr_min ) return false ; if ( $ curr_sum + $ arr [ $ i ] > $ curr_min ) { $ studentsRequired ++ ; $ curr_sum = $ arr [ $ i ] ; if ( $ studentsRequired > $ m ) return false ; } else $ curr_sum += $ arr [ $ i ] ; } return true ; } function findPages ( $ arr , $ n , $ m ) { $ sum = 0 ; if ( $ n < $ m ) return -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; $ start = 0 ; $ end = $ sum ; $ result = PHP_INT_MAX ; while ( $ start <= $ end ) { $ mid = ( int ) ( $ start + $ end ) / 2 ; if ( isPossible ( $ arr , $ n , $ m , $ mid ) ) { $ result = $ mid ; $ end = $ mid - 1 ; } else $ start = $ mid + 1 ; } return $ result ; } $ arr = array ( 12 , 34 , 67 , 90 ) ; $ n = count ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ pages ▁ = ▁ " , findPages ( $ arr , $ n , $ m ) , " STRNEWLINE " ; ? >
Count distinct elements in an array | This function prints all distinct elements ; Creates an empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver Code
< ? php function countDistinct ( $ arr , $ n ) { $ s = array ( ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { array_push ( $ s , $ arr [ $ i ] ) ; } $ s = array_unique ( $ s ) ; return count ( $ s ) ; } $ arr = array ( 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ) ; $ n = count ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >
Rearrange an array to maximize i * arr [ i ] | Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver code
< ? php function findOptimalSolution ( $ a , $ N ) { sort ( $ a ) ; $ points = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ points += $ a [ $ i ] * $ i ; } return $ points ; } $ a = array ( 1 , 4 , 2 , 3 , 9 ) ; $ N = sizeof ( $ a ) ; echo ( findOptimalSolution ( $ a , $ N ) ) ; ? >
Minimum number of towers required such that every house is in the range of at least one tower | Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; given elements ; print number of towers
< ? php function number_of_tower ( $ house , $ range , $ n ) { sort ( $ house ) ; $ numOfTower = 0 ; $ i = 0 ; while ( $ i < $ n ) { $ numOfTower ++ ; $ loc = $ house [ $ i ] + $ range ; while ( $ i < $ n && $ house [ $ i ] <= $ loc ) $ i ++ ; -- $ i ; $ loc = $ house [ $ i ] + $ range ; while ( $ i < $ n && $ house [ $ i ] <= $ loc ) $ i ++ ; } return $ numOfTower ; } $ house = array ( 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 ) ; $ range = 2 ; $ n = sizeof ( $ house ) / sizeof ( $ house [ 0 ] ) ; echo number_of_tower ( $ house , $ range , $ n ) ; ? >
Check if the characters of a given string are in alphabetical order | Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver code ; check whether the string is in alphabetical order or not
< ? php Function isAlphabaticOrder ( $ s ) { $ n = strlen ( $ s ) ; $ c = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ c [ $ i ] = $ s [ $ i ] ; } sort ( $ c ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ c [ $ i ] != $ s [ $ i ] ) return false ; return true ; } $ s = " aabbbcc " ; if ( isAlphabaticOrder ( $ s ) ) echo " Yes " ; else echo " No " ; ? >
Sort first k values in ascending order and remaining n | function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Our arr contains 8 elements
< ? php function printOrder ( $ arr , $ n , $ k ) { $ a = array_slice ( $ arr , 0 , $ k ) ; sort ( $ a ) ; $ b = array_slice ( $ arr , $ k , $ n ) ; sort ( $ b ) ; $ b = array_reverse ( $ b ) ; unset ( $ arr ) ; $ arr = $ a ; return array_merge ( $ arr , $ b ) ; } $ arr = array ( 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , -1 ) ; $ k = 4 ; $ n = count ( $ arr ) ; $ arr = printOrder ( $ arr , $ n , $ k ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Program to sort string in descending order | PHP program to sort a string in descending order using library function ; Driver Code ; descOrder ( $s ) ; function call
< ? php function descOrder ( $ s ) { $ s = str_split ( $ s ) ; rsort ( $ s ) ; echo implode ( ' ' , $ s ) ; } $ s = " geeksforgeeks " ; ? >
Smallest element in an array that is repeated exactly ' k ' times . | PHP program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr that is repeated k times ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
< ? php $ MAX = 1000 ; function findDuplicate ( $ arr , $ n , $ k ) { global $ MAX ; $ freq = array_fill ( 0 , $ MAX , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < 1 && $ arr [ $ i ] > $ MAX ) { echo " Out ▁ of ▁ range " ; return -1 ; } $ freq [ $ arr [ $ i ] ] += 1 ; } for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { if ( $ freq [ $ i ] == $ k ) return $ i ; } return -1 ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = count ( $ arr ) ; echo findDuplicate ( $ arr , $ n , $ k ) ; ? >
Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calling pendulum function
< ? php function pendulumArrangement ( $ arr , $ n ) { sort ( $ arr , $ n ) ; sort ( $ arr ) ; $ op [ $ n ] = NULL ; $ mid = floor ( ( $ n - 1 ) / 2 ) ; $ j = 1 ; $ i = 1 ; $ op [ $ mid ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i <= $ mid ; $ i ++ ) { $ op [ $ mid + $ i ] = $ arr [ $ j ++ ] ; $ op [ $ mid - $ i ] = $ arr [ $ j ++ ] ; } if ( $ n % 2 == 0 ) $ op [ $ mid + $ i ] = $ arr [ $ j ] ; echo " Pendulum ▁ arrangement : " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ op [ $ i ] , " ▁ " ; echo " STRNEWLINE " ; } $ arr = array ( 14 , 6 , 19 , 21 , 12 ) ; $ n = sizeof ( $ arr ) ; pendulumArrangement ( $ arr , $ n ) ; ? >
Minimize the sum of product of two arrays with permutations allowed | Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driver Code
< ? php function minValue ( $ A , $ B , $ n ) { sort ( $ A ) ; sort ( $ A , $ n ) ; sort ( $ B ) ; sort ( $ B , $ n ) ; $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ result += ( $ A [ $ i ] * $ B [ $ n - $ i - 1 ] ) ; return $ result ; } $ A = array ( 3 , 1 , 1 ) ; $ B = array ( 6 , 5 , 4 ) ; $ n = sizeof ( $ A ) / sizeof ( $ A [ 0 ] ) ; echo minValue ( $ A , $ B , $ n ) ; ? >
Maximum number that can be display on Seven Segment Display using N segments | Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver 's Code
< ? php function printMaxNumber ( $ n ) { if ( $ n & 1 ) { echo "7" ; for ( $ i = 0 ; $ i < ( $ n - 3 ) / 2 ; $ i ++ ) echo "1" ; } else { for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) echo "1" ; } } $ n = 5 ; printMaxNumber ( $ n ) ; ? >
Partition an array such into maximum increasing segments | Returns the maximum number of sorted subarrays in a valid partition ; Find minimum value from right for every index ; Finding the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array . ; if current max is less than the right prefix min , we increase number of partitions . ; Driver code ; Find minimum value from right for every index
< ? php function sorted_partitions ( $ arr , $ n ) { $ right_min [ $ n + 1 ] = array ( ) ; $ right_min [ $ n ] = PHP_INT_MAX ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ right_min [ $ i ] = min ( $ right_min [ $ i + 1 ] , $ arr [ $ i ] ) ; } $ partitions = 0 ; for ( $ current_max = $ arr [ 0 ] , $ i = 0 ; $ i < $ n ; $ i ++ ) { $ current_max = max ( $ current_max , $ arr [ $ i ] ) ; if ( $ current_max <= $ right_min [ $ i + 1 ] ) $ partitions ++ ; } return $ partitions ; } $ arr = array ( 3 , 1 , 2 , 4 , 100 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; $ ans = sorted_partitions ( $ arr , $ n ) ; echo $ ans , " STRNEWLINE " ; ? >
Minimize Cost with Replacement with other allowed | this function returns the minimum cost of the array ; Code driven
< ? php function getMinCost ( $ arr , $ n ) { $ min_ele = min ( $ arr ) ; return $ min_ele * ( $ n - 1 ) ; } $ arr = array ( 4 , 2 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo getMinCost ( $ arr , $ n ) ; #This  code contributed by ajit NEW_LINE ? >
Check if it is possible to return to the starting position after moving in the given directions | Main method ; $n = 0 ; Count of North $s = 0 ; Count of South $e = 0 ; Count of East $w = 0 ; Count of West
< ? php $ st = " NNNWEWESSS " ; $ len = strlen ( $ st ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ st [ $ i ] == ' N ' ) $ n += 1 ; if ( $ st [ $ i ] == ' S ' ) $ s += 1 ; if ( $ st [ $ i ] == ' W ' ) $ w += 1 ; if ( $ st [ $ i ] == ' E ' ) $ e += 1 ; } if ( $ n == $ s && $ w == $ e ) echo " YES STRNEWLINE " ; else echo " NO STRNEWLINE " ; ? >
Minimum cost to make array size 1 by removing larger of pairs | function to calculate the minimum cost ; Minimum cost is n - 1 multiplied with minimum element . ; Driver Code
< ? php function cost ( $ a , $ n ) { return ( $ n - 1 ) * ( min ( $ a ) ) ; } $ a = array ( 4 , 3 , 2 ) ; $ n = count ( $ a ) ; echo cost ( $ a , $ n ) ; ? >
Minimum cost for acquiring all coins with k extra coins allowed with every coin | function to calculate min cost ; sort the coins value ; calculate no . of coins needed ; calculate sum of all selected coins ; Driver Code
< ? php function minCost ( $ coin , $ n , $ k ) { sort ( $ coin ) ; sort ( $ coin , $ n ) ; $ coins_needed = ceil ( 1.0 * $ n / ( $ k + 1 ) ) ; $ ans = 0 ; for ( $ i = 0 ; $ i <= $ coins_needed - 1 ; $ i ++ ) $ ans += $ coin [ $ i ] ; return $ ans ; } { $ coin = array ( 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ) ; $ n = sizeof ( $ coin ) / sizeof ( $ coin [ 0 ] ) ; $ k = 3 ; echo minCost ( $ coin , $ n , $ k ) ; return 0 ; } ? >
Gould 's Sequence | Utility function to count odd numbers in ith row of Pascals 's triangle ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's Sequence up to n ; Get n ; Function call
< ? php function countOddNumber ( $ row_num ) { $ count = 0 ; while ( $ row_num ) { $ count += $ row_num & 1 ; $ row_num >>= 1 ; } return ( 1 << $ count ) ; } function gouldSequence ( $ n ) { for ( $ row_num = 0 ; $ row_num < $ n ; $ row_num ++ ) { echo countOddNumber ( $ row_num ) , " " ; } } $ n = 16 ; gouldSequence ( $ n ) ; ? >
Tiling with Dominoes | PHP program to find no . of ways to fill a 3 xn board with 2 x1 dominoes . ; Driver Code
< ? php function countWays ( $ n ) { $ A = array ( ) ; $ B = array ( ) ; $ A [ 0 ] = 1 ; $ A [ 1 ] = 0 ; $ B [ 0 ] = 0 ; $ B [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ A [ $ i ] = $ A [ $ i - 2 ] + 2 * $ B [ $ i - 1 ] ; $ B [ $ i ] = $ A [ $ i - 1 ] + $ B [ $ i - 2 ] ; } return $ A [ $ n ] ; } $ n = 8 ; echo countWays ( $ n ) ; ? >
Newman | Recursive Function to find the n - th element ; Driver Code
< ? php function sequence ( $ n ) { if ( $ n == 1 $ n == 2 ) return 1 ; else return sequence ( sequence ( $ n - 1 ) ) + sequence ( $ n - sequence ( $ n - 1 ) ) ; } $ n = 10 ; echo ( sequence ( $ n ) ) ; ? >
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of peopl reduce to ( x - 2 ) . ; Driver code
< ? php function numberOfWays ( $ x ) { if ( $ x == 0 $ x == 1 ) return 1 ; else return numberOfWays ( $ x - 1 ) + ( $ x - 1 ) * numberOfWays ( $ x - 2 ) ; } $ x = 3 ; echo numberOfWays ( $ x ) ; ? >
Counting pairs when a person can form pair with at most one | PHP program for Number of ways in which participant can take part . ; Driver code
< ? php function numberOfWays ( $ x ) { $ dp [ 0 ] = 1 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ x ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + ( $ i - 1 ) * $ dp [ $ i - 2 ] ; return $ dp [ $ x ] ; } $ x = 3 ; echo numberOfWays ( $ x ) ; ? >
Longest Repeated Subsequence | Refer https : www . geeksforgeeks . org / longest - repeating - subsequence / for complete code . This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match
< ? php function findLongestRepeatingSubSeq ( $ str ) { $ n = strlen ( $ str ) ; $ dp = array_fill ( 0 , $ n + 1 , array_fill ( 0 , $ n + 1 , NULL ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { if ( $ str [ $ i - 1 ] == $ str [ $ j - 1 ] && $ i != $ j ) $ dp [ $ i ] [ $ j ] = 1 + $ dp [ $ i - 1 ] [ $ j - 1 ] ; else $ dp [ $ i ] [ $ j ] = max ( $ dp [ $ i ] [ $ j - 1 ] , $ dp [ $ i - 1 ] [ $ j ] ) ; } } return $ dp [ $ n ] [ $ n ] ; } ? >
Number of ways to arrange N items under given constraints | method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; Driver code
< ? php function waysToArrange ( $ N , $ K , $ k ) { $ C [ $ N + 1 ] [ $ N + 1 ] = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ N ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ C [ $ i ] [ $ j ] = 1 ; else $ C [ $ i ] [ $ j ] = ( $ C [ $ i - 1 ] [ $ j - 1 ] + $ C [ $ i - 1 ] [ $ j ] ) ; } } $ dp [ $ K ] = array ( ) ; $ count = 0 ; $ dp [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ K ; $ i ++ ) { $ dp [ $ i + 1 ] = ( $ dp [ $ i ] * $ C [ $ count + $ k [ $ i ] - 1 ] [ $ k [ $ i ] - 1 ] ) ; $ count += $ k [ $ i ] ; } return $ dp [ $ K ] ; } $ N = 4 ; $ k = array ( 2 , 2 ) ; $ K = sizeof ( $ k ) ; echo waysToArrange ( $ N , $ K , $ k ) , " STRNEWLINE " ; ? >
Minimum cells required to reach destination with jumps equal to cell values | function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != INT_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; Driver Code
< ? php function minCells ( $ mat , $ m , $ n ) { $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = PHP_INT_MAX ; $ dp [ 0 ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ dp [ $ i ] [ $ j ] != PHP_INT_MAX and ( $ j + $ mat [ $ i ] [ $ j ] ) < $ n and ( $ dp [ $ i ] [ $ j ] + 1 ) < $ dp [ $ i ] [ $ j + $ mat [ $ i ] [ $ j ] ] ) $ dp [ $ i ] [ $ j + $ mat [ $ i ] [ $ j ] ] = $ dp [ $ i ] [ $ j ] + 1 ; if ( $ dp [ $ i ] [ $ j ] != PHP_INT_MAX and ( $ i + $ mat [ $ i ] [ $ j ] ) < $ m and ( $ dp [ $ i ] [ $ j ] + 1 ) < $ dp [ $ i + $ mat [ $ i ] [ $ j ] ] [ $ j ] ) $ dp [ $ i + $ mat [ $ i ] [ $ j ] ] [ $ j ] = $ dp [ $ i ] [ $ j ] + 1 ; } } if ( $ dp [ $ m - 1 ] [ $ n - 1 ] != PHP_INT_MAX ) return $ dp [ $ m - 1 ] [ $ n - 1 ] ; return -1 ; } $ mat = array ( array ( 2 , 3 , 2 , 1 , 4 ) , array ( 3 , 2 , 5 , 8 , 2 ) , array ( 1 , 1 , 2 , 2 , 1 ) ) ; $ m = 3 ; $ n = 5 ; echo " Minimum ▁ number ▁ of ▁ cells ▁ = ▁ " , minCells ( $ mat , $ m , $ n ) ; ? >