"Lexicographic rank of a string | A O ( n ) solution for finding rank of string ; all elements of count [ ] are initialized with 0 ; A utility function to find factorial of n ; Construct a count array where value at every index contains count of smaller characters in whole string ; Removes a character ch from count [ ] array constructed by populateAndIncreaseCount ( ) ; A function to find rank of a string in all permutations of characters ; Populate the count array such that count [ i ] contains count of characters which are present in str and are smaller than i ; count number of chars smaller than str [ i ] fron str [ i + 1 ] to str [ len - 1 ] ; Reduce count of characters greater than str [ i ] ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX_CHAR = 256 ; $ count = array_fill ( 0 , $ MAX_CHAR + 1 , 0 ) ; function fact ( $ n ) { return ( $ n <= 1 ) ? 1 : $ n * fact ( $ n - 1 ) ; } function populateAndIncreaseCount ( & $ count , $ str ) { global $ MAX_CHAR ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; ++ $ i ) ++ $ count [ ord ( $ str [ $ i ] ) ] ; for ( $ i = 1 ; $ i < $ MAX_CHAR ; ++ $ i ) $ count [ $ i ] += $ count [ $ i - 1 ] ; } function updatecount ( & $ count , $ ch ) { global $ MAX_CHAR ; for ( $ i = ord ( $ ch ) ; $ i < $ MAX_CHAR ; ++ $ i ) -- $ count [ $ i ] ; } function findRank ( $ str ) { global $ MAX_CHAR ; $ len = strlen ( $ str ) ; $ mul = fact ( $ len ) ; $ rank = 1 ; populateAndIncreaseCount ( $ count , $ str ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { $ mul = ( int ) ( $ mul / ( $ len - $ i ) ) ; $ rank += $ count [ ord ( $ str [ $ i ] ) - 1 ] * $ mul ; updatecount ( $ count , $ str [ $ i ] ) ; } return $ rank ; } $ str = " string " ; echo findRank ( $ str ) ; ? >


"Lexicographic rank of a string | A utility function to find factorial of n ; A utility function to count smaller characters on right of arr [ low ] ; A function to find rank of a string in all permutations of characters ; count number of chars smaller than str [ i ] fron str [ i + 1 ] to str [ len - 1 ] ; Driver Code"
How can the above be solved in PHP?

< ? php function fact ( $ n ) { return ( $ n <= 1 ) ? 1 : $ n * fact ( $ n - 1 ) ; } function findSmallerInRight ( $ str , $ low , $ high ) { $ countRight = 0 ; for ( $ i = $ low + 1 ; $ i <= $ high ; ++ $ i ) if ( $ str [ $ i ] < $ str [ $ low ] ) ++ $ countRight ; return $ countRight ; } function findRank ( $ str ) { $ len = strlen ( $ str ) ; $ mul = fact ( $ len ) ; $ rank = 1 ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { $ mul /= $ len - $ i ; $ countRight = findSmallerInRight ( $ str , $ i , $ len - 1 ) ; $ rank += $ countRight * $ mul ; } return $ rank ; } $ str = " string " ; echo findRank ( $ str ) ; ? >


"Lexicographically Kth smallest way to reach given coordinate from origin | Return ( a + b ) ! / a ! b ! ; finding ( a + b ) ! ; finding ( a + b ) ! / a ! ; finding ( a + b ) ! / b ! ; Return the Kth smallest way to reach given coordinate from origin ; if at origin ; if on y - axis ; decrement y . ; Move vertical ; recursive call to take next step . ; If on x - axis ; decrement x . ; Move horizontal . ; recursive call to take next step . ; If x + y C x is greater than K ; Move Horizontal ; recursive call to take next step . ; Move vertical ; recursive call to take next step . ; Driver Code"
How can the above be solved in PHP?

< ? php function factorial ( $ a , $ b ) { $ res = 1 ; for ( $ i = 1 ; $ i <= ( $ a + $ b ) ; $ i ++ ) $ res = $ res * $ i ; for ( $ i = 1 ; $ i <= $ a ; $ i ++ ) $ res = $ res / $ i ; for ( $ i = 1 ; $ i <= $ b ; $ i ++ ) $ res = $ res / $ i ; return $ res ; } function Ksmallest ( $ x , $ y , $ k ) { if ( $ x == 0 && $ y == 0 ) return ; else if ( $ x == 0 ) { $ y -- ; echo ( " V " ) ; Ksmallest ( $ x , $ y , $ k ) ; } else if ( $ y == 0 ) { $ x -- ; echo ( " H " ) ; Ksmallest ( $ x , $ y , $ k ) ; } else { if ( factorial ( $ x - 1 , $ y ) > $ k ) { echo ( " H " ) ; Ksmallest ( $ x - 1 , $ y , $ k ) ; } else { echo ( " V " ) ; Ksmallest ( $ x , $ y - 1 , $ k - factorial ( $ x - 1 , $ y ) ) ; } } } $ x = 2 ; $ y = 2 ; $ k = 2 ; Ksmallest ( $ x , $ y , $ k ) ; ? >


"Lexicographically first palindromic string | PHP program to find first palindromic permutation of given string ; Function to count frequency of each char in the string . freq [ 0 ] for ' a ' , ... . , freq [ 25 ] for ' z ' ; Cases to check whether a palindr0mic string can be formed or not ; count_odd to count no of chars with odd frequency ; For even length string no odd freq character ; For odd length string one odd freq character ; Function to find odd freq char and reducing its freq by 1 returns " " if odd freq char is not present ; To find lexicographically first palindromic string . ; Assigning odd freq character if present else empty string . ; Traverse characters in increasing order ; Divide all occurrences into two halves . Note that odd character is removed by findOddAndRemoveItsFreq ( ) ; creating front string ; creating rear string ; Final palindromic string which is lexicographically first ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX_CHAR = 26 ; function countFreq ( $ str , & $ freq , $ len ) { for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; } function canMakePalindrome ( $ freq , $ len ) { global $ MAX_CHAR ; $ count_odd = 0 ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ freq [ $ i ] % 2 != 0 ) $ count_odd ++ ; if ( $ len % 2 == 0 ) { if ( $ count_odd > 0 ) return false ; else return true ; } if ( $ count_odd != 1 ) return false ; return true ; } function findOddAndRemoveItsFreq ( $ freq ) { global $ MAX_CHAR ; $ odd_str = " " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { if ( $ freq [ $ i ] % 2 != 0 ) { $ freq [ $ i ] -- ; $ odd_str . = chr ( $ i + ord ( ' a ' ) ) ; return $ odd_str ; } } return $ odd_str ; } function findPalindromicString ( $ str ) { global $ MAX_CHAR ; $ len = strlen ( $ str ) ; $ freq = array_fill ( 0 , $ MAX_CHAR , 0 ) ; countFreq ( $ str , $ freq , $ len ) ; if ( ! canMakePalindrome ( $ freq , $ len ) ) return " No ▁ Palindromic ▁ String " ; $ odd_str = findOddAndRemoveItsFreq ( $ freq ) ; $ front_str = " " ; $ rear_str = " ▁ " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { $ temp = " " ; if ( $ freq [ $ i ] != 0 ) { $ ch = chr ( $ i + ord ( ' a ' ) ) ; for ( $ j = 1 ; $ j <= ( int ) ( $ freq [ $ i ] / 2 ) ; $ j ++ ) $ temp . = $ ch ; $ front_str . = $ temp ; $ rear_str = $ temp . $ rear_str ; } } return ( $ front_str . $ odd_str . $ rear_str ) ; } $ str = " malayalam " ; echo findPalindromicString ( $ str ) ; ? >


"Lexicographically largest sub | Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code"
How can the above be solved in PHP?

< ? php function getSubSeq ( $ s , $ n ) { $ res = " " ; $ cr = 0 ; while ( $ cr < $ n ) { $ mx = $ s [ $ cr ] ; for ( $ i = $ cr + 1 ; $ i < $ n ; $ i ++ ) $ mx = max ( $ mx , $ s [ $ i ] ) ; $ lst = $ cr ; for ( $ i = $ cr ; $ i < $ n ; $ i ++ ) if ( $ s [ $ i ] == $ mx ) { $ res . = $ s [ $ i ] ; $ lst = $ i ; } $ cr = $ lst + 1 ; } return $ res ; } $ s = " geeksforgeeks " ; $ n = strlen ( $ s ) ; echo getSubSeq ( $ s , $ n ) ; ? >


"Lexicographically smallest permutation with no digits at Original Index | Function to print the smallest permutation ; when n is even ; when n is odd ; handling last 3 digit ; add EOL and print result ; Driver Code"
How can the above be solved in PHP?


"Lexicographically smallest string formed by appending a character from the first K characters of a given string | Function to find the new string thus formed by removing characters ; new string ; Remove characters until the string is empty ; Traverse to find the smallest character in the first k characters ; append the smallest character ; removing the lexicographically smallest character from the string ; Driver code"
How can the above be solved in PHP?

< ? php function newString ( $ s , $ k ) { $ X = " " ; while ( strlen ( $ s ) > 0 ) { $ temp = $ s [ 0 ] ; for ( $ i = 1 ; $ i < $ k && $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] < $ temp ) { $ temp = $ s [ $ i ] ; } } $ X = $ X . $ temp ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) { if ( $ s [ $ i ] == $ temp ) { $ s = substr ( $ s , 0 , $ i ) . substr ( $ s , $ i + 1 , strlen ( $ s ) ) ; break ; } } } return $ X ; } $ s = " gaurang " ; $ k = 3 ; echo ( newString ( $ s , $ k ) ) ; ? >


"Lexicographically smallest string whose hamming distance from given string is exactly K | function to find Lexicographically smallest string with hamming distance k ; If k is 0 , output input string ; Copying input string into output string ; Traverse all the character of the string ; If current character is not ' a ' ; copy character ' a ' to output string ; str2 [ i ] = ' a ' ; ; If hamming distance became k , break ; ; If k is less than p ; Traversing string in reverse order ; Driver Code"
How can the above be solved in PHP?


"Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | Function to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only . ; To store frequency of digits ; size of string ; Take lexicographically larger digit in b ; get frequency of each character ; If no such string exits ; Maximum frequency ; Driver Code"
How can the above be solved in PHP?


"Leyland Number | PHP program to print first N Leyland Numbers . ; Print first n Leyland Number . ; Outer loop for x from 2 to n . ; Inner loop for y from 2 to x . ; Calculating x ^ y + y ^ x ; Sorting the all Leyland Number . ; Printing first n Leyland number . ; Driver Code"
How can the above be solved in PHP?


"Linear Diophantine Equations | utility function to find the GCD of two numbers ; This function checks if integral solutions are possible ; First example ; Second example ; Third example"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { return ( $ a % $ b == 0 ) ? abs ( $ b ) : gcd ( $ b , $ a % $ b ) ; } function isPossible ( $ a , $ b , $ c ) { return ( $ c % gcd ( $ a , $ b ) == 0 ) ; } $ a = 3 ; $ b = 6 ; $ c = 9 ; if ( isPossible ( $ a , $ b , $ c ) == true ) echo " Possible
 " ; else echo " Not ▁ Possible
 " ; $ a = 3 ; $ b = 6 ; $ c = 8 ; if ( isPossible ( $ a , $ b , $ c ) == true ) echo " Possible
 " ; else echo " Not ▁ Possible
 " ; $ a = 2 ; $ b = 5 ; $ c = 1 ; if ( isPossible ( $ a , $ b , $ c ) == true ) echo " Possible
 " ; else echo " Not ▁ Possible
 " ; ? >


"Lobb Number | PHP Program to find Ln , m Lobb Number . ; Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using p reviously stored values ; Return the Lm , n Lobb Number . ; Driven Code"
How can the above be solved in PHP?


"Logarithm | PHP program to find log ( n ) using Recursion ; Drive main"
How can the above be solved in PHP?

< ? php function Log2n ( $ n ) { return ( $ n > 1 ) ? 1 + Log2n ( $ n / 2 ) : 0 ; } $ n = 32 ; echo Log2n ( $ n ) ; ? >


"Longest Arithmetic Progression | DP | Returns length of the longest AP subset in a given set ; Create a table and initialize all values as 2. The value of L [ i ] [ j ] stores LLAP with set [ i ] and set [ j ] as first two elements of AP . Only valid entries are the entries where j > i ; Fill entries in last column as 2. There will always be two elements in AP with last number of set as second element in AP ; Consider every element as second element of AP ; Search for i and k for j ; Before changing i , set L [ i ] [ j ] as 2 ; Found i and k for j , LLAP with i and j as first two elements is equal to LLAP with j and k as first two elements plus 1. L [ j ] [ k ] must have been filled before as we run the loop from right side ; Update overall LLAP , if needed ; Change i and k to fill more L [ i ] [ j ] values for current j ; If the loop was stopped due to k becoming more than n - 1 , set the remaining entities in column j as 2 ; Driver Code"
How can the above be solved in PHP?


"Longest Bitonic Subsequence | DP | lbs ( ) returns the length of the Longest Bitonic Subsequence in arr [ ] of size n . The function mainly creates two temporary arrays lis [ ] and lds [ ] and returns the maximum lis [ i ] + lds [ i ] - 1. lis [ i ] == > Longest Increasing subsequence ending with arr [ i ] lds [ i ] == > Longest decreasing subsequence starting with arr [ i ] ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right ; Allocate memory for lds and initialize LDS values for all indexes ; Compute LDS values from right to left ; Return the maximum value of lis [ i ] + lds [ i ] - 1 ; Driver Code"
How can the above be solved in PHP?


"Longest Common Anagram Subsequence | PHP implementation to find the length of the longest common anagram subsequence ; function to find the length of the longest common anagram subsequence ; hash tables for storing frequencies of each character ; calculate frequency of each character of ' str1' ; calculate frequency of each character of ' str2' ; for each character add its minimum frequency out of the two strings in ' len ' ; required length ; Driver Code"
How can the above be solved in PHP?

< ? php $ SIZE = 26 ; function longCommomAnagramSubseq ( $ str1 , $ str2 , $ n1 , $ n2 ) { global $ SIZE ; $ freq1 = array ( ) ; $ freq2 = array ( ) ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ freq1 [ $ i ] = 0 ; $ freq2 [ $ i ] = 0 ; } $ len = 0 ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) $ freq1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ n2 ; $ i ++ ) $ freq2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ len += min ( $ freq1 [ $ i ] , $ freq2 [ $ i ] ) ; } return $ len ; } $ str1 = " abdacp " ; $ str2 = " ckamb " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; echo ( " Length ▁ = ▁ " . longCommomAnagramSubseq ( $ str1 , $ str2 , $ n1 , $ n2 ) ) ; ? >


"Longest Common Prefix Matching | Set | A Utility Function to find the common prefix between first and last strings ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; sorts the N set of strings ; prints the common prefix of the first and the last string of the set of strings ; Driver Code"
How can the above be solved in PHP?

< ? php function commonPrefixUtil ( $ str1 , $ str2 ) { $ result = " " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; for ( $ i = 0 , $ j = 0 ; $ i <= $ n1 - 1 && $ j <= $ n2 - 1 ; $ i ++ , $ j ++ ) { if ( $ str1 [ $ i ] != $ str2 [ $ j ] ) break ; $ result = $ result . $ str1 [ $ i ] ; } return ( $ result ) ; } function commonPrefix ( & $ arr , $ n ) { sort ( $ arr ) ; echo commonPrefixUtil ( $ arr [ 0 ] , $ arr [ $ n - 1 ] ) ; } $ arr = array ( " geeksforgeeks " , " geeks " , " geek " , " geezer " ) ; $ n = sizeof ( $ arr ) ; commonPrefix ( $ arr , $ n ) ; ? >


"Longest Common Subsequence | DP using Memoization | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code ; Find the length of string"
How can the above be solved in PHP?

< ? php function lcs ( $ X , $ Y , $ m , $ n ) { if ( $ m == 0 $ n == 0 ) return 0 ; if ( $ X [ $ m - 1 ] == $ Y [ $ n - 1 ] ) return 1 + lcs ( $ X , $ Y , $ m - 1 , $ n - 1 ) ; else return max ( lcs ( $ X , $ Y , $ m , $ n - 1 ) , lcs ( $ X , $ Y , $ m - 1 , $ n ) ) ; } $ X = " AGGTAB " ; $ Y = " GXTXAYB " ; $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; echo " Length ▁ of ▁ LCS : ▁ " . lcs ( $ X , $ Y , $ m , $ n ) ; ? >


"Longest Common Subsequence | DP | Dynamic Programming C # implementation of LCS problem ; find the length of the strings ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note : L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains the length of LCS of X [ 0. . n - 1 ] & Y [ 0. . m - 1 ] ; Driver Code"
How can the above be solved in PHP?


"Longest Common Subsequence | DP | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code"
How can the above be solved in PHP?

< ? php function lcs ( $ X , $ Y , $ m , $ n ) { if ( $ m == 0 $ n == 0 ) return 0 ; else if ( $ X [ $ m - 1 ] == $ Y [ $ n - 1 ] ) return 1 + lcs ( $ X , $ Y , $ m - 1 , $ n - 1 ) ; else return max ( lcs ( $ X , $ Y , $ m , $ n - 1 ) , lcs ( $ X , $ Y , $ m - 1 , $ n ) ) ; } $ X = " AGGTAB " ; $ Y = " GXTXAYB " ; echo " Length ▁ of ▁ LCS ▁ is ▁ " ; echo lcs ( $ X , $ Y , strlen ( $ X ) , strlen ( $ Y ) ) ; ? >


"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"
How can the above be solved in PHP?

< ? 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 ) ) ; ? >


"Longest Common Substring in an Array of Strings | function to find the stem ( longest common substring ) from the string array ; Determine size of the array ; Take first word from array as reference ; generating all possible substrings of our reference string arr [ 0 ] i . e s ; Check if the generated stem is common to all words ; If current substring is present in all strings and its length is greater than current result ; Driver Code ; Function call"
How can the above be solved in PHP?

< ? php function findstem ( $ arr ) { $ n = count ( $ arr ) ; $ s = $ arr [ 0 ] ; $ len = strlen ( $ s ) ; $ res = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j <= $ len ; $ j ++ ) { $ stem = substr ( $ s , $ i , $ j - $ i ) ; $ k = 1 ; for ( $ k = 1 ; $ k < $ n ; $ k ++ ) if ( ! strpos ( $ arr [ $ k ] , $ stem ) ) break ; if ( $ k <= $ n && strlen ( $ res ) < strlen ( $ stem ) ) $ res = $ stem ; } } return $ res ; } $ arr = array ( " grace " , " graceful " , " disgraceful " , " gracefully " ) ; $ stems = findstem ( $ arr ) ; print ( $ stems ) ; ? >


"Longest Common Substring | DP | Returns length of function for longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Driver code"
How can the above be solved in PHP?

< ? php function lcs ( $ i , $ j , $ count , & $ X , & $ Y ) { if ( $ i == 0 $ j == 0 ) return $ count ; if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) { $ count = lcs ( $ i - 1 , $ j - 1 , $ count + 1 , $ X , $ Y ) ; } $ count = max ( $ count , lcs ( $ i , $ j - 1 , 0 , $ X , $ Y ) , lcs ( $ i - 1 , $ j , 0 , $ X , $ Y ) ) ; return $ count ; } $ X = " abcdxyz " ; $ Y = " xyzabcd " ; $ n = strlen ( $ X ) ; $ m = strlen ( $ Y ) ; echo lcs ( $ n , $ m , 0 , $ X , $ Y ) ; ? >


"Longest Common Substring | DP | Returns length of longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Notethat LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; Driver Code"
How can the above be solved in PHP?

< ? php function LCSubStr ( $ X , $ Y , $ m , $ n ) { $ LCSuff = array_fill ( 0 , $ m + 1 , array_fill ( 0 , $ n + 1 , NULL ) ) ; $ result = 0 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) $ LCSuff [ $ i ] [ $ j ] = 0 ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) { $ LCSuff [ $ i ] [ $ j ] = $ LCSuff [ $ i - 1 ] [ $ j - 1 ] + 1 ; $ result = max ( $ result , $ LCSuff [ $ i ] [ $ j ] ) ; } else $ LCSuff [ $ i ] [ $ j ] = 0 ; } } return $ result ; } $ X = " OldSite : GeeksforGeeks . org " ; $ Y = " NewSite : GeeksQuiz . com " ; $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; echo " Length ▁ of ▁ Longest ▁ Common ▁ Substring ▁ is ▁ " . LCSubStr ( $ X , $ Y , $ m , $ n ) ; ? >


"Longest Decreasing Subsequence | Function that returns the length of the longest decreasing subsequence ; Initialize LDS with 1 for all index The minimum LDS starting with any element is always 1 ; Compute LDS from every index in bottom up manner ; Select the maximum of all the LDS values ; returns the length of the LDS ; Driver Code"
How can the above be solved in PHP?


"Longest Non | utility function to check whether a string is palindrome or not ; Check for palindrome . ; palindrome string ; function to find maximum length substring which is not palindrome ; to check whether all characters of the string are same or not ; All characters are same , we can 't  make a non-palindromic string. ; If string is palindrome , we can make it non - palindrome by removing any corner character ; Complete string is not a palindrome . ; Driver code"
How can the above be solved in PHP?

< ? php function isPalindrome ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) if ( $ str [ $ i ] != $ str [ $ n - $ i - 1 ] ) return false ; return true ; } function maxLengthNonPalinSubstring ( $ str ) { $ n = strlen ( $ str ) ; $ ch = $ str [ 0 ] ; $ i = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ str [ $ i ] != $ ch ) break ; if ( $ i == $ n ) return 0 ; if ( isPalindrome ( $ str ) ) return ( $ n - 1 ) ; return $ n ; } $ str = " abba " ; echo " Maximum ▁ Length ▁ = ▁ " , maxLengthNonPalinSubstring ( $ str ) ; ? >


"Longest Palindromic Subsequence | DP | A utility function to get max of two integers ; Returns the length of the longest palindromic subsequence in seq ; Create a table to store results of subproblems ; Strings of length 1 are palindrome of lentgh 1 ; Build the table . Note that the lower diagonal values of table are useless and not filled in the process . The values are filled in a manner similar to Matrix Chain Multiplication DP solution ( Seewww . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) . https : cl is length of substring ; Driver Code"
How can the above be solved in PHP?


"Longest Palindromic Subsequence | DP | Returns the length of the longest palindromic subsequence in seq ; Base Case 1 : If there is only 1 character ; Base Case 2 : If there are only 2 characters and both are same ; If the first and last characters match ; If the first and last characters do not match ; Driver Code"
How can the above be solved in PHP?

< ? php function lps ( $ seq , $ i , $ j ) { if ( $ i == $ j ) return 1 ; if ( $ seq [ $ i ] == $ seq [ $ j ] && $ i + 1 == $ j ) return 2 ; if ( $ seq [ $ i ] == $ seq [ $ j ] ) return lps ( $ seq , $ i + 1 , $ j - 1 ) + 2 ; return max ( lps ( $ seq , $ i , $ j - 1 ) , lps ( $ seq , $ i + 1 , $ j ) ) ; } $ seq = " GEEKSFORGEEKS " ; $ n = strlen ( $ seq ) ; echo " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ " . lps ( $ seq , 0 , $ n - 1 ) ; ? >


"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"
How can the above be solved in PHP?


"Longest Repeated Subsequence | This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; THIS PART OF CODE IS SAME AS BELOW POST . IT FILLS dp [ ] [ ] https : www . geeksforgeeks . org / longest - repeating - subsequence / OR the code mentioned above . ; THIS PART OF CODE FINDS THE RESULT STRING USING DP [ ] [ ] , Initialize result ; Traverse dp [ ] [ ] from bottom right ; If this cell is same as diagonally adjacent cell just above it , then same characters are present at str [ i - 1 ] and str [ j - 1 ] . Append any of them to result . ; Otherwise we move to the side that that gave us maximum result ; Since we traverse dp [ ] [ ] from bottom , we get result in reverse order . ; Driver Code"
How can the above be solved in PHP?


"Longest Repeating Subsequence | 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 ; Driver Code"
How can the above be solved in PHP?

< ? php function findLongestRepeatingSubSeq ( $ str ) { $ n = strlen ( $ str ) ; $ dp = array ( array ( ) ) ; 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 ] ; } $ str = " aabb " ; echo " The ▁ length ▁ of ▁ the ▁ largest ▁ " . " subsequence ▁ that ▁ repeats ▁ itself ▁ is ▁ : ▁ " , findLongestRepeatingSubSeq ( $ str ) ; ? >


"Longest Span with same Sum in two Binary arrays | Returns length of the longest common subarray with same sum ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver Code"
How can the above be solved in PHP?

< ? php function longestCommonSum ( $ arr1 , $ arr2 , $ n ) { $ maxLen = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 = 0 ; $ sum2 = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ sum1 += $ arr1 [ $ j ] ; $ sum2 += $ arr2 [ $ j ] ; if ( $ sum1 == $ sum2 ) { $ len = $ j - $ i + 1 ; if ( $ len > $ maxLen ) $ maxLen = $ len ; } } } return $ maxLen ; } $ arr1 = array ( 0 , 1 , 0 , 1 , 1 , 1 , 1 ) ; $ arr2 = array ( 1 , 1 , 1 , 1 , 1 , 0 , 1 ) ; $ n = sizeof ( $ arr1 ) ; echo " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ " . " with ▁ same ▁ " , " sum ▁ is ▁ " , longestCommonSum ( $ arr1 , $ arr2 , $ n ) ; ? >


"Longest Sub | Function to return the max length of the sub - array that have the maximum average ( average value of the elements ) ; Finding the maximum value ; If consecutive maximum found ; Find the max length of consecutive max ; Driver code"
How can the above be solved in PHP?


"Longest Subarray with first element greater than or equal to Last element | Search function for searching the first element of the subarray which is greater or equal to the last element ( num ) ; Returns length of the longest array with first element smaller than the last element . ; Initially the search space is empty . ; We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space . ; we will search for the index first element in the search space and we will use it find the index of it in the original array . ; Update the answer if the length of the subarray is greater than the previously calculated lengths . ; Driver code"
How can the above be solved in PHP?

< ? php function binarySearch ( $ searchSpace , $ s , $ e , $ num ) { $ ans = 0 ; while ( $ s <= $ e ) { $ mid = ( $ s + $ e ) / 2 ; if ( $ searchSpace [ $ mid ] >= $ num ) { $ ans = $ mid ; $ e = $ mid - 1 ; } else { $ s = $ mid + 1 ; } } return $ ans ; } function longestSubArr ( & $ arr , $ n ) { $ j = 0 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ j == 0 or $ searchSpace [ $ j - 1 ] < $ arr [ $ i ] ) { $ searchSpace [ $ j ] = $ arr [ $ i ] ; $ index [ $ j ] = $ i ; $ j ++ ; } $ idx = binarySearch ( $ searchSpace , 0 , $ j - 1 , $ arr [ $ i ] ) ; $ ans = max ( $ ans , $ i - $ index [ $ idx ] + 1 ) ; } return $ ans ; } $ arr = array ( -5 , -1 , 7 , 5 , 1 , -2 ) ; $ n = sizeof ( $ arr ) ; echo ( longestSubArr ( $ arr , $ n ) ) ; ? >


"Longest Uncommon Subsequence | function to calculate length of longest uncommon subsequence ; Case 1 : If strings are equal ; for case 2 and case 3 ; input strings"
How can the above be solved in PHP?

< ? php function findLUSlength ( $ a , $ b ) { if ( ! strcmp ( $ a , $ b ) ) return 0 ; return max ( strlen ( $ a ) , strlen ( $ b ) ) ; } $ a = " abcdabcd " ; $ b = " abcabc " ; echo ( findLUSlength ( $ a , $ b ) ) ; ? >


"Longest alternating ( positive and negative ) subarray starting at every index | PHP program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Print result ; Driver code"
How can the above be solved in PHP?

< ? php function longestAlternating ( $ arr , $ n ) { $ count = array ( ) ; $ count [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] * $ arr [ $ i + 1 ] < 0 ) $ count [ $ i ] = $ count [ $ i + 1 ] + 1 ; else $ count [ $ i ] = 1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ count [ $ i ] , " ▁ " ; } $ a = array ( -5 , -1 , -1 , 2 , -2 , -3 ) ; $ n = count ( $ a ) ; longestAlternating ( $ a , $ 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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"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"
How can the above be solved in PHP?

< ? 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 common subsequence with permutations allowed | Function to calculate longest string str1 -- > first string str2 -- > second string count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest string whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; append character ( ' a ' + i ) in resultant string ' result ' by min ( count1 [ $i ] , count2 [ $i ] ) times ; Driver Code"
How can the above be solved in PHP?

< ? php function longestString ( $ str1 , $ str2 ) { $ count1 = array_fill ( 0 , 26 , NULL ) ; $ count2 = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < strlen ( $ str1 ) ; $ i ++ ) $ count1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < strlen ( $ str2 ) ; $ i ++ ) $ count2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; $ result = " " ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) for ( $ j = 1 ; $ j <= min ( $ count1 [ $ i ] , $ count2 [ $ i ] ) ; $ j ++ ) $ result = $ result . chr ( ord ( ' a ' ) + $ i ) ; echo $ result ; } $ str1 = " geeks " ; $ str2 = " cake " ; longestString ( $ str1 , $ str2 ) ; ? >


"Longest rod that can be inserted within a right circular cylinder | Function to find the side of the cube ; height and radius cannot be negative ; length of rod ; Driver code"
How can the above be solved in PHP?


"Longest sub | Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code"
How can the above be solved in PHP?

< ? php function longestSubArray ( $ arr , $ n ) { $ isZeroPresent = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 0 ) { $ isZeroPresent = true ; break ; } } if ( $ isZeroPresent ) return $ n ; return 0 ; } $ arr = array ( 1 , 2 , 3 , 0 , 1 , 2 , 0 ) ; $ n = sizeof ( $ arr ) ; echo longestSubArray ( $ arr , $ n ) ; ? >


"Longest sub | PHP implementation to find the length of the longest sub$having frequency of each character less than equal to k ; function to find the length of the longest sub$ having frequency of each character less than equal to k ; hash table to store frequency of each table ; ' start ' index of the current substring ; to store the maximum length ; traverse the $ ' str ' ; get the current character as ' ch ' ; increase frequency of ' ch ' in ' freq [ ] ' ; if frequency of ' ch ' becomes more than ' k ' ; update ' maxLen ' ; decrease frequency of each character as they are encountered from the ' start ' index until frequency of ' ch ' is greater than ' k ' ; decrement frequency by '1' ; increment ' start ' ; update maxLen ; required length ; Driver Code"
How can the above be solved in PHP?

< ? php $ SIZE = 26 ; function longSubstring ( $ str , $ k ) { global $ SIZE ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) $ freq [ $ i ] = 0 ; $ start = 0 ; $ maxLen = 0 ; $ ch = ' ' $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ ch = $ str [ $ i ] ; $ freq [ ord ( $ ch ) - ord ( ' a ' ) ] ++ ; if ( $ freq [ ord ( $ ch ) - ord ( ' a ' ) ] > $ k ) { if ( $ maxLen < ( $ i - $ start ) ) $ maxLen = $ i - $ start ; while ( $ freq [ ord ( $ ch ) - ord ( ' a ' ) ] > $ k ) { $ freq [ ord ( $ str [ $ start ] ) - ord ( ' a ' ) ] -- ; $ start ++ ; } } } if ( $ maxLen < ( $ n - $ start ) ) $ maxLen = $ n - $ start ; return $ maxLen ; } $ str = " babcaag " ; $ k = 1 ; echo ( " Length ▁ = ▁ " . longSubstring ( $ str , $ k ) ) ; ? >


"Longest subarray such that the difference of max and min is at | PHP code to find longest subarray with difference between max and min as at - most 1. ; longest constant range length ; first number ; If we see same number ; If we see different number , but same as previous . ; If number is neither same as previous nor as current . ; Driver Code"
How can the above be solved in PHP?

< ? php function longestSubarray ( $ input , $ length ) { $ prev = -1 ; $ prevCount = 0 ; $ currentCount = 1 ; $ longest = 1 ; $ current = $ input [ 0 ] ; for ( $ i = 1 ; $ i < $ length ; $ i ++ ) { $ next = $ input [ $ i ] ; if ( $ next == $ current ) { $ currentCount ++ ; } else if ( $ next == $ prev ) { $ prevCount += $ currentCount ; $ prev = $ current ; $ current = $ next ; $ currentCount = 1 ; } else { $ longest = max ( $ longest , $ currentCount + $ prevCount ) ; $ prev = $ current ; $ prevCount = $ currentCount ; $ current = $ next ; $ currentCount = 1 ; } } return max ( $ longest , $ currentCount + $ prevCount ) ; } $ input = array ( 5 , 5 , 6 , 7 , 6 ) ; echo ( longestSubarray ( $ input , count ( $ input ) ) ) ; ? >


"Longest subarray with elements divisible by k | function to find longest subarray ; This will contain length of longest subarray found ; Driver code"
How can the above be solved in PHP?

< ? php function longestsubarray ( $ arr , $ n , $ k ) { $ current_count = 0 ; $ max_count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % $ k == 0 ) $ current_count ++ ; else $ current_count = 0 ; $ max_count = max ( $ current_count , $ max_count ) ; } return $ max_count ; } $ arr = array ( 2 , 5 , 11 , 32 , 64 , 88 ) ; $ n = sizeof ( $ arr ) ; $ k = 8 ; echo longestsubarray ( $ arr , $ n , $ k ) ; ? >


"Longest subarray with elements having equal modulo K | function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code"
How can the above be solved in PHP?

< ? php function LongestSubarray ( $ arr , $ n , $ k ) { $ cnt = 1 ; $ max_length = 1 ; $ prev_mod = $ arr [ 0 ] % $ k ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr_mod = $ arr [ $ i ] % $ k ; if ( $ curr_mod == $ prev_mod ) { $ cnt ++ ; } else { $ max_length = max ( $ max_length , $ cnt ) ; $ cnt = 1 ; $ prev_mod = $ curr_mod ; } } return max ( $ max_length , $ cnt ) ; } $ arr = array ( 4 , 9 , 7 , 18 , 29 , 11 ) ; $ n = count ( $ arr ) ; $ k = 11 ; echo LongestSubarray ( $ arr , $ n , $ k ) ; ? >


"Longest subsequence such that difference between adjacents is one | Function to find the length of longest subsequence ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Start traversing the given array ; Compare with all the previous elements ; If the element is consecutive then consider this subsequence and update dp [ i ] if required . ; Longest length will be the maximum value of dp array . ; Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }"
How can the above be solved in PHP?

< ? php function longestSubseqWithDiffOne ( $ arr , $ n ) { $ dp [ $ n ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ dp [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( ( $ arr [ $ i ] == $ arr [ $ j ] + 1 ) || ( $ arr [ $ i ] == $ arr [ $ j ] - 1 ) ) $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ j ] + 1 ) ; } } $ result = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ result < $ dp [ $ i ] ) $ result = $ dp [ $ i ] ; return $ result ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; echo longestSubseqWithDiffOne ( $ arr , $ n ) ; ? >


"Longest substring of 0 s in a string formed by k concatenations | Function to calculate maximum length of substring containing only zero ; loop to first calculate longest substring in string ; if all elements in string are '0' ; Else , find size of prefix and suffix containing only zeroes ; Calculate prefix containing only zeroes ; Calculate suffix containing only zeroes ; if k <= 1 then there is no need to take prefix + suffix into account ; Driver code"
How can the above be solved in PHP?

< ? php function subzero ( $ str , $ k ) { $ ans = 0 ; $ curr = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { if ( $ str [ $ i ] == '0' ) $ curr ++ ; else $ curr = 0 ; $ ans = max ( $ ans , $ curr ) ; } if ( $ ans == $ len ) return $ len * $ k ; else { $ pre = 0 ; $ suff = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == '0' ) $ pre ++ ; else break ; } for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == '0' ) $ suff ++ ; else break ; } if ( $ k > 1 ) $ ans = max ( $ ans , $ pre + $ suff ) ; return $ ans ; } } $ str = "00100110" ; $ k = 5 ; echo subzero ( $ str , $ k ) ; ? >


"Longest substring of only 4 's from the first N characters of the infinite string | PHP implementation of the approach ; Function to return the length of longest contiguous string containing only 4 aTMs from the first N characters of the string ; Initialize result ; Initialize prefix sum array of characters and product variable ; Preprocessing of prefix sum array ; Initialize variable to store the string length where N belongs to ; Finding the string length where N belongs to ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAXN = 30 ; function countMaxLength ( $ N ) { $ res = 0 ; $ pre = array ( ) ; $ p = 1 ; $ pre [ 0 ] = 0 ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' AXN ' ; $ i ++ ) { $ p *= 2 ; $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ i * $ p ; } $ ind = 0 ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' AXN ' ; $ i ++ ) { if ( $ pre [ $ i ] >= $ N ) { $ ind = $ i ; break ; } } $ x = $ N - $ pre [ $ ind - 1 ] ; $ y = 2 * $ ind - 1 ; if ( $ x >= $ y ) $ res = min ( $ x , $ y ) ; else $ res = max ( $ x , 2 * ( $ ind - 2 ) + 1 ) ; return $ res ; } $ N = 25 ; echo countMaxLength ( $ N ) ; ? >


"Longest substring of vowels | PHP program to find the longest substring of vowels . ; Increment current count if s [ i ] is vowel ; check previous value is greater then or not ; Driver code"
How can the above be solved in PHP?

< ? php function isVowel ( $ c ) { return ( $ c == ' a ' $ c == ' e ' $ c == ' i ' $ c == ' o ' $ c == ' u ' ) ; } function longestVowel ( $ s ) { $ count = 0 ; $ res = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( isVowel ( $ s [ $ i ] ) ) $ count ++ ; else { $ res = max ( $ res , $ count ) ; $ count = 0 ; } } return max ( $ res , $ count ) ; } $ s = " theeare " ; echo longestVowel ( $ s ) ; ? >


"Lucas Numbers | Iterative function ; declaring base values for positions 0 and 1 ; generating number ; Driver Code"
How can the above be solved in PHP?


"Lucas Numbers | recursive function ; Base cases ; recurrence relation ; Driver Code"
How can the above be solved in PHP?

< ? php function lucas ( $ n ) { if ( $ n == 0 ) return 2 ; if ( $ n == 1 ) return 1 ; return lucas ( $ n - 1 ) + lucas ( $ n - 2 ) ; } $ n = 9 ; echo lucas ( $ n ) ; ? >


"Lucky Numbers | Returns 1 if n is a lucky no . ohterwise returns 0 ; variable next_position is just for readability of the program we can remove it and use n only ; calculate next position of input no ; Driver Code"
How can the above be solved in PHP?

< ? php function isLucky ( $ n ) { $ counter = 2 ; $ next_position = $ n ; if ( $ counter > $ n ) return 1 ; if ( $ n % $ counter == 0 ) return 0 ; $ next_position -= $ next_position / $ counter ; $ counter ++ ; return isLucky ( $ next_position ) ; } $ x = 5 ; if ( isLucky ( $ x ) ) echo $ x , " ▁ is ▁ a ▁ lucky ▁ no . " ; else echo $ x , " ▁ is ▁ not ▁ a ▁ lucky ▁ no . " ; ? >


"Lychrel Number Implementation | Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX_ITERATIONS = 20 ; function isLychrel ( $ number ) { global $ MAX_ITERATIONS ; for ( $ i = 0 ; $ i < $ MAX_ITERATIONS ; $ i ++ ) { $ number = $ number + reverse ( $ number ) ; if ( isPalindrome ( $ number ) ) return " false " ; } return " true " ; } function isPalindrome ( $ number ) { return $ number == reverse ( $ number ) ; } function reverse ( $ number ) { $ reverse = 0 ; while ( $ number > 0 ) { $ remainder = $ number % 10 ; $ reverse = ( $ reverse * 10 ) + $ remainder ; $ number = ( int ) ( $ number / 10 ) ; } return $ reverse ; } $ number = 295 ; echo $ number . " ▁ is ▁ lychrel ? ▁ " . isLychrel ( $ number ) ; ? >


"Magical Indices in an array | Function to count number of magical indices . ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code"
How can the above be solved in PHP?


"Make all array elements equal with minimum cost | Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver Code"
How can the above be solved in PHP?

< ? php function computeCost ( $ arr , $ N , $ X ) { $ cost = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ cost += abs ( $ arr [ $ i ] - $ X ) ; return $ cost ; } function minCostToMakeElementEqual ( $ arr , $ N ) { $ low ; $ high ; $ low = $ high = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ low > $ arr [ $ i ] ) $ low = $ arr [ $ i ] ; if ( $ high < $ arr [ $ i ] ) $ high = $ arr [ $ i ] ; } while ( ( $ high - $ low ) > 2 ) { $ mid1 = $ low + ( floor ( $ high - $ low ) / 3 ) ; $ mid2 = $ high - ( $ high - $ low ) / 3 ; $ cost1 = computeCost ( $ arr , $ N , $ mid1 ) ; $ cost2 = computeCost ( $ arr , $ N , $ mid2 ) ; if ( $ cost1 < $ cost2 ) $ high = $ mid2 ; else $ low = $ mid1 ; } return computeCost ( $ arr , $ N , ( $ low + $ high ) / 2 ) ; } $ arr = array ( 1 , 100 , 101 ) ; $ N = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo minCostToMakeElementEqual ( $ arr , $ N ) ; ? >


"Make all elements of an array equal with the given operation | Function that returns true if all the elements of the array can be made equal with the given operation ; To store the sum of the array elements and the maximum element from the array ; Driver code"
How can the above be solved in PHP?

 < ? php function isPossible ( $ n , $ k , $ arr ) { $ sum = $ arr [ 0 ] ; $ maxVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ maxVal = max ( $ maxVal , $ arr [ $ i ] ) ; } if ( ( float ) $ maxVal > ( float ) ( $ sum + $ k ) / $ n ) return false ; return true ; } $ k = 8 ; $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; if ( isPossible ( $ n , $ k , $ arr ) ) echo " Yes " ; else echo " No " ; # This  code is contributed by akt_miit.
? >


"Make all numbers of an array equal | Function that returns true if all the array elements can be made equal with the given operation ; Divide number by 2 ; Divide number by 3 ; Driver code"
How can the above be solved in PHP?

 < ? php function EqualNumbers ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ a [ $ i ] % 2 == 0 ) $ a [ $ i ] /= 2 ; while ( $ a [ $ i ] % 3 == 0 ) $ a [ $ i ] /= 3 ; if ( $ a [ $ i ] != $ a [ 0 ] ) { return false ; } } return true ; } $ a = array ( 50 , 75 , 150 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; if ( EqualNumbers ( $ a , $ n ) ) echo " Yes " ; else echo " No " ; #This  code is contributed by ajit..
? >


"Make array elements equal in Minimum Steps | Returns the minimum steps required to make an array of N elements equal , where the first array element equals M ; Corner Case 1 : When N = 1 ; Corner Case 2 : When N = 2 else if ( $N == 2 ) corner case 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function steps ( $ N , $ M ) { if ( $ N == 1 ) return 0 ; return $ M ; return 2 * $ M + ( $ N - 3 ) ; } $ N = 4 ; $ M = 4 ; echo steps ( $ N , $ M ) ; ? >


"Make lexicographically smallest palindrome by substituting missing characters | Function to return the lexicographically smallest palindrome that can be made from the given string after replacing the required characters ; If characters are missing at both the positions then substitute it with ' a ' ; If only str [ j ] = ' * ' then update it with the value at str [ i ] ; If only str [ i ] = ' * ' then update it with the value at str [ j ] ; If characters at both positions are not equal and != ' * ' then the string cannot be made palindrome ; Return the required palindrome ; Driver code"
How can the above be solved in PHP?

< ? php function makePalindrome ( $ str ) { $ i = 0 ; $ j = strlen ( $ str ) - 1 ; while ( $ i <= $ j ) { if ( $ str [ $ i ] == ' * ' && $ str [ $ j ] == ' * ' ) { $ str [ $ i ] = ' a ' ; $ str [ $ j ] = ' a ' ; } else if ( $ str [ $ j ] == ' * ' ) $ str [ $ j ] = $ str [ $ i ] ; else if ( $ str [ $ i ] == ' * ' ) $ str [ $ i ] = $ str [ $ j ] ; else if ( $ str [ $ i ] != $ str [ $ j ] ) return " - 1" ; $ i ++ ; $ j -- ; } return $ str ; } $ str = " na * an " ; echo makePalindrome ( $ str ) ; ? >


"Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Driver code"
How can the above be solved in PHP?

< ? php function minSum ( $ arr , $ n ) { $ sum = $ arr [ 0 ] ; $ prev = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] <= $ prev ) { $ prev = $ prev + 1 ; $ sum = $ sum + $ prev ; } else { $ sum = $ sum + $ arr [ $ i ] ; $ prev = $ arr [ $ i ] ; } } return $ sum ; } $ arr = array ( 2 , 2 , 3 , 5 , 6 ) ; $ n = count ( $ arr ) ; echo minSum ( $ arr , $ n ) ; ? >


"Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code"
How can the above be solved in PHP?


"Making elements of two arrays same with minimum increment / decrement | PHP program to find minimum increment / decrement operations to make array elements same . ; sorting both arrays in ascending order ; variable to store the final result ; After sorting both arrays Now each array is in non - decreasing order . Thus , we will now compare each element of the array and do the increment or decrement operation depending upon the value of array b [ ] . ; Driver code"
How can the above be solved in PHP?

< ? php function MinOperation ( $ a , $ b , $ n ) { sort ( $ a ) ; sort ( $ b ) ; $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ a [ $ i ] > $ b [ $ i ] ) $ result = $ result + abs ( $ a [ $ i ] - $ b [ $ i ] ) ; else if ( $ a [ $ i ] < $ b [ $ i ] ) $ result = $ result + abs ( $ a [ $ i ] - $ b [ $ i ] ) ; } return $ result ; } $ a = array ( 3 , 1 , 1 ) ; $ b = array ( 1 , 2 , 2 ) ; $ n = sizeof ( $ a ) ; echo MinOperation ( $ a , $ b , $ n ) ; ? >


"Making zero array by decrementing pairs of adjacent | PHP implementation of the above approach ; converting array element into number ; Check if divisible by 11 ; Driver Code"
How can the above be solved in PHP?

< ? php function isPossibleToZero ( $ a , $ n ) { $ num = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ num = $ num * 10 + $ a [ $ i ] ; return ( $ num % 11 == 0 ) ; } $ arr = array ( 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; if ( isPossibleToZero ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ;


"Making zero array by decrementing pairs of adjacent | PHP program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver Code"
How can the above be solved in PHP?

< ? php function isPossibleToZero ( $ a , $ n ) { $ even = 0 ; $ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i & 1 ) $ odd += $ a [ $ i ] ; else $ even += $ a [ $ i ] ; } return ( $ odd == $ even ) ; } $ arr = array ( 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; if ( isPossibleToZero ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Master Theorem For Subtract and Conquer Recurrences | PHP code for the above approach ; Driver Code"
How can the above be solved in PHP?

< ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; } $ n = 9 ; echo fib ( $ n ) ; ? >


"Match Expression where a single special character in pattern can match one or more characters | Returns true if pat matches with text ; i is used as an index in pattern and j as an index in text ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code"
How can the above be solved in PHP?

< ? php function regexMatch ( $ text , $ pat ) { $ lenText = strlen ( $ text ) ; $ letPat = strlen ( $ pat ) ; $ i = 0 ; $ j = 0 ; while ( $ i < $ letPat ) { if ( $ pat [ $ i ] != ' # ' ) { if ( $ pat [ $ i ] != $ text [ $ j ] ) return false ; $ i ++ ; $ j ++ ; } else { $ j ++ ; while ( $ text [ $ j ] != $ pat [ $ i + 1 ] ) $ j ++ ; $ i ++ ; } } return ( $ j == $ lenText ) ; } $ str = " ABABABA " ; $ pat = " A # B # A " ; if ( regexMatch ( $ str , $ pat ) ) echo " yes " ; else echo " no " ; ? >


"Matrix Chain Multiplication ( A O ( N ^ 2 ) Solution ) | Matrix Mi has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in dp [ ] [ ] . 0 th row and 0 th column of dp [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; Simply following above recursive formula . ; Driver code"
How can the above be solved in PHP?


"Matrix Exponentiation | A utility function to multiply two matrices a [ ] [ ] and b [ ] [ ] . Multiplication result is stored back in b [ ] [ ] ; Creating an auxiliary matrix to store elements of the multiplication matrix ; storing the multiplication result in a [ ] [ ] ; Updating our matrix ; Function to compute F raise to power n - 2. ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Return n 'th term of a series defined using below recurrence relation. f(n) is defined as f(n) = f(n-1) + f(n-2) + f(n-3), n>=3 Base Cases : f(0) = 0, f(1) = 1, f(2) = 1 ; Driver code"
How can the above be solved in PHP?


"Max occurring divisor in an interval | function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver code"
How can the above be solved in PHP?

< ? php function findDivisor ( $ x , $ y ) { if ( $ x == $ y ) return $ y ; return 2 ; } $ x = 3 ; $ y = 16 ; echo findDivisor ( $ x , $ y ) ; ? >


"Maximise the number of toys that can be purchased with amount K | This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment the count variable ; Driver Code"
How can the above be solved in PHP?

 < ? php function maximum_toys ( $ cost , $ N , $ K ) { $ count = 0 ; $ sum = 0 ; sort ( $ cost ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ sum + $ cost [ $ i ] <= $ K ) { $ sum = $ sum + $ cost [ $ i ] ; $ count ++ ; } } return $ count ; } $ K = 50 ; $ cost = array ( 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 ) ; $ N = count ( $ cost ) ; echo maximum_toys ( $ cost , $ N , $ K ) , "
 " ; ? >


"Maximize a value for a semicircle of given radius | Function to find the maximum value of F ; using the formula derived for getting the maximum value of F ; Drivers code"
How can the above be solved in PHP?

< ? php function maximumValueOfF ( $ R ) { return 4 * $ R * $ R + 0.25 ; } $ R = 3 ; echo maximumValueOfF ( $ R ) ; ? >


"Maximize array elements upto given number | Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index . 1. dp [ i ] [ j ] = 1 if value j can be obtained upto index i . 2. dp [ i ] [ j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is true , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code"
How can the above be solved in PHP?

< ? php function findMaxVal ( $ arr , $ n , $ num , $ maxLimit ) { $ ind ; $ val ; $ dp [ $ n ] [ $ maxLimit + 1 ] = array ( ) ; for ( $ ind = 0 ; $ ind < $ n ; $ ind ++ ) { for ( $ val = 0 ; $ val <= $ maxLimit ; $ val ++ ) { if ( $ ind == 0 ) { if ( $ num - $ arr [ $ ind ] == $ val $ num + $ arr [ $ ind ] == $ val ) { $ dp [ $ ind ] [ $ val ] = 1 ; } else { $ dp [ $ ind ] [ $ val ] = 0 ; } } else { if ( $ val - $ arr [ $ ind ] >= 0 && $ val + $ arr [ $ ind ] <= $ maxLimit ) { $ dp [ $ ind ] [ $ val ] = $ dp [ $ ind - 1 ] [ $ val - $ arr [ $ ind ] ] || $ dp [ $ ind - 1 ] [ $ val + $ arr [ $ ind ] ] ; } else if ( $ val - $ arr [ $ ind ] >= 0 ) { $ dp [ $ ind ] [ $ val ] = $ dp [ $ ind - 1 ] [ $ val - $ arr [ $ ind ] ] ; } else if ( $ val + $ arr [ $ ind ] <= $ maxLimit ) { $ dp [ $ ind ] [ $ val ] = $ dp [ $ ind - 1 ] [ $ val + $ arr [ $ ind ] ] ; } else { $ dp [ $ ind ] [ $ val ] = 0 ; } } } } for ( $ val = $ maxLimit ; $ val >= 0 ; $ val -- ) { if ( $ dp [ $ n - 1 ] [ $ val ] ) { return $ val ; } } return -1 ; } $ num = 1 ; $ arr = array ( 3 , 10 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; $ maxLimit = 15 ; echo findMaxVal ( $ arr , $ n , $ num , $ maxLimit ) ; ? >


"Maximize array elements upto given number | Utility function to find maximum possible value ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; Function to find maximum possible value that can be obtained using array elements and given number . ; variable to store maximum value that can be obtained . ; variable to store current index position . ; call to utility function to find maximum possible value that can be obtained . ; Driver code"
How can the above be solved in PHP?

< ? php function findMaxValUtil ( $ arr , $ n , $ num , $ maxLimit , $ ind , & $ ans ) { if ( $ ind == $ n ) { $ ans = max ( $ ans , $ num ) ; return ; } if ( $ num - $ arr [ $ ind ] >= 0 ) { findMaxValUtil ( $ arr , $ n , $ num - $ arr [ $ ind ] , $ maxLimit , $ ind + 1 , $ ans ) ; } if ( $ num + $ arr [ $ ind ] <= $ maxLimit ) { findMaxValUtil ( $ arr , $ n , $ num + $ arr [ $ ind ] , $ maxLimit , $ ind + 1 , $ ans ) ; } } function findMaxVal ( $ arr , $ n , $ num , $ maxLimit ) { $ ans = 0 ; $ ind = 0 ; findMaxValUtil ( $ arr , $ n , $ num , $ maxLimit , $ ind , $ ans ) ; return $ ans ; } $ num = 1 ; $ arr = array ( 3 , 10 , 6 , 4 , 5 ) ; $ n = count ( $ arr ) ; $ maxLimit = 15 ; echo ( findMaxVal ( $ arr , $ n , $ num , $ maxLimit ) ) ; ? >


"Maximize array sum after K negations | Set 1 | This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; this the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver Code"
How can the above be solved in PHP?

< ? php function maximumSum ( $ arr , $ n , $ k ) { $ INT_MAX = 0 ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { $ min = $ INT_MAX ; $ index = -1 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] < $ min ) { $ min = $ arr [ $ j ] ; $ index = $ j ; } } if ( $ min == 0 ) break ; $ arr [ $ index ] = - $ arr [ $ index ] ; } $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; return $ sum ; } $ arr = array ( -2 , 0 , 5 , -1 , 2 ) ; $ k = 4 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo maximumSum ( $ arr , $ n , $ k ) ; ? >


"Maximize profit when divisibility by two numbers have associated profits | PHP implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code"
How can the above be solved in PHP?


"Maximize the binary matrix by filpping submatrix once | PHP program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; Driver code"
How can the above be solved in PHP?


"Maximize the bitwise OR of an array | Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Driver Code"
How can the above be solved in PHP?


"Maximize the given number by replacing a segment of digits with the alternate digits given | PHP implementation of the approach Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code ; This Code is contributed is Tushill ."
How can the above be solved in PHP?


"Maximize the maximum subarray sum after removing atmost one element | Function to return the maximum sub - array sum ; Initialized ; Traverse in the array ; Increase the sum ; If sub - array sum is more than the previous ; If sum is negative ; Function that returns the maximum sub - array sum after removing an element from the same sub - array ; Maximum sub - array sum using Kadane 's Algorithm ; Re - apply Kadane 's with minor changes ; Increase the sum ; If sub - array sum is greater than the previous ; If elements are 0 , no removal ; If elements are more , then store the minimum value in the sub - array obtained till now ; If sum is negative ; Re - initialize everything ; Driver code"
How can the above be solved in PHP?

< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } function maximizeSum ( $ a , $ n ) { $ cnt = 0 ; $ mini = PHP_INT_MAX ; $ minSubarray = PHP_INT_MAX ; $ sum = maxSubArraySum ( $ a , $ n ) ; $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; $ cnt ++ ; $ minSubarray = min ( $ a [ $ i ] , $ minSubarray ) ; if ( $ sum == $ max_ending_here ) { if ( $ cnt == 1 ) $ mini = min ( $ mini , 0 ) ; else $ mini = min ( $ mini , $ minSubarray ) ; } if ( $ max_ending_here < 0 ) { $ max_ending_here = 0 ; $ cnt = 0 ; $ minSubarray = PHP_INT_MAX ; } } return $ sum - $ mini ; } $ a = array ( 1 , 2 , 3 , -2 , 3 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo maximizeSum ( $ a , $ n ) ; ? >


"Maximize the number by rearranging bits | An efficient Java program to find minimum number formed by bits of a given number . ; Returns maximum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's present in binary  representation of a. ; If a$32 bits are set . ; find a number witn n least significant set bits . ; Now shift result by 32 - n ; Driver Code"
How can the above be solved in PHP?

< ? php function _popcnt32 ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ n = $ n & ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function maximize ( $ a ) { $ n = _popcnt32 ( $ a ) ; if ( $ n == 32 ) return $ a ; $ res = ( 1 << $ n ) - 1 ; return ( $ res << ( 32 - $ n ) ) ; } $ a = 3 ; echo ( maximize ( $ a ) ) ; ? >


"Maximize the product of four factors of a Number | For calculation of a ^ b ; Function to check ; every odd and number less than 3. ; every number divisible by 4. ; every number divisible by 6. ; every number divisible by 10. ; for every even number which is not divisible by above values . ; Driver code"
How can the above be solved in PHP?

< ? php function modExp ( $ a , $ b ) { $ result = 1 ; while ( $ b > 0 ) { if ( $ b & 1 ) $ result = $ result * $ a ; $ a = $ a * $ a ; $ b /= 2 ; } return $ result ; } function check ( $ num ) { if ( $ num & 1 $ num < 3 ) return -1 ; else if ( $ num % 4 == 0 ) return modExp ( $ num / 4 , 4 ) ; else if ( $ num % 6 == 0 ) return modExp ( $ num / 3 , 2 ) * modExp ( $ num / 6 , 2 ) ; else if ( $ num % 10 == 0 ) return modExp ( $ num / 5 , 2 ) * ( $ num / 10 ) * ( $ num / 2 ) ; else return -1 ; } $ num = 10 ; echo check ( $ num ) ; ? >


"Maximize the profit by selling at | Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code"
How can the above be solved in PHP?

< ? php function solve ( $ N , $ M , & $ cp , & $ sp ) { $ profit = array_fill ( 0 , $ N , NULL ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ profit [ $ i ] = $ sp [ $ i ] - $ cp [ $ i ] ; rsort ( $ profit ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { if ( $ profit [ $ i ] > 0 ) $ sum += $ profit [ $ i ] ; else break ; } return $ sum ; } $ N = 5 ; $ M = 3 ; $ CP = array ( 5 , 10 , 35 , 7 , 23 ) ; $ SP = array ( 11 , 10 , 0 , 9 , 19 ) ; echo solve ( $ N , $ M , $ CP , $ SP ) ; ? >


"Maximize the subarray sum after multiplying all elements of any subarray with X | PHP implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code"
How can the above be solved in PHP?

< ? php $ N = 5 ; function func ( $ idx , $ cur , $ a , $ dp , $ n , $ x ) { if ( $ idx == $ n ) return 0 ; if ( $ dp [ $ idx ] [ $ cur ] != -1 ) return $ dp [ $ idx ] [ $ cur ] ; $ ans = 0 ; if ( $ cur == 0 ) { $ ans = max ( $ ans , $ a [ $ idx ] + func ( $ idx + 1 , 0 , $ a , $ dp , $ n , $ x ) ) ; $ ans = max ( $ ans , $ x * $ a [ $ idx ] + func ( $ idx + 1 , 1 , $ a , $ dp , $ n , $ x ) ) ; } else if ( $ cur == 1 ) { $ ans = max ( $ ans , $ x * $ a [ $ idx ] + func ( $ idx + 1 , 1 , $ a , $ dp , $ n , $ x ) ) ; $ ans = max ( $ ans , $ a [ $ idx ] + func ( $ idx + 1 , 2 , $ a , $ dp , $ n , $ x ) ) ; } else $ ans = max ( $ ans , $ a [ $ idx ] + func ( $ idx + 1 , 2 , $ a , $ dp , $ n , $ x ) ) ; return $ dp [ $ idx ] [ $ cur ] = $ ans ; } function getMaximumSum ( $ a , $ n , $ x ) { $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { $ dp [ $ i ] [ $ j ] = -1 ; } } $ maxi = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ maxi = max ( $ maxi , func ( $ i , 0 , $ a , $ dp , $ n , $ x ) ) ; return $ maxi ; } $ a = array ( -3 , 8 , -2 , 1 , -6 ) ; $ n = count ( $ a ) ; $ x = -1 ; echo getMaximumSum ( $ a , $ n , $ x ) ; ? >


"Maximize the sum of arr [ i ] * i | PHP program to find the maximum value of i * arr [ i ] ; Sort the array ; Finding the sum of arr [ i ] * i ; Driver Code"
How can the above be solved in PHP?

< ? php function maxSum ( $ arr , $ n ) { sort ( $ arr ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( $ arr [ $ i ] * $ i ) ; return $ sum ; } $ arr = array ( 3 , 5 , 6 , 1 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , $ n ) ; ? >


"Maximize the sum of array by multiplying prefix of array with | PHP implementation of the approach ; To store sum ; To store ending indices of the chosen prefix arrays ; Adding the absolute value of a [ i ] ; If i == 0 then there is no index to be flipped in ( i - 1 ) position ; print the maximised sum ; print the ending indices of the chosen prefix arrays ; Driver Code"
How can the above be solved in PHP?


"Maximize the sum of products of the degrees between any two vertices of the tree | Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code"
How can the above be solved in PHP?

< ? php function maxSum ( $ N ) { $ ans = 0 ; for ( $ u = 1 ; $ u <= $ N ; $ u ++ ) { for ( $ v = 1 ; $ v <= $ N ; $ v ++ ) { if ( $ u == $ v ) continue ; $ degreeU = 2 ; if ( $ u == 1 $ u == $ N ) $ degreeU = 1 ; $ degreeV = 2 ; if ( $ v == 1 $ v == $ N ) $ degreeV = 1 ; $ ans += ( $ degreeU * $ degreeV ) ; } } return $ ans ; } $ N = 6 ; echo maxSum ( $ N ) ; ? >


"Maximize the value of the given expression | Function to return the maximum result ; To store the count of negative integers ; Sum of all the three integers ; Product of all the three integers ; To store the smallest and the largest among all the three integers ; Calculate the count of negative integers ; Depending upon count of negatives ; When all three are positive integers ; For single negative integer ; For two negative integers ; For three negative integers ; Driver Code"
How can the above be solved in PHP?

< ? php function maximumResult ( $ a , $ b , $ c ) { $ countOfNegative = 0 ; $ sum = $ a + $ b + $ c ; $ product = $ a * $ b * $ c ; $ largest = max ( $ a , $ b , $ c ) ; $ smallest = min ( $ a , $ b , $ c ) ; if ( $ a < 0 ) $ countOfNegative ++ ; if ( $ b < 0 ) $ countOfNegative ++ ; if ( $ c < 0 ) $ countOfNegative ++ ; switch ( $ countOfNegative ) { case 0 : return ( $ sum - $ largest ) * $ largest ; case 1 : return ( $ product / $ smallest ) + $ smallest ; case 2 : return ( $ product / $ largest ) + $ largest ; case 3 : return ( $ sum - $ smallest ) * $ smallest ; } } $ a = -2 ; $ b = -1 ; $ c = -4 ; echo maximumResult ( $ a , $ b , $ c ) ; ? >


"Maximize the value of x + y + z such that ax + by + cz = n | Function to return the maximum value of ( x + y + z ) such that ( ax + by + cz = n ) ; i represents possible values of a * x ; j represents possible values of b * y ; If z is an integer ; Driver code ; Function Call"
How can the above be solved in PHP?


"Maximizing Probability of one type from N containers | Returns the Maximum probability for Drawing 1 copy of number A from N containers with N copies each of numbers A and B ; Pmax = N / ( N + 1 ) ; 1. N = 1 ; 2. N = 2 ; 3. N = 10"
How can the above be solved in PHP?

 < ? php function calculateProbability ( $ N ) { $ probability = $ N / ( $ N + 1 ) ; return $ probability ; } $ N = 1 ; $ probabilityMax = calculateProbability ( $ N ) ; echo ( " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " . $ N . " is , " round ( $ probabilityMax , 4 ) . "
 " ) ; $ N = 2 ; $ probabilityMax = calculateProbability ( $ N ) ; echo ( " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " . $ N . " is , " round ( $ probabilityMax , 4 ) . "
 " ) ; $ N = 10 ; $ probabilityMax = calculateProbability ( $ N ) ; echo ( " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " . $ N . " is , " round ( $ probabilityMax , 4 ) . "
 " ) ; ? >


"Maximum AND value of a pair in an array | Function for finding maximum and value pair ; Driver Code"
How can the above be solved in PHP?

< ? php function maxAND ( $ arr , $ n ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ res = max ( $ res , $ arr [ $ i ] & $ arr [ $ j ] ) ; return $ res ; } $ arr = array ( 4 , 8 , 6 , 2 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ AND ▁ Value ▁ = ▁ " , maxAND ( $ arr , $ n ) ; ? >


"Maximum AND value of a pair in an array | Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Driver Code"
How can the above be solved in PHP?

< ? php function checkBit ( $ pattern , $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ( $ pattern & $ arr [ $ i ] ) == $ pattern ) $ count ++ ; return $ count ; } function maxAND ( $ arr , $ n ) { $ res = 0 ; $ count ; for ( $ bit = 31 ; $ bit >= 0 ; $ bit -- ) { $ count = checkBit ( $ res | ( 1 << $ bit ) , $ arr , $ n ) ; if ( $ count >= 2 ) $ res |= ( 1 << $ bit ) ; } return $ res ; } $ arr = array ( 4 , 8 , 6 , 2 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ AND ▁ Value ▁ = ▁ " , maxAND ( $ arr , $ n ) ; ? >


"Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; If there are only two values in the range [ L , R ] ; Driver code"
How can the above be solved in PHP?


"Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code"
How can the above be solved in PHP?

< ? php function maxAND ( $ L , $ R ) { $ maximum = $ L & $ R ; for ( $ i = $ L ; $ i < $ R ; $ i ++ ) for ( $ j = $ i + 1 ; $ j <= $ R ; $ j ++ ) $ maximum = max ( $ maximum , ( $ i & $ j ) ) ; return $ maximum ; } $ L = 1 ; $ R = 632 ; echo ( maxAND ( $ L , $ R ) ) ; ? >


"Maximum Consecutive Zeroes in Concatenated Binary String | returns the maximum size of a substring consisting only of zeroes after k concatenation ; stores the maximum length of the required substring ; if the current character is 0 ; stores maximum length of current substrings with zeroes ; if the whole $is filled with zero ; computes the length of the maximal prefix which contains only zeroes ; computes the length of the maximal suffix which contains only zeroes ; if more than 1 concatenations are to be made ; Driver code"
How can the above be solved in PHP?

< ? php function max_length_substring ( $ st , $ n , $ k ) { $ max_len = 0 ; $ len = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ st [ $ i ] == '0' ) $ len ++ ; else $ len = 0 ; $ max_len = max ( $ max_len , $ len ) ; } if ( $ max_len == $ n ) return $ n * $ k ; $ pref = 0 ; $ suff = 0 ; for ( $ i = 0 ; $ st [ $ i ] == '0' ; ++ $ i , ++ $ pref ) ; for ( $ i = $ n - 1 ; $ st [ $ i ] == '0' ; -- $ i , ++ $ suff ) ; if ( $ k > 1 ) $ max_len = max ( $ max_len , $ pref + $ suff ) ; return $ max_len ; } $ n = 6 ; $ k = 3 ; $ st = "110010" ; $ ans = max_length_substring ( $ st , $ n , $ k ) ; echo $ ans ; ? >


"Maximum GCD from Given Product of Unknowns | Function to return the required gcd ; Count the number of times 2 divides p ; Equivalent to p = p / 2 ; ; If 2 divides p ; Check all the possible numbers that can divide p ; If n in the end is a prime number ; Return the required gcd ; Driver code"
How can the above be solved in PHP?


"Maximum OR sum of sub | function to find maximum OR sum ; OR sum of all the elements in both arrays ; Driver Code"
How can the above be solved in PHP?


"Maximum Primes whose sum is equal to given N | Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code"
How can the above be solved in PHP?

< ? php function maxPrimes ( $ n ) { return ( int ) ( $ n / 2 ) ; } $ n = 17 ; echo maxPrimes ( $ n ) ; ? >


"Maximum Product Cutting | DP | The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver code"
How can the above be solved in PHP?

< ? php function maxProd ( $ n ) { if ( $ n == 2 $ n == 3 ) return ( $ n - 1 ) ; $ res = 1 ; while ( $ n > 4 ) { $ n = $ n - 3 ; $ res = $ res * 3 ; } return ( $ n * $ res ) ; } echo ( " Maximum ▁ Product ▁ is ▁ " ) ; echo ( maxProd ( 10 ) ) ; ? >


"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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"Maximum Product Subarray | Added negative product case | Function to find maximum subarray product . ; As maximum product can be negative , so initialize ans with minimum integer value . ; Variable to store maximum product until current value . ; Variable to store minimum product until current value . ; Variable used during updation of maximum product and minimum product . is prevMax ; If current element is positive , update maxval . Update minval if it is negative . ; If current element is zero , maximum product cannot end at current element . Update minval with 1 and maxval with 0. maxval is updated to 0 as in case all other elements are negative , then maxval is 0. ; If current element is negative , then new value of maxval is previous minval * arr [ i ] and new value of minval is previous maxval * arr [ i ] . Before updating maxval , store its previous value in prevMax to be used to update minval . ; Update ans if necessary . ; If maxval is zero , then to calculate product for next iteration , it should be set to 1 as maximum product subarray does not include 0. The minimum possible value to be considered in maximum product subarray is already stored in minval , so when maxval is negative it is set to 1. ; Driver Code"
How can the above be solved in PHP?

< ? php function findMaxProduct ( & $ arr , $ n ) { $ ans = 0 ; $ maxval = 1 ; $ minval = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) { $ maxval = $ maxval * $ arr [ i ] ; $ minval = min ( 1 , $ minval * $ arr [ $ i ] ) ; } else if ( $ arr [ $ i ] == 0 ) { $ minval = 1 ; $ maxval = 0 ; } else if ( $ arr [ $ i ] < 0 ) { $ prevMax = $ maxval ; $ maxval = $ minval * $ arr [ $ i ] ; $ minval = $ prevMax * $ arr [ $ i ] ; } $ ans = max ( $ ans , $ maxval ) ; if ( $ maxval <= 0 ) { $ maxval = 1 ; } } return $ ans ; } $ arr = array ( 0 , -4 , 0 , -2 ) ; $ n = sizeof ( $ arr ) ; echo findMaxProduct ( $ 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"
How can the above be solved in PHP?

 < ? 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
 " ) ; } else { echo ( " The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ▁ " . $ maxSubarraySum . "
 " ) ; } } $ A = array ( 3 , 4 , 5 , -4 , 6 ) ; $ B = array ( 1 , 8 , 5 ) ; $ n = count ( $ A ) ; $ m = count ( $ B ) ; findMaxSubarraySum ( $ A , $ B , $ n , $ m ) ; ? >


"Maximum Subarray Sum after inverting at most two elements | Function to return the maximum required sub - array sum ; Creating one based indexing ; 2d array to contain solution for each step ; Case 1 : Choosing current or ( current + previous ) whichever is smaller ; Case 2 : ( a ) Altering sign and add to previous case 1 or value 0 ; Case 2 : ( b ) Adding current element with previous case 2 and updating the maximum ; Case 3 : ( a ) Altering sign and add to previous case 2 ; Case 3 : ( b ) Adding current element with previous case 3 ; Updating the maximum value of variable ans ; Return the final solution ; Driver code"
How can the above be solved in PHP?


"Maximum Subarray Sum using Divide and Conquer algorithm | Find the maximum possible sum in arr [ ] such that arr [ m ] is part of it ; Include elements on left of mid . ; Include elements on right of mid ; Return sum of elements on left and right of mid returning only left_sum + right_sum will fail for [ - 2 , 1 ] ; Returns sum of maximum sum subarray in aa [ l . . h ] ; Base Case : Only one element ; Find middle point ; Return maximum of following three possible cases a ) Maximum subarray sum in left half b ) Maximum subarray sum in right half c ) Maximum subarray sum such that the subarray crosses the midpoint ; Driver Code"
How can the above be solved in PHP?

< ? php function maxCrossingSum ( & $ arr , $ l , $ m , $ h ) { $ sum = 0 ; $ left_sum = PHP_INT_MIN ; for ( $ i = $ m ; $ i >= $ l ; $ i -- ) { $ sum = $ sum + $ arr [ $ i ] ; if ( $ sum > $ left_sum ) $ left_sum = $ sum ; } $ sum = 0 ; $ right_sum = PHP_INT_MIN ; for ( $ i = $ m + 1 ; $ i <= $ h ; $ i ++ ) { $ sum = $ sum + $ arr [ $ i ] ; if ( $ sum > $ right_sum ) $ right_sum = $ sum ; } return max ( $ left_sum + $ right_sum , $ left_sum , $ right_sum ) ; } function maxSubArraySum ( & $ arr , $ l , $ h ) { if ( $ l == $ h ) return $ arr [ $ l ] ; $ m = intval ( ( $ l + $ h ) / 2 ) ; return max ( maxSubArraySum ( $ arr , $ l , $ m ) , maxSubArraySum ( $ arr , $ m + 1 , $ h ) , maxCrossingSum ( $ arr , $ l , $ m , $ h ) ) ; } $ arr = array ( 2 , 3 , 4 , 5 , 7 ) ; $ n = count ( $ arr ) ; $ max_sum = maxSubArraySum ( $ arr , 0 , $ n - 1 ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_sum ; ? >


"Maximum Sum Increasing Subsequence | DP | maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code"
How can the above be solved in PHP?

< ? php function maxSumIS ( $ arr , $ n ) { $ max = 0 ; $ msis = array ( $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ msis [ $ i ] = $ arr [ $ i ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ msis [ $ i ] < $ msis [ $ j ] + $ arr [ $ i ] ) $ msis [ $ i ] = $ msis [ $ j ] + $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max < $ msis [ $ i ] ) $ max = $ msis [ $ i ] ; return $ max ; } $ arr = array ( 1 , 101 , 2 , 3 , 100 , 4 , 5 ) ; $ n = count ( $ arr ) ; echo " Sum ▁ of ▁ maximum ▁ sum ▁ increasing ▁ subsequence ▁ is ▁ " . maxSumIS ( $ arr , $ n ) ; ? >


"Maximum Sum Path in Two Arrays | This function returns the sum of elements on maximum path from beginning to end ; initialize indexes for ar1 [ ] and ar2 [ ] ; Initialize result and current sum through ar1 [ ] and ar2 [ ] . ; Below 3 loops are similar to merge in merge sort ; Add elements of ar1 [ ] to sum1 ; Add elements of ar2 [ ] to sum2 ; we reached a common point ; Take the maximum of two sums and add to result Also add the common element of array , once ; Update sum1 and sum2 for elements after this intersection point ; update i and j to move to next element of each array ; Add remaining elements of ar1 [ ] ; Add remaining elements of ar2 [ ] ; Add maximum of two sums of remaining elements ; Driver Code ; Function call"
How can the above be solved in PHP?


"Maximum Sum of Products of Two Arrays | Function that calculates maximum sum of products of two arrays ; Variable to store the sum of products of array elements ; length of the arrays ; Sorting both the arrays ; Traversing both the arrays and calculating sum of product ; Driver code"
How can the above be solved in PHP?

< ? php function maximumSOP ( & $ a , & $ b ) { $ sop = 0 ; $ n = sizeof ( $ a ) ; sort ( $ a ) ; sort ( $ b ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sop = $ sop + ( $ a [ $ i ] * $ b [ $ i ] ) ; } return $ sop ; } $ A = array ( 1 , 2 , 3 ) ; $ B = array ( 4 , 5 , 1 ) ; echo maximumSOP ( $ A , $ B ) ; ? >


"Maximum XOR using K numbers from 1 to n | To return max xor sum of 1 to n using at most k numbers ; If k is 1 then maximum possible sum is n ; Finding number greater than or equal to n with most significant bit same as n . For example , if n is 4 , result is 7. If n is 5 or 6 , result is 7 ; Return res - 1 which denotes a number with all bits set to 1 ; Driver code"
How can the above be solved in PHP?

< ? php function maxXorSum ( $ n , $ k ) { if ( $ k == 1 ) return $ n ; $ res = 1 ; while ( $ res <= $ n ) $ res <<= 1 ; return $ res - 1 ; } $ n = 4 ; $ k = 3 ; echo maxXorSum ( $ n , $ k ) ; ? >


"Maximum XOR value in matrix | PHP program to Find maximum XOR value in matrix either row or column wise maximum number of row and column ; function return the maximum xor value that is either row or column wise ; for row xor and column xor ; traverse matrix ; xor row element ; for each column : j is act as row & i act as column xor column element ; update maximum between r_xor , c_xor ; return maximum xor value ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 1000 ; function maxXOR ( $ mat , $ N ) { $ r_xor ; $ c_xor ; $ max_xor = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ r_xor = 0 ; $ c_xor = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { $ r_xor = $ r_xor ^ $ mat [ $ i ] [ $ j ] ; $ c_xor = $ c_xor ^ $ mat [ $ j ] [ $ i ] ; } if ( $ max_xor < max ( $ r_xor , $ c_xor ) ) $ max_xor = max ( $ r_xor , $ c_xor ) ; } return $ max_xor ; } $ N = 3 ; $ mat = array ( array ( 1 , 5 , 4 ) , array ( 3 , 7 , 2 ) , array ( 5 , 9 , 10 ) ) ; echo " maximum ▁ XOR ▁ value ▁ : ▁ " , maxXOR ( $ mat , $ N ) ; ? >


"Maximum XOR value of a pair from a range | method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver Code"
How can the above be solved in PHP?

 < ? php function maxXORInRange ( $ L , $ R ) { $ LXR = $ L ^ $ R ; $ msbPos = 0 ; while ( $ LXR ) { $ msbPos ++ ; $ LXR >>= 1 ; } $ maxXOR = 0 ; $ two = 1 ; while ( $ msbPos -- ) { $ maxXOR += $ two ; $ two <<= 1 ; } return $ maxXOR ; } $ L = 8 ; $ R = 20 ; echo maxXORInRange ( $ L , $ R ) , "
 " ; ? >


"Maximum XOR | function to calculate maximum XOR value ; Return ( 2 ^ c - 1 ) ; Driver Code"
How can the above be solved in PHP?


"Maximum absolute difference between sum of two contiguous sub | Find maximum subarray sum for subarray [ 0. . i ] using standard Kadane ' s ▁ algorithm . ▁ ▁ This ▁ version ▁ of ▁ Kadane ' s Algorithm will work if all numbers are negative ; Find maximum subarray sum for subarray [ i . . n ] using Kadane ' s ▁ algorithm . ▁ This ▁ ▁ version ▁ of ▁ Kadane ' s Algorithm will work if all numbers are negative ; The function finds two non - overlapping contiguous sub - arrays such that the absolute difference between the sum of two sub - array is maximum . ; create and build an array that stores maximum sums of subarrays that lie in arr [ 0. . . i ] ; create and build an array that stores maximum sums of subarrays that lie in arr [ i + 1. . . n - 1 ] ; Invert array ( change sign ) to find minumum sum subarrays ; create and build an array that stores minimum sums of subarrays that lie in arr [ 0. . . i ] ; create and build an array that stores minimum sums of subarrays that lie in arr [ i + 1. . . n - 1 ] ; For each index i , take maximum of 1. abs ( max sum subarray that lies in arr [ 0. . . i ] - min sum subarray that lies in arr [ i + 1. . . n - 1 ] ) 2. abs ( min sum subarray that lies in arr [ 0. . . i ] - max sum subarray that lies in arr [ i + 1. . . n - 1 ] ) ; Driver Code"
How can the above be solved in PHP?

< ? php function maxLeftSubArraySum ( & $ a , $ size , & $ sum ) { $ max_so_far = $ a [ 0 ] ; $ curr_max = $ a [ 0 ] ; $ sum [ 0 ] = $ max_so_far ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) { $ curr_max = max ( $ a [ $ i ] , $ curr_max + $ a [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; $ sum [ $ i ] = $ max_so_far ; } return $ max_so_far ; } function maxRightSubArraySum ( & $ a , $ n , & $ sum ) { $ max_so_far = $ a [ $ n ] ; $ curr_max = $ a [ $ n ] ; $ sum [ $ n ] = $ max_so_far ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ curr_max = max ( $ a [ $ i ] , $ curr_max + $ a [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; $ sum [ $ i ] = $ max_so_far ; } return $ max_so_far ; } function findMaxAbsDiff ( & $ arr , $ n ) { $ leftMax = array_fill ( 0 , $ n , NULL ) ; maxLeftSubArraySum ( $ arr , $ n , $ leftMax ) ; $ rightMax = array_fill ( 0 , $ n , NULL ) ; maxRightSubArraySum ( $ arr , $ n - 1 , $ rightMax ) ; $ invertArr = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ invertArr [ $ i ] = - $ arr [ $ i ] ; $ leftMin = array_fill ( 0 , $ n , NULL ) ; maxLeftSubArraySum ( $ invertArr , $ n , $ leftMin ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ leftMin [ $ i ] = - $ leftMin [ $ i ] ; $ rightMin = array_fill ( 0 , $ n , NULL ) ; maxRightSubArraySum ( $ invertArr , $ n - 1 , $ rightMin ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ rightMin [ $ i ] = - $ rightMin [ $ i ] ; $ result = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ absValue = max ( abs ( $ leftMax [ $ i ] - $ rightMin [ $ i + 1 ] ) , abs ( $ leftMin [ $ i ] - $ rightMax [ $ i + 1 ] ) ) ; if ( $ absValue > $ result ) $ result = $ absValue ; } return $ result ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; echo findMaxAbsDiff ( $ a , $ n ) ; ? >


"Maximum absolute difference of value and index sums | Brute force PHP program to calculate the maximum absolute difference of an array . ; Utility function to calculate the value of absolute difference for the pair ( i , j ) . ; Function to return maximum absolute difference in brute force . ; Variable for storing the maximum absolute distance throughout the traversal of loops . ; Iterate through all pairs . ; If the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; Driver Code"
How can the above be solved in PHP?

< ? php function calculateDiff ( $ i , $ j , $ arr ) { return abs ( $ arr [ $ i ] - $ arr [ $ j ] ) + abs ( $ i - $ j ) ; } function maxDistance ( $ arr , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( calculateDiff ( $ i , $ j , $ arr ) > $ result ) $ result = calculateDiff ( $ i , $ j , $ arr ) ; } } return $ result ; } $ arr = array ( -70 , -64 , -6 , -56 , 64 , 61 , -57 , 16 , 48 , -98 ) ; $ n = sizeof ( $ arr ) ; echo maxDistance ( $ arr , $ n ) ; ? >


"Maximum absolute difference of value and index sums | Function to return maximum absolute difference in linear time . ; max and min variables as described in algorithm . ; Updating max and min variables as described in algorithm . ; Calculating maximum absolute difference . ; Driver Code"
How can the above be solved in PHP?

< ? php function maxDistance ( $ arr , $ n ) { $ max1 = PHP_INT_MIN ; $ min1 = PHP_INT_MAX ; $ max2 = PHP_INT_MIN ; $ min2 = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ max1 = max ( $ max1 , $ arr [ $ i ] + $ i ) ; $ min1 = min ( $ min1 , $ arr [ $ i ] + $ i ) ; $ max2 = max ( $ max2 , $ arr [ $ i ] - $ i ) ; $ min2 = min ( $ min2 , $ arr [ $ i ] - $ i ) ; } return max ( $ max1 - $ min1 , $ max2 - $ min2 ) ; } $ arr = array ( -70 , -64 , -6 , -56 , 64 , 61 , -57 , 16 , 48 , -98 ) ; $ n = count ( $ arr ) ; echo maxDistance ( $ arr , $ n ) ; ? >


"Maximum and Minimum Values of an Algebraic Expression | PHP program to find the maximum and minimum values of an Algebraic expression of given form ; Finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by 50 to avoid negative indexing in array ; Driver Code"
How can the above be solved in PHP?


"Maximum and Minimum in a square matrix . | PHP program for finding maximum and minimum in a matrix . ; Finds maximum and minimum in arr [ 0. . n - 1 ] [ 0. . n - 1 ] using pair wise comparisons ; Traverses rows one by one ; Compare elements from beginning and end of current row ; Driver Code"
How can the above be solved in PHP?


"Maximum and minimum sums from two numbers with digit replacements | Find new value of x after replacing digit " from " to " to " ; Required digit found , replace it ; Returns maximum and minimum possible sums of x1 and x2 if digit replacements are allowed . ; We always get minimum sum if we replace 6 with 5. ; We always get maximum sum if we replace 5 with 6. ; Driver code"
How can the above be solved in PHP?

 < ? php function replaceDig ( $ x , $ from , $ to ) { $ result = 0 ; $ multiply = 1 ; while ( $ x > 0 ) { $ reminder = $ x % 10 ; if ( $ reminder == $ from ) $ result = $ result + $ to * $ multiply ; else $ result = $ result + $ reminder * $ multiply ; $ multiply *= 10 ; $ x = $ x / 10 ; } return $ result ; } function calculateMinMaxSum ( $ x1 , $ x2 ) { $ minSum = replaceDig ( $ x1 , 6 , 5 ) + replaceDig ( $ x2 , 6 , 5 ) ; $ maxSum = replaceDig ( $ x1 , 5 , 6 ) + replaceDig ( $ x2 , 5 , 6 ) ; echo " Minimum sum = " ▁ , ▁ $ minSum , " " ;
 	 echo ▁ " Maximum sum = " } $ x1 = 5466 ; $ x2 = 4555 ; calculateMinMaxSum ( $ x1 , $ x2 ) ; ? >


"Maximum area of quadrilateral | PHP program to find maximum are of a quadrilateral ; Calculating the semi - perimeter of the given quadrilateral ; Applying Brahmagupta 's formula to  get maximum area of quadrilateral ; Driver code"
How can the above be solved in PHP?

< ? php function maxArea ( $ a , $ b , $ c , $ d ) { $ semiperimeter = ( $ a + $ b + $ c + $ d ) / 2 ; return sqrt ( ( $ semiperimeter - $ a ) * ( $ semiperimeter - $ b ) * ( $ semiperimeter - $ c ) * ( $ semiperimeter - $ d ) ) ; } $ a = 1 ; $ b = 2 ; $ c = 1 ; $ d = 2 ; echo ( maxArea ( $ a , $ b , $ c , $ d ) ) ; ? >


"Maximum area of rectangle possible with given perimeter | Function to find max area ; return area ; Driver code"
How can the above be solved in PHP?

< ? php function maxArea ( $ perimeter ) { $ length = ( int ) ceil ( $ perimeter / 4 ) ; $ breadth = ( int ) floor ( $ perimeter / 4 ) ; return ( $ length * $ breadth ) ; } $ n = 38 ; echo " Maximum ▁ Area ▁ = ▁ " , maxArea ( $ n ) ; ? >


"Maximum area rectangle by picking four sides from array | function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; Driver Code"
How can the above be solved in PHP?

< ? php function findArea ( $ arr , $ n ) { rsort ( $ arr ) ; $ dimension = array ( 0 , 0 ) ; for ( $ i = 0 , $ j = 0 ; $ i < $ n - 1 && $ j < 2 ; $ i ++ ) if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ dimension [ $ j ++ ] = $ arr [ $ i ++ ] ; return ( $ dimension [ 0 ] * $ dimension [ 1 ] ) ; } $ arr = array ( 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ) ; $ n = count ( $ arr ) ; echo findArea ( $ arr , $ n ) ; ? >


"Maximum array sum that can be obtained after exactly k changes | Utility function to return the sum of the array elements ; Function to return the maximized sum of the array after performing the given operation exactly k times ; Sort the array elements ; Change signs of the negative elements starting from the smallest ; If a single operation has to be performed then it must be performed on the smallest positive element ; To store the index of the minimum element ; Update the minimum index ; Perform remaining operation on the smallest element ; Return the sum of the updated array ; Driver code"
How can the above be solved in PHP?


"Maximum binomial coefficient term value | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return maximum binomial coefficient term value . ; if n is even ; if n is odd ; Driver Code"
How can the above be solved in PHP?

 < ? php function binomialCoeff ( $ n , $ k ) { $ C [ $ n + 1 ] [ $ k + 1 ] = array ( 0 ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= min ( $ i , $ k ) ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ C [ $ i ] [ $ j ] = 1 ; else $ C [ $ i ] [ $ j ] = $ C [ $ i - 1 ] [ $ j - 1 ] + $ C [ $ i - 1 ] [ $ j ] ; } } return $ C [ $ n ] [ $ k ] ; } function maxcoefficientvalue ( $ n ) { if ( $ n % 2 == 0 ) return binomialCoeff ( $ n , $ n / 2 ) ; else return binomialCoeff ( $ n , ( $ n + 1 ) / 2 ) ; } $ n = 4 ; echo maxcoefficientvalue ( $ n ) , "
 " ; ? >


"Maximum consecutive repeating character in string | Returns the maximum repeating character in a given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code"
How can the above be solved in PHP?

< ? php function maxRepeating ( $ str ) { $ n = strlen ( $ str ) ; $ count = 0 ; $ res = $ str [ 0 ] ; $ cur_count = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i < $ n - 1 && $ str [ $ i ] == $ str [ $ i + 1 ] ) $ cur_count ++ ; else { if ( $ cur_count > $ count ) { $ count = $ cur_count ; $ res = $ str [ $ i ] ; } $ cur_count = 1 ; } } return $ res ; } $ str = " aaaabbaaccde " ; echo maxRepeating ( $ str ) ; ? >