"Maximum consecutive repeating character in string | function to find out the maximum repeating character in given string ; Find the maximum repeating character starting from str [ i ] ; Update result if required ; Driver code"
How can the above be solved in PHP?

< ? php function maxRepeating ( $ str ) { $ len = strlen ( $ str ) ; $ count = 0 ; $ res = $ str [ 0 ] ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ cur_count = 1 ; for ( $ j = $ i + 1 ; $ j < $ len ; $ j ++ ) { if ( $ str [ $ i ] != $ str [ $ j ] ) break ; $ cur_count ++ ; } if ( $ cur_count > $ count ) { $ count = $ cur_count ; $ res = $ str [ $ i ] ; } } return $ res ; } $ str = " aaaabbaaccde " ; echo maxRepeating ( $ str ) ; ? >


"Maximum count of equal numbers in an array after performing given operations | Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver Code ; size of an array"
How can the above be solved in PHP?

< ? php function EqualNumbers ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; if ( $ sum % $ n ) return $ n - 1 ; return $ n ; } $ a = array ( 1 , 4 , 1 ) ; $ n = sizeof ( $ a ) ; echo EqualNumbers ( $ a , $ n ) ;


"Maximum decimal value path in a binary matrix | Returns maximum decimal value in binary matrix . Here p indicate power of 2 ; Out of matrix boundary ; If current matrix value is 1 then return result + power ( 2 , p ) else result ; Driver Code"
How can the above be solved in PHP?

< ? php function maxDecimalValue ( $ mat , $ i , $ j , $ p ) { $ N = 4 ; if ( $ i >= $ N $ j >= $ N ) return 0 ; $ result = max ( maxDecimalValue ( $ mat , $ i , $ j + 1 , $ p + 1 ) , maxDecimalValue ( $ mat , $ i + 1 , $ j , $ p + 1 ) ) ; if ( $ mat [ $ i ] [ $ j ] == 1 ) return pow ( 2 , $ p ) + $ result ; else return $ result ; } $ mat = array ( array ( 1 , 1 , 0 , 1 ) , array ( 0 , 1 , 1 , 0 ) , array ( 1 , 0 , 0 , 1 ) , array ( 1 , 0 , 1 , 1 ) ) ; echo maxDecimalValue ( $ mat , 0 , 0 , 0 ) ; ? >


"Maximum difference between group of k | utility function for array sum ; function for finding maximum group difference of array ; sort the array ; find array sum ; difference for k - smallest diff1 = ( arraysum - k_smallest ) - k_smallest ; reverse array for finding sum 0f 1 st k - largest ; difference for k - largest diff2 = ( arraysum - k_largest ) - k_largest ; return maximum difference value ; Driver Code"
How can the above be solved in PHP?

< ? php function arraySum ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; return $ sum ; } function maxDiff ( $ arr , $ n , $ k ) { sort ( $ arr ) ; $ arraysum = arraySum ( $ arr , $ n ) ; $ diff1 = abs ( $ arraysum - 2 * arraySum ( $ arr , $ k ) ) ; array_reverse ( $ arr ) ; $ diff2 = abs ( $ arraysum - 2 * arraySum ( $ arr , $ k ) ) ; return ( max ( $ diff1 , $ diff2 ) ) ; } $ arr = array ( 1 , 7 , 4 , 8 , -1 , 5 , 2 , 1 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Maximum ▁ Difference ▁ = ▁ " , maxDiff ( $ arr , $ n , $ k ) ; ? >


"Maximum difference between groups of size two | PHP program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Driver code"
How can the above be solved in PHP?

 < ? php function CalculateMax ( $ arr , $ n ) { sort ( $ arr ) ; $ min_sum = $ arr [ 0 ] + $ arr [ 1 ] ; $ max_sum = $ arr [ $ n - 1 ] + $ arr [ $ n - 2 ] ; return abs ( $ max_sum - $ min_sum ) ; } $ arr = array ( 6 , 7 , 1 , 11 ) ; $ n = sizeof ( $ arr ) ; echo CalculateMax ( $ arr , $ n ) , "
 " ; ? >


"Maximum difference between two elements in an Array | Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code"
How can the above be solved in PHP?

< ? php function maxAbsDiff ( $ arr , $ n ) { $ minEle = $ arr [ 0 ] ; $ maxEle = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ minEle = min ( $ minEle , $ arr [ $ i ] ) ; $ maxEle = max ( $ maxEle , $ arr [ $ i ] ) ; } return ( $ maxEle - $ minEle ) ; } $ arr = array ( 2 , 1 , 5 , 3 ) ; $ n = sizeof ( $ arr ) ; echo maxAbsDiff ( $ arr , $ n ) ;


"Maximum difference between two elements such that larger element appears after the smaller number | The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Driver Code ; Function calling"
How can the above be solved in PHP?

< ? php function maxDiff ( $ arr , $ arr_size ) { $ max_diff = $ arr [ 1 ] - $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ arr_size ; $ j ++ ) { if ( $ arr [ $ j ] - $ arr [ $ i ] > $ max_diff ) $ max_diff = $ arr [ $ j ] - $ arr [ $ i ] ; } } return $ max_diff ; } $ arr = array ( 1 , 2 , 90 , 10 , 110 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ difference ▁ is ▁ " . maxDiff ( $ arr , $ n ) ;


"Maximum difference between two elements such that larger element appears after the smaller number | The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize Result ; Initialize max element from right side ; Driver Code ; Function calling"
How can the above be solved in PHP?

< ? php function maxDiff ( $ arr , $ n ) { $ maxDiff = -1 ; $ maxRight = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] > $ maxRight ) $ maxRight = $ arr [ $ i ] ; else { $ diff = $ maxRight - $ arr [ $ i ] ; if ( $ diff > $ maxDiff ) { $ maxDiff = $ diff ; } } } return $ maxDiff ; } $ arr = array ( 1 , 2 , 90 , 10 , 110 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ difference ▁ is ▁ " , maxDiff ( $ arr , $ n ) ; ? >


"Maximum difference between two elements such that larger element appears after the smaller number | The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize diff , current sum and max sum ; Calculate current diff ; Calculate current sum ; Update max sum , if needed ; Driver Code ; Function calling"
How can the above be solved in PHP?

< ? php function maxDiff ( $ arr , $ n ) { $ diff = $ arr [ 1 ] - $ arr [ 0 ] ; $ curr_sum = $ diff ; $ max_sum = $ curr_sum ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { $ diff = $ arr [ $ i + 1 ] - $ arr [ $ i ] ; if ( $ curr_sum > 0 ) $ curr_sum += $ diff ; else $ curr_sum = $ diff ; if ( $ curr_sum > $ max_sum ) $ max_sum = $ curr_sum ; } return $ max_sum ; } $ arr = array ( 80 , 2 , 6 , 3 , 100 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ difference ▁ is ▁ " , maxDiff ( $ arr , $ n ) ; ? >


"Maximum difference of sum of elements in two rows in a matrix | PHP program to find maximum difference of sum of elements of two rows ; Function to find maximum difference of sum of elements of two rows such that second row appears before first row . ; auxiliary array to store sum of all elements of each row ; calculate sum of each row and store it in rowSum array ; calculating maximum difference of two elements such that rowSum [ i ] < rowsum [ j ] ; if current difference is greater than previous then update it ; if new element is less than previous minimum element then update it so that we may get maximum difference in remaining array ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function maxRowDiff ( $ mat , $ m , $ n ) { global $ MAX ; $ rowSum = array ( ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ mat [ $ i ] [ $ j ] ; $ rowSum [ $ i ] = $ sum ; } $ max_diff = $ rowSum [ 1 ] - $ rowSum [ 0 ] ; $ min_element = $ rowSum [ 0 ] ; for ( $ i = 1 ; $ i < $ m ; $ i ++ ) { if ( $ rowSum [ $ i ] - $ min_element > $ max_diff ) $ max_diff = $ rowSum [ $ i ] - $ min_element ; if ( $ rowSum [ $ i ] < $ min_element ) $ min_element = $ rowSum [ $ i ] ; } return $ max_diff ; } $ m = 5 ; $ n = 4 ; $ mat = array ( array ( -1 , 2 , 3 , 4 ) , array ( 5 , 3 , -2 , 1 ) , array ( 6 , 7 , 2 , -3 ) , array ( 2 , 9 , 1 , 4 ) , array ( 2 , 1 , -2 , 0 ) ) ; echo maxRowDiff ( $ mat , $ m , $ n ) ; ? >


"Maximum difference of zeros and ones in binary string | Set 2 ( O ( n ) time ) | Returns the length of substring with maximum difference of zeroes and ones in binary string ; traverse a binary string from left to right ; add current value to the current_sum according to the Character if it ' s ▁ ' 0 ' add 1 else -1 ; update maximum sum ; return - 1 if string does not contain any zero that means all ones otherwise max_sum ; Driver Code"
How can the above be solved in PHP?

 < ? php function findLength ( $ str , $ n ) { $ current_sum = 0 ; $ max_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ current_sum += ( $ str [ $ i ] == '0' ? 1 : -1 ) ; if ( $ current_sum < 0 ) $ current_sum = 0 ; $ max_sum = max ( $ current_sum , $ max_sum ) ; } return $ max_sum == 0 ? -1 : $ max_sum ; } $ s = "11000010001" ; $ n = 11 ; echo findLength ( $ s , $ n ) , "
 " ; ? >


"Maximum distinct lines passing through a single point | function to find maximum lines which passes through a single point ; Driver Code"
How can the above be solved in PHP?


"Maximum distinct lowercase alphabets between two uppercase | PHP Program to find maximum lowercase alphabets present between two uppercase alphabets ; Function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; Ignoring lowercase characters in the beginning . ; We start from next of first capital letter and traverse through remaining character . ; If character is in uppercase , ; Count all distinct lower case characters ; Update maximum count ; Reset count array ; If character is in lowercase ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX_CHAR = 26 ; function maxLower ( $ str ) { global $ MAX_CHAR ; $ n = strlen ( $ str ) ; $ i = 0 ; for ( ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) { $ i ++ ; break ; } } $ maxCount = 0 ; $ count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; for ( ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) { $ currCount = 0 ; for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) if ( $ count [ $ j ] > 0 ) $ currCount ++ ; $ maxCount = max ( $ maxCount , $ currCount ) ; $ count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; } if ( $ str [ $ i ] >= ' a ' && $ str [ $ i ] <= ' z ' ) $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; } return $ maxCount ; } $ str = " zACaAbbaazzC " ; echo maxLower ( $ str ) ; ? >


"Maximum elements that can be made equal with k updates | Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code"
How can the above be solved in PHP?

 < ? php function ElementsCalculationFunc ( $ pre , $ maxx , $ x , $ k , $ n ) { for ( $ i = 0 , $ j = $ x ; $ j <= $ n ; $ j ++ , $ i ++ ) { if ( $ x * $ maxx [ $ j ] - ( $ pre [ $ j ] - $ pre [ $ i ] ) <= $ k ) return true ; } return false ; } function MaxNumberOfElements ( $ a , $ n , $ k ) { sort ( $ a ) ; for ( $ i = 0 ; $ i <= $ n ; ++ $ i ) { $ pre [ $ i ] = 0 ; $ maxx [ $ i ] = 0 ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ a [ $ i - 1 ] ; $ maxx [ $ i ] = max ( $ maxx [ $ i - 1 ] , $ a [ $ i - 1 ] ) ; } $ l = 1 ; $ r = $ n ; $ ans ; while ( $ l < $ r ) { $ mid = ( $ l + $ r ) / 2 ; if ( ElementsCalculationFunc ( $ pre , $ maxx , $ mid - 1 , $ k , $ n ) ) { $ ans = $ mid ; $ l = $ mid + 1 ; } else $ r = $ mid - 1 ; } echo $ ans , "
 " ; } $ arr = array ( 2 , 4 , 9 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ k = 3 ; MaxNumberOfElements ( $ arr , $ n , $ k ) ; #This  code is contributed by akt_mit.
? >


"Maximum elements that can be made equal with k updates | PHP program to find maximum elements that can be made equal with k ; Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code"
How can the above be solved in PHP?

 < ? php function ElementsCalculationFunc ( $ pre , $ maxx , $ x , $ k , $ n ) { for ( $ i = 0 , $ j = $ x ; $ j <= $ n ; $ j ++ , $ i ++ ) { function ElementsCalculationFunc ( $ pre , $ maxx , $ x , $ k , $ n ) { for ( $ i = 0 , $ j = $ x ; $ j <= $ n ; $ j ++ , $ i ++ ) { if ( $ x * $ maxx [ $ j ] - ( $ pre [ $ j ] - $ pre [ $ i ] ) <= $ k ) return true ; } return false ; } function MaxNumberOfElements ( $ a , $ n , $ k ) { sort ( $ a ) ; for ( $ i = 0 ; $ i <= $ n ; ++ $ i ) { $ pre [ $ i ] = 0 ; $ maxx [ $ i ] = 0 ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ a [ $ i - 1 ] ; $ maxx [ $ i ] = max ( $ maxx [ $ i - 1 ] , $ a [ $ i - 1 ] ) ; } $ l = 1 ; $ r = $ n ; $ ans ; while ( $ l < $ r ) { $ mid = ( $ l + $ r ) / 2 ; if ( ElementsCalculationFunc ( $ pre , $ maxx , $ mid - 1 , $ k , $ n ) ) { $ ans = $ mid ; $ l = $ mid + 1 ; } else $ r = $ mid - 1 ; } echo $ ans , "
 " ; } $ arr = array ( 2 , 4 , 9 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ k = 3 ; MaxNumberOfElements ( $ arr , $ n , $ k ) ; #This  code is contributed by akt_mit.
? >


"Maximum elements which can be crossed using given units of a and b | Function to find the number of elements crossed ; Keep a copy of a ; Iterate in the binary array ; If no a and b left to use ; If there is no a ; use b and increase a by 1 if arr [ i ] is 1 ; simply use b ; Use a if theres no b ; Increase a and use b if arr [ i ] == 1 ; Use a ; Driver code"
How can the above be solved in PHP?


"Maximum games played by winner | Method returns maximum games a winner needs to play in N - player tournament ; for 0 games , 1 player is needed for 1 game , 2 players are required ; loop until i - th Fibonacci number is less than or equal to N ; result is ( i - 2 ) because i will be incremented one extra in while loop and we want the last value which is smaller than N , so one more decrement ; Driver Code"
How can the above be solved in PHP?

< ? php function maxGameByWinner ( $ N ) { $ dp [ $ N ] = 0 ; $ dp [ 0 ] = 1 ; $ dp [ 1 ] = 2 ; $ i = 2 ; do { $ dp [ $ i ] = $ dp [ $ i - 1 ] + $ dp [ $ i - 2 ] ; } while ( $ dp [ $ i ++ ] <= $ N ) ; return ( $ i - 2 ) ; } $ N = 10 ; echo maxGameByWinner ( $ N ) ; ? >


"Maximum given sized rectangles that can be cut out of a sheet of paper | Function to return the maximum rectangles possible ; Cut rectangles horizontally if possible ; One rectangle is a single cell ; Total rectangles = total cells ; Cut rectangles vertically if possible ; Return the maximum possible rectangles ; Driver code"
How can the above be solved in PHP?

< ? php function maxRectangles ( $ L , $ B , $ l , $ b ) { $ horizontal = 0 ; $ vertical = 0 ; if ( $ l <= $ L && $ b <= $ B ) { $ columns = ( int ) ( $ B / $ b ) ; $ rows = ( int ) ( $ L / $ l ) ; $ horizontal = $ rows * $ columns ; } if ( $ l <= $ B && $ b <= $ L ) { $ columns = ( int ) ( $ L / $ b ) ; $ rows = ( int ) ( $ B / $ l ) ; $ vertical = $ rows * $ columns ; } return max ( $ horizontal , $ vertical ) ; } $ L = 10 ; $ B = 7 ; $ l = 4 ; $ b = 3 ; print ( maxRectangles ( $ L , $ B , $ l , $ b ) ) ; ? >


"Maximum height when coins are arranged in a triangle | Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e decides the accuracy level ; Method to find maximum height of arrangement of coins ; calculating portion inside the square root ; Driver code to test above method"
How can the above be solved in PHP?

< ? php function squareRoot ( $ n ) { $ x = $ n ; $ y = 1 ; $ e = 0.000001 ; while ( $ x - $ y > $ e ) { $ x = ( $ x + $ y ) / 2 ; $ y = $ n / $ x ; } return $ x ; } function findMaximumHeight ( $ N ) { $ n = 1 + 8 * $ N ; $ maxH = ( -1 + squareRoot ( $ n ) ) / 2 ; return floor ( $ maxH ) ; } $ N = 12 ; echo findMaximumHeight ( $ N ) ; ? >


"Maximum in array which is at | Function to find the index of Max element that satisfies the condition ; Finding index of max of the array ; Returns - 1 if the max element is not twice of the i - th element . ; Driver Code"
How can the above be solved in PHP?

< ? php function findIndex ( $ arr , $ len ) { $ maxIndex = 0 ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) if ( $ arr [ $ i ] > $ arr [ $ maxIndex ] ) $ maxIndex = $ i ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) if ( $ maxIndex != $ i and $ arr [ $ maxIndex ] < 2 * $ arr [ $ i ] ) return -1 ; return $ maxIndex ; } $ arr = array ( 3 , 6 , 1 , 0 ) ; $ len = count ( $ arr ) ; echo findIndex ( $ arr , $ len ) ; ? >


"Maximum length of balanced string after swapping and removal of characters | PHP implementation of the approach Function to return the length of the longest balanced sub - string ; To store the count of parentheses ; Traversing the string ; Check type of parentheses and incrementing count for it ; Sum all pair of balanced parentheses ; Driven code"
How can the above be solved in PHP?

< ? php function maxBalancedStr ( $ s ) { $ open1 = 0 ; $ close1 = 0 ; $ open2 = 0 ; $ close2 = 0 ; $ open3 = 0 ; $ close3 = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { switch ( $ s [ $ i ] ) { case ' ( ' : $ open1 ++ ; break ; case ' ) ' : $ close1 ++ ; break ; case ' { ' : $ open2 ++ ; break ; case ' } ' : $ close2 ++ ; break ; case ' [ ' : $ open3 ++ ; break ; case ' ] ' : $ close3 ++ ; break ; } } $ maxLen = 2 * min ( $ open1 , $ close1 ) + 2 * min ( $ open2 , $ close2 ) + 2 * min ( $ open3 , $ close3 ) ; return $ maxLen ; } { $ s = " ) ) [ ] ] ( ( " ; echo ( maxBalancedStr ( $ s ) ) ; }


"Maximum length of segments of 0 ' s ▁ and ▁ 1' s | Recursive Function to find total length of the array where 1 is greater than zero ; If reaches till end ; If $dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for $start index ; Driver Code ; Size of string ; Calling the function to find the value of function"
How can the above be solved in PHP?

< ? php function find ( $ start , $ adj , $ n , $ dp ) { if ( $ start == $ n ) return 0 ; if ( $ dp [ $ start ] != -1 ) return $ dp [ $ start ] ; $ dp [ $ start ] = 0 ; $ one = 0 ; $ zero = 0 ; for ( $ k = $ start ; $ k < $ n ; $ k ++ ) { if ( $ adj [ $ k ] == '1' ) $ one ++ ; else $ zero ++ ; if ( $ one > $ zero ) $ dp [ $ start ] = max ( $ dp [ $ start ] , find ( $ k + 1 , $ adj , $ n , $ dp ) + $ k - $ start + 1 ) ; else $ dp [ $ start ] = max ( $ dp [ $ start ] , find ( $ k + 1 , $ adj , $ n , $ dp ) ) ; } return $ dp [ $ start ] ; } $ adj = "100110001010001" ; $ n = strlen ( $ adj ) ; $ dp = array_fill ( 0 , $ n + 1 , -1 ) ; echo find ( 0 , $ adj , $ n , $ dp ) ; ? >


"Maximum length of subarray such that sum of the subarray is even | Function to find length of the longest subarray such that sum of the subarray is even ; Check if sum of complete array is even ; if ( $sum % 2 == 0 ) total sum is already even ; Find an index i such the a [ i ] is odd and compare length of both halfs excluding a [ i ] to find max length subarray ; Driver Code"
How can the above be solved in PHP?


"Maximum length prefix of one string that occurs as subsequence in another | Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code"
How can the above be solved in PHP?

< ? php function maxPrefix ( $ s , $ t ) { $ count = 0 ; for ( $ i = 0 ; $ i < strlen ( $ t ) ; $ i ++ ) { if ( $ count == strlen ( $ s ) ) break ; if ( $ t [ $ i ] == $ s [ $ count ] ) $ count ++ ; } return $ count ; } { $ S = " digger " ; $ T = " biggerdiagram " ; echo maxPrefix ( $ S , $ T ) ; return 0 ; } ? >


"Maximum length subsequence with difference between adjacent elements as either 0 or 1 | function to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; Initialize mls [ ] values for all indexes ; Compute optimized maximum length subsequence values in bottom up manner ; Store maximum of all ' mls ' values in ' max ' ; required maximum length subsequence ; Driver Code"
How can the above be solved in PHP?

< ? php function maxLenSub ( $ arr , $ n ) { $ mls = array ( ) ; $ max = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mls [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) <= 1 and $ mls [ $ i ] < $ mls [ $ j ] + 1 ) $ mls [ $ i ] = $ mls [ $ j ] + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max < $ mls [ $ i ] ) $ max = $ mls [ $ i ] ; return $ max ; } $ arr = array ( 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ length ▁ subsequence ▁ = ▁ " , maxLenSub ( $ arr , $ n ) ; ? >


"Maximum length substring having all same characters after k changes | function to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; function which returns maximum length of substring ; Driver code"
How can the above be solved in PHP?

< ? php function findLen ( $ A , $ n , $ k , $ ch ) { $ maxlen = 1 ; $ cnt = 0 ; $ l = 0 ; $ r = 0 ; while ( $ r < $ n ) { if ( $ A [ $ r ] != $ ch ) ++ $ cnt ; while ( $ cnt > $ k ) { if ( $ A [ $ l ] != $ ch ) -- $ cnt ; ++ $ l ; } $ maxlen = max ( $ maxlen , $ r - $ l + 1 ) ; ++ $ r ; } return $ maxlen ; } function answer ( $ A , $ n , $ k ) { $ maxlen = 1 ; for ( $ i = 0 ; $ i < 26 ; ++ $ i ) { $ maxlen = max ( $ maxlen , findLen ( $ A , $ n , $ k , $ i + ' A ' ) ) ; $ maxlen = max ( $ maxlen , findLen ( $ A , $ n , $ k , $ i + ' a ' ) ) ; } return $ maxlen ; } $ n = 5 ; $ k = 2 ; $ A = " ABABA " ; echo " Maximum length = " ▁ . ▁ answer ( $ A , ▁ $ n , ▁ $ k ) ▁ . ▁ " " $ n = 6 ; $ k = 4 ; $ B = " HHHHHH " ; echo " Maximum length = " ▁ . ▁ answer ( $ B , ▁ $ n , ▁ $ k ) ▁ . ▁ " " ? >


"Maximum litres of water that can be bought with N Rupees | PHP implementation of the above approach ; if buying glass bottles is profitable ; Glass bottles that can be bought ; Change budget according the bought bottles ; Plastic bottles that can be bought ; if only plastic bottles need to be bought ; Driver Code"
How can the above be solved in PHP?

 < ? php function maxLitres ( $ budget , $ plastic , $ glass , $ refund ) { if ( $ glass - $ refund < $ plastic ) { $ ans = max ( ( int ) ( $ budget - $ refund ) / ( $ glass - $ refund ) , 0 ) ; $ budget -= $ ans * ( $ glass - $ refund ) ; $ ans += ( int ) ( $ budget / $ plastic ) ; echo $ ans . "
 " ; } else echo ( int ) ( $ budget / $ plastic ) . "
 " ; } $ budget = 10 ; $ plastic = 11 ; $ glass = 9 ; $ refund = 8 ; maxLitres ( $ budget , $ plastic , $ glass , $ refund ) ; ? >


"Maximum money that can be withdrawn in two steps | Function to return the maximum coins we can get ; Update elements such that X > Y ; Take from the maximum ; Refill ; Again , take the maximum ; Driver code"
How can the above be solved in PHP?

< ? php function maxCoins ( $ X , $ Y ) { if ( $ X < $ Y ) swap ( $ X , $ Y ) ; $ coins = $ X ; $ X -- ; $ coins += max ( $ X , $ Y ) ; return $ coins ; } $ X = 7 ; $ Y = 5 ; echo maxCoins ( $ X , $ Y ) ; ? >


"Maximum number by concatenating every element in a rotation of an array | Function to print the largest number ; store the index of largest left most digit of elements ; Iterate for all numbers ; check for the the last digit ; check for the largest left most digit ; print the rotation of array ; print the rotation of array ; Driver Code"
How can the above be solved in PHP?


"Maximum number of candies that can be bought | Function to return the maximum candies that can be bought ; Buy all the candies of the last type ; Starting from second last ; Amount of candies of the current type that can be bought ; Add candies of current type that can be bought ; Update the previous bought amount ; Driver code"
How can the above be solved in PHP?

< ? php function maxCandies ( $ arr , $ n ) { $ prevBought = $ arr [ $ n - 1 ] ; $ candies = $ prevBought ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ x = min ( $ prevBought - 1 , $ arr [ $ i ] ) ; if ( $ x >= 0 ) { $ candies += $ x ; $ prevBought = $ x ; } } return $ candies ; } $ arr = array ( 1 , 2 , 1 , 3 , 6 ) ; $ n = sizeof ( $ arr ) ; echo ( maxCandies ( $ arr , $ n ) ) ; ? >


"Maximum number of edges in Bipartite graph | Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code"
How can the above be solved in PHP?

< ? php function maxEdges ( $ N ) { $ edges = 0 ; $ edges = floor ( ( $ N * $ N ) / 4 ) ; return $ edges ; } $ N = 5 ; echo maxEdges ( $ N ) ; ? >


"Maximum number of ones in a N * N matrix with given constraints | Function that returns the maximum number of ones ; Minimum number of zeroes ; Totol cells = square of the size of the matrices ; Initialising the answer ; Initialising the variables"
How can the above be solved in PHP?

< ? php function getMaxOnes ( $ n , $ x ) { $ zeroes = ( int ) ( $ n / $ x ) ; $ zeroes = $ zeroes * $ zeroes ; $ total = $ n * $ n ; $ ans = $ total - $ zeroes ; return $ ans ; } $ n = 5 ; $ x = 2 ; echo getMaxOnes ( $ n , $ x ) ; ? >


"Maximum number of partitions that can be sorted individually to make sorted | Function to find maximum partitions . ; Find maximum in prefix arr [ 0. . i ] ; If maximum so far is equal to index , we can make a new partition ending at index i . ; Driver code"
How can the above be solved in PHP?

< ? php function maxPartitions ( $ arr , $ n ) { $ ans = 0 ; $ max_so_far = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ max_so_far = max ( $ max_so_far , $ arr [ $ i ] ) ; if ( $ max_so_far == $ i ) $ ans ++ ; } return $ ans ; } { $ arr = array ( 1 , 0 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo maxPartitions ( $ arr , $ n ) ; return 0 ; } ? >


"Maximum number of pieces in N cuts | Function for finding maximum pieces with n cuts . ; to maximize number of pieces x is the horizontal cuts ; Now ( x ) is the horizontal cuts and ( n - x ) is vertical cuts , then maximum number of pieces = ( x + 1 ) * ( n - x + 1 ) ; Taking the maximum number of cuts allowed as 3 ; Finding and printing the max number of pieces"
How can the above be solved in PHP?

< ? php function findMaximumPieces ( $ n ) { $ x = ( int ) ( $ n / 2 ) ; return ( ( $ x + 1 ) * ( $ n - $ x + 1 ) ) ; } $ n = 3 ; echo " Max ▁ number ▁ of ▁ pieces ▁ for ▁ n ▁ = ▁ " . $ n . " ▁ is ▁ " . findMaximumPieces ( 3 ) ; ? >


"Maximum number of removals of given subsequence from a string | Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code"
How can the above be solved in PHP?

< ? php function maxOperations ( $ str ) { $ i = $ g = $ gk = $ gks = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == ' g ' ) { $ g ++ ; } else if ( $ str [ $ i ] == ' k ' ) { if ( $ g > 0 ) { $ g -- ; $ gk ++ ; } } else if ( $ str [ $ i ] == ' s ' ) { if ( $ gk > 0 ) { $ gk -- ; $ gks ++ ; } } } return $ gks ; } $ a = " ggkssk " ; echo maxOperations ( $ a ) ; ? >


"Maximum number of segments of lengths a , b and c | function to find the maximum number of segments ; stores the maximum number of segments each index can have ; initialize with - 1 ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( $i + $a <= $n ) avoid buffer overflow ; if ( $i + $b <= $n ) avoid buffer overflow ; if ( $i + $c <= $n ) avoid buffer overflow ; Driver code"
How can the above be solved in PHP?


"Maximum number of segments that can contain the given points | Function to return the maximum number of segments ; Sort both the vectors ; Initially pointing to the first element of b [ ] ; Try to find a match in b [ ] ; The segment ends before b [ j ] ; The point lies within the segment ; The segment starts after b [ j ] ; Return the required count ; Driver code"
How can the above be solved in PHP?


"Maximum number of teams that can be formed with given persons | Function that returns true if it possible to form a team with the given n and m ; 1 person of Type1 and 2 persons of Type2 can be chosen ; 1 person of Type2 and 2 persons of Type1 can be chosen ; Cannot from a team ; Function to return the maximum number of teams that can be formed ; To store the required count of teams formed ; Choose 2 persons of Type1 ; And 1 person of Type2 ; Choose 2 persons of Type2 ; And 1 person of Type1 ; Another team has been formed ; Driver code"
How can the above be solved in PHP?

< ? php function canFormTeam ( $ n , $ m ) { if ( $ n >= 1 && $ m >= 2 ) return true ; if ( $ m >= 1 && $ n >= 2 ) return true ; return false ; } function maxTeams ( $ n , $ m ) { $ count = 0 ; while ( canFormTeam ( $ n , $ m ) ) { if ( $ n > $ m ) { $ n -= 2 ; $ m -= 1 ; } else { $ m -= 2 ; $ n -= 1 ; } $ count ++ ; } return $ count ; } $ n = 4 ; $ m = 5 ; echo maxTeams ( $ n , $ m ) ; ? >


"Maximum number of unique prime factors | Return maximum number of prime factors for any number in [ 1 , N ] ; Based on Sieve of Eratosthenes ; If p is prime ; We simply multiply first set of prime numbers while the product is smaller than N . ; Driver Code"
How can the above be solved in PHP?

 < ? php function maxPrimefactorNum ( $ N ) { if ( $ N < 2 ) return 0 ; $ arr = array_fill ( 0 , ( $ N + 1 ) , true ) ; $ prod = 1 ; $ res = 0 ; for ( $ p = 2 ; $ p * $ p <= $ N ; $ p ++ ) { if ( $ arr [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ N ; $ i += $ p ) $ arr [ $ i ] = false ; $ prod *= $ p ; if ( $ prod > $ N ) return $ res ; $ res ++ ; } } return $ res ; } $ N = 500 ; echo maxPrimefactorNum ( $ N ) . "
 " ; ? >


"Maximum number of unique prime factors | Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; Driver Code"
How can the above be solved in PHP?

< ? php function maxPrimefactorNum ( $ N ) { $ arr = array_fill ( 0 , $ N + 1 , 0 ) ; for ( $ i = 2 ; $ i * $ i <= $ N ; $ i ++ ) { if ( ! $ arr [ $ i ] ) for ( $ j = 2 * $ i ; $ j <= $ N ; $ j += $ i ) $ arr [ $ j ] ++ ; $ arr [ $ i ] = 1 ; } return max ( $ arr ) ; } $ N = 40 ; echo maxPrimefactorNum ( $ N ) ; ? >


"Maximum number that can be display on Seven Segment Display using N segments | Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver 's Code"
How can the above be solved in PHP?

< ? php function printMaxNumber ( $ n ) { if ( $ n & 1 ) { echo "7" ; for ( $ i = 0 ; $ i < ( $ n - 3 ) / 2 ; $ i ++ ) echo "1" ; } else { for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) echo "1" ; } } $ n = 5 ; printMaxNumber ( $ n ) ; ? >


"Maximum number with same digit factorial product | Function to return the required number ; Count the frequency of each digit ; 4 ! can be expressed as 2 ! * 2 ! * 3 ! ; 6 ! can be expressed as 5 ! * 3 ! ; 8 ! can be expressed as 7 ! * 2 ! * 2 ! * 2 ! ; 9 ! can be expressed as 7 ! * 3 ! * 3 ! * 2 ! ; To store the required number ; If number has only either 1 and 0 as its digits ; Generate the greatest number possible ; Driver code"
How can the above be solved in PHP?


"Maximum path sum for each position with jumps under divisibility condition | PHP program to print maximum path sum ending with each position x such that all path step positions divide x . ; Create an array such that dp [ i ] stores maximum path sum ending with i . ; Calculating maximum sum path for each element . ; Finding previous step for arr [ i ] Moving from 1 to sqrt ( i + 1 ) since all the divisors are present from sqrt ( i + 1 ) . ; Checking if j is divisor of i + 1. ; Checking which divisor will provide greater value . ; Printing the answer ( Maximum path sum ending with every position i + 1. ; Driven Program ; Function calling"
How can the above be solved in PHP?


"Maximum path sum in a triangle . | Function for finding maximum sum ; loop for bottom - up calculation ; for each element , check both elements just below the number and below right to the number add the maximum of them to it ; return the top element which stores the maximum sum ; Driver Code"
How can the above be solved in PHP?


"Maximum path sum in an Inverted triangle | SET 2 | PHP program implementation of Max sum problem in a triangle ; Function for finding maximum sum ; Loop for bottom - up calculation ; For each element , check both elements just below the number and below left to the number add the maximum of them to it ; Return the maximum sum ; Driver Code"
How can the above be solved in PHP?


"Maximum path sum that starting with any cell of 0 | PHP program to find Maximum path sum start any column in row '0' and ends up to any column in row ' n - 1' ; function find maximum sum path ; create 2D matrix to store the sum of the path ; copy all element of first column into ' dp ' first column ; Find maximum path sum that end ups at any column of last row ' N - 1' ; return maximum sum path ; Driver Code"
How can the above be solved in PHP?


"Maximum points of intersection n lines | nC2 = ( n ) * ( n - 1 ) / 2 ; ; n is number of line"
How can the above be solved in PHP?

 < ? php function countMaxIntersect ( $ n ) { return ( $ n ) * ( $ n - 1 ) / 2 ; } $ n = 8 ; echo countMaxIntersect ( $ n ) . "
 " ; ? >


"Maximum positive integer divisible by C and is in the range [ A , B ] | Function to return the required number ; If b % c = 0 then b is the required number ; Else get the maximum multiple of c smaller than b ; Driver code"
How can the above be solved in PHP?

< ? php function getMaxNum ( $ a , $ b , $ c ) { if ( $ b % $ c == 0 ) return $ b ; $ x = ( ( int ) ( $ b / $ c ) * $ c ) ; if ( $ x >= $ a && $ x <= $ b ) return $ x ; else return -1 ; } $ a = 2 ; $ b = 10 ; $ c = 3 ; echo ( getMaxNum ( $ a , $ b , $ c ) ) ; ? >


"Maximum possible difference of two subsets of an array | function for maximum subset diff ; if frequency of any element is two make both equal to zero ; Driver Code"
How can the above be solved in PHP?

< ? php function maxDiff ( $ arr , $ n ) { $ SubsetSum_1 = 0 ; $ SubsetSum_2 = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ isSingleOccurance = true ; for ( $ j = $ i + 1 ; $ j <= $ n - 1 ; $ j ++ ) { if ( $ arr [ $ i ] == $ arr [ $ j ] ) { $ isSingleOccurance = false ; $ arr [ $ i ] = $ arr [ $ j ] = 0 ; break ; } } if ( $ isSingleOccurance ) { if ( $ arr [ $ i ] > 0 ) $ SubsetSum_1 += $ arr [ $ i ] ; else $ SubsetSum_2 += $ arr [ $ i ] ; } } return abs ( $ SubsetSum_1 - $ SubsetSum_2 ) ; } $ arr = array ( 4 , 2 , -3 , 3 , -2 , -2 , 8 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ Difference ▁ = ▁ " , maxDiff ( $ arr , $ n ) ; ? >


"Maximum possible difference of two subsets of an array | function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; Driver Code"
How can the above be solved in PHP?

< ? php function maxDiff ( $ arr , $ n ) { $ result = 0 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] != $ arr [ $ i + 1 ] ) $ result += abs ( $ arr [ $ i ] ) ; else $ i ++ ; } if ( $ arr [ $ n - 2 ] != $ arr [ $ n - 1 ] ) $ result += abs ( $ arr [ $ n - 1 ] ) ; return $ result ; } $ arr = array ( 4 , 2 , -3 , 3 , -2 , -2 , 8 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ Difference ▁ = ▁ " , maxDiff ( $ arr , $ n ) ; ? >


"Maximum possible intersection by moving centers of line segments | Function to print the maximum intersection ; Case 1 ; Case 2 ; Case 3 ; Driver Code"
How can the above be solved in PHP?

< ? php function max_intersection ( $ center , $ length , $ k ) { sort ( $ center ) ; if ( $ center [ 2 ] - $ center [ 0 ] >= 2 * $ k + $ length ) { return 0 ; } else if ( $ center [ 2 ] - $ center [ 0 ] >= 2 * $ k ) { return ( 2 * $ k - ( $ center [ 2 ] - $ center [ 0 ] - $ length ) ) ; } else return $ length ; } $ center = array ( 1 , 2 , 3 ) ; $ L = 1 ; $ K = 1 ; echo max_intersection ( $ center , $ L , $ K ) ; ? >


"Maximum power of jump required to reach the end of string | Function to calculate the maximum power of the jump ; Initialize the count with 1 ; Find the character at last index ; Start traversing the string ; Check if the current char is equal to the last character ; max_so_far stores maximum value of the power of the jump from starting to ith position ; Reset the count to 1 ; Else , increment the number of jumps / count ; Return the maximum number of jumps ; Driver code"
How can the above be solved in PHP?

< ? php function powerOfJump ( $ s ) { $ count = 1 ; $ max_so_far = PHP_INT_MIN ; $ ch = $ s [ strlen ( $ s ) - 1 ] ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == $ ch ) { if ( $ count > $ max_so_far ) { $ max_so_far = $ count ; } $ count = 1 ; } else $ count ++ ; } return $ max_so_far ; } $ st = "1010101" ; echo powerOfJump ( $ st ) ; ? >


"Maximum product of 4 adjacent elements in matrix | PHP program to find out the maximum product in the matrix which four elements are adjacent to each other in one direction ; function to find max product ; iterate the rows . ; iterate the columns . ; check the maximum product in horizontal row . ; check the maximum product in vertical row . ; check the maximum product in diagonal going through down - right ; check the maximum product in diagonal going through up - right ; Driver Code"
How can the above be solved in PHP?


"Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Construct four auxiliary vectors of size n and initialize them by - 1 ; will contain max product ; to store maximum element on left of array ; to store minimum element on left of array ; leftMax [ i ] will contain max element on left of arr [ i ] excluding arr [ i ] . leftMin [ i ] will contain min element on left of arr [ i ] excluding arr [ i ] . ; reset max_sum to store maximum element on right of array ; reset min_sum to store minimum element on right of array ; rightMax [ i ] will contain max element on right of arr [ i ] excluding arr [ i ] . rightMin [ i ] will contain min element on right of arr [ i ] excluding arr [ i ] . ; For all array indexes i except first and last , compute maximum of arr [ i ] * x * y where x can be leftMax [ i ] or leftMin [ i ] and y can be rightMax [ i ] or rightMin [ i ] . ; Driver program to test above functions"
How can the above be solved in PHP?

< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 3 ) return -1 ; $ leftMin = array_fill ( 0 , $ n , -1 ) ; $ rightMin = array_fill ( 0 , $ n , -1 ) ; $ leftMax = array_fill ( 0 , $ n , -1 ) ; $ rightMax = array_fill ( 0 , $ n , -1 ) ; $ max_product = PHP_INT_MIN ; $ max_sum = $ arr [ 0 ] ; $ min_sum = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ leftMax [ $ i ] = $ max_sum ; if ( $ arr [ $ i ] > $ max_sum ) $ max_sum = $ arr [ $ i ] ; $ leftMin [ $ i ] = $ min_sum ; if ( $ arr [ $ i ] < $ min_sum ) $ min_sum = $ arr [ $ i ] ; } $ max_sum = $ arr [ $ n - 1 ] ; $ min_sum = $ arr [ $ n - 1 ] ; for ( $ j = $ n - 2 ; $ j >= 0 ; $ j -- ) { $ rightMax [ $ j ] = $ max_sum ; if ( $ arr [ $ j ] > $ max_sum ) $ max_sum = $ arr [ $ j ] ; $ rightMin [ $ j ] = $ min_sum ; if ( $ arr [ $ j ] < $ min_sum ) $ min_sum = $ arr [ $ j ] ; } for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { $ max1 = max ( $ arr [ $ i ] * $ leftMax [ $ i ] * $ rightMax [ $ i ] , $ arr [ $ i ] * $ leftMin [ $ i ] * $ rightMin [ $ i ] ) ; $ max2 = max ( $ arr [ $ i ] * $ leftMax [ $ i ] * $ rightMin [ $ i ] , $ arr [ $ i ] * $ leftMin [ $ i ] * $ rightMax [ $ i ] ) ; $ max_product = max ( $ max_product , max ( $ max1 , $ max2 ) ) ; } return $ max_product ; } $ arr = array ( 1 , 4 , 3 , -6 , -7 , 0 ) ; $ n = count ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Triplet ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " . $ max ; ? >


"Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Sort the array in ascending order ; Return the maximum of product of last three elements and product of first two elements and last element ; Driver code"
How can the above be solved in PHP?

< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 3 ) { return -1 ; } sort ( $ arr ) ; return max ( $ arr [ 0 ] * $ arr [ 1 ] * $ arr [ $ n - 1 ] , $ arr [ $ n - 1 ] * $ arr [ $ n - 2 ] * $ arr [ $ n - 3 ] ) ; } $ arr = array ( -10 , -3 , 5 , 6 , -20 ) ; $ n = sizeof ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) { echo ( " No ▁ Triplet ▁ Exists " ) ; } else { echo ( " Maximum ▁ product ▁ is ▁ " . $ max ) ; }


"Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; will contain max product ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProduct ( $ arr , $ n ) { $ INT_MIN = 0 ; if ( $ n < 3 ) return -1 ; $ max_product = $ INT_MIN ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n - 1 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) $ max_product = max ( $ max_product , $ arr [ $ i ] * $ arr [ $ j ] * $ arr [ $ k ] ) ; return $ max_product ; } $ arr = array ( 10 , 3 , 5 , 6 , 20 ) ; $ n = sizeof ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Triplet ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " , $ max ; ? >


"Maximum product of an increasing subsequence | Returns product of maximum product increasing subsequence . ; Initialize MPIS values ; Compute optimized MPIS values considering every element as ending element of sequence ; Pick maximum of all product values ; Driver program to test above function"
How can the above be solved in PHP?

< ? php function lis ( & $ arr , $ n ) { $ mpis = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mpis [ $ i ] = $ arr [ $ i ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ mpis [ $ i ] < ( $ mpis [ $ j ] * $ arr [ $ i ] ) ) $ mpis [ $ i ] = $ mpis [ $ j ] * $ arr [ $ i ] ; return max ( $ mpis ) ; } $ arr = array ( 3 , 100 , 4 , 5 , 150 , 6 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo lis ( $ arr , $ n ) ; return 0 ; ? >


"Maximum product of subsequence of size k | Required function ; sorting given input array ; variable to store final product of all element of sub - sequence of size k ; CASE I If max element is 0 and k is odd then max product will be 0 ; CASE II If all elements are negative and k is odd then max product will be product of rightmost - subarray of size k ; else i is current left pointer index ; j is current right pointer index ; CASE III if k is odd and rightmost element in sorted array is positive then it must come in subsequence Multiplying A [ j ] with product and correspondingly changing j ; CASE IV Now k is even Now we deal with pairs Each time a pair is multiplied to product ie . . two elements are added to subsequence each time Effectively k becomes half Hence , k >>= 1 means k /= 2 ; Now finding k corresponding pairs to get maximum possible value of product ; product from left pointers ; product from right pointers ; Taking the max product from two choices Correspondingly changing the pointer 's position ; Finally return product ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProductSubarrayOfSizeK ( $ A , $ n , $ k ) { sort ( $ A ) ; $ product = 1 ; if ( $ A [ $ n - 1 ] == 0 && ( $ k & 1 ) ) return 0 ; if ( $ A [ $ n - 1 ] <= 0 && ( $ k & 1 ) ) { for ( $ i = $ n - 1 ; $ i >= $ n - $ k ; $ i -- ) $ product *= $ A [ $ i ] ; return $ product ; } $ i = 0 ; $ j = $ n - 1 ; if ( $ k & 1 ) { $ product *= $ A [ $ j ] ; $ j -- ; $ k -- ; } $ k >>= 1 ; for ( $ itr = 0 ; $ itr < $ k ; $ itr ++ ) { $ left_product = $ A [ $ i ] * $ A [ $ i + 1 ] ; $ right_product = $ A [ $ j ] * $ A [ $ j - 1 ] ; if ( $ left_product > $ right_product ) { $ product *= $ left_product ; $ i += 2 ; } else { $ product *= $ right_product ; $ j -= 2 ; } } return $ product ; } $ A = array ( 1 , 2 , -1 , -3 , -6 , 4 ) ; $ n = count ( $ A ) ; $ k = 4 ; echo maxProductSubarrayOfSizeK ( $ A , $ n , $ k ) ; ? >


"Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Initialize Maximum , second maximum , third maximum and fourth maximum element ; Initialize Minimum , second minimum , third minimum and fourth minimum element ; Update Maximum , second maximum , third maximum and fourth maximum element ; Update second maximum , third maximum and fourth maximum element ; Update third maximum and fourth maximum element ; Update fourth maximum element ; Update Minimum , second minimum , third minimum and fourth minimum element ; Update second minimum , third minimum and fourth minimum element ; Update third minimum and fourth minimum element ; Update fourth minimum element ; Return the maximum of x , y and z ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 4 ) return -1 ; $ maxA = -2147483648 ; $ maxB = -2147483648 ; $ maxC = -2147483648 ; $ maxD = -2147483648 ; $ minA = 2147483647 ; $ minB = 2147483647 ; $ minC = 2147483647 ; $ minD = 2147483647 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ maxA ) { $ maxD = $ maxC ; $ maxC = $ maxB ; $ maxB = $ maxA ; $ maxA = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] > $ maxB ) { $ maxD = $ maxC ; $ maxC = $ maxB ; $ maxB = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] > $ maxC ) { $ maxD = $ maxC ; $ maxC = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] > $ maxD ) $ maxD = $ arr [ $ i ] ; if ( $ arr [ $ i ] < $ minA ) { $ minD = $ minC ; $ minC = $ minB ; $ minB = $ minA ; $ minA = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] < $ minB ) { $ minD = $ minC ; $ minC = $ minB ; $ minB = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] < $ minC ) { $ minD = $ minC ; $ minC = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] < $ minD ) $ minD = $ arr [ $ i ] ; } $ x = $ maxA * $ maxB * $ maxC * $ maxD ; $ y = $ minA * $ minB * $ minC * $ minD ; $ z = $ minA * $ minB * $ maxA * $ maxB ; return max ( $ x , max ( $ y , $ z ) ) ; } $ arr = array ( 1 , -4 , 3 , -6 , 7 , 0 ) ; $ n = count ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Quadruple ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " . $ max ; ? >


"Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Sort the array in ascending order ; Return the maximum of x , y and z ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 4 ) return -1 ; sort ( $ arr ) ; $ x = $ arr [ $ n - 1 ] * $ arr [ $ n - 2 ] * $ arr [ $ n - 3 ] * $ arr [ $ n - 4 ] ; $ y = $ arr [ 0 ] * $ arr [ 1 ] * $ arr [ 2 ] * $ arr [ 3 ] ; $ z = $ arr [ 0 ] * $ arr [ 1 ] * $ arr [ $ n - 1 ] * $ arr [ $ n - 2 ] ; return max ( $ x , max ( $ y , $ z ) ) ; } $ arr = array ( -10 , -3 , 5 , 6 , -20 ) ; $ n = sizeof ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Quadruple ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " . $ max ;


"Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; will contain max product ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 4 ) return -1 ; $ max_product = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n - 3 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n - 2 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n - 1 ; $ k ++ ) for ( $ l = $ k + 1 ; $ l < $ n ; $ l ++ ) $ max_product = max ( $ max_product , $ arr [ $ i ] * $ arr [ $ j ] * $ arr [ $ k ] * $ arr [ $ l ] ) ; return $ max_product ; } $ arr = array ( 10 , 3 , 5 , 6 , 20 ) ; $ n = count ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Quadruple ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " , $ max ; ? >


"Maximum product subset of an array | PHP program to find maximum product of a subset . ; Find count of negative numbers , count of zeros , negative number with least absolute value and product of non - zero numbers ; If number is 0 , we don 't  multiply it with product. ; Count negatives and keep track of negative number with least absolute value . ; If there are all zeros ; If there are odd number of negative numbers ; Exceptional case : There is only negative and all other are zeros ; Otherwise result is product of all non - zeros divided by negative number with least absolute value . ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProductSubset ( $ a , $ n ) { if ( $ n == 1 ) return $ a [ 0 ] ; $ max_neg = PHP_INT_MIN ; $ count_neg = 0 ; $ count_zero = 0 ; $ prod = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 0 ) { $ count_zero ++ ; continue ; } if ( $ a [ $ i ] < 0 ) { $ count_neg ++ ; $ max_neg = max ( $ max_neg , $ a [ $ i ] ) ; } $ prod = $ prod * $ a [ $ i ] ; } if ( $ count_zero == $ n ) return 0 ; if ( $ count_neg & 1 ) { if ( $ count_neg == 1 && $ count_zero > 0 && $ count_zero + $ count_neg == $ n ) return 0 ; $ prod = $ prod / $ max_neg ; } return $ prod ; } $ a = array ( -1 , -1 , -2 , 4 , 3 ) ; $ n = sizeof ( $ a ) ; echo maxProductSubset ( $ a , $ n ) ; ? >


"Maximum profit after buying and selling stocks with transaction fees | PHP implementation of above approach ; b [ 0 ] will contain the maximum profit ; b [ 1 ] will contain the day on which we are getting the maximum profit ; here finding the max profit ; if we get less then or equal to zero it means we are not getting the profit ; check if sum is greater then maximum then store the new maximum ; Driver code"
How can the above be solved in PHP?


"Maximum profit by buying and selling a share at most k times | Function to find out maximum profit by buying & selling a share atmost k times given stock price of n days ; table to store results of subproblems profit [ t ] [ i ] stores maximum profit using atmost t transactions up to day i ( including day i ) ; For day 0 , you can 't  earn money irrespective  of how many times you trade ; profit is 0 if we don 't  do any transaction  (i.e. k =0) ; fill the table in bottom - up fashion ; Driver Code"
How can the above be solved in PHP?

< ? php function maxProfit ( $ price , $ n , $ k ) { $ profit [ $ k + 1 ] [ $ n + 1 ] = 0 ; for ( $ i = 0 ; $ i <= $ k ; $ i ++ ) $ profit [ $ i ] [ 0 ] = 0 ; for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ profit [ 0 ] [ $ j ] = 0 ; $ prevDiff = NULL ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ prevDiff = max ( $ prevDiff , $ profit [ $ i - 1 ] [ $ j - 1 ] - $ price [ $ j - 1 ] ) ; $ profit [ $ i ] [ $ j ] = max ( $ profit [ $ i ] [ $ j - 1 ] , $ price [ $ j ] + $ prevDiff ) ; } } return $ profit [ $ k ] [ $ n - 1 ] ; } $ k = 3 ; $ price = array ( 12 , 14 , 17 , 10 , 14 , 13 , 12 , 15 ) ; $ n = sizeof ( $ price ) ; echo " Maximum ▁ profit ▁ is : ▁ " , maxProfit ( $ price , $ n , $ k ) ; ? >


"Maximum set bit sum in array without considering adjacent elements | Function to count total number of set bits in an integer ; Maximum sum of set bits ; Calculate total number of set bits for every element of the array ; find total set bits for each number and store back into the array ; current max excluding i ; current max including i ; return max of incl and excl ; Driver code"
How can the above be solved in PHP?

 < ? php function bit ( $ n ) { $ count = 0 ; while ( $ n ) { $ count ++ ; $ n = $ n & ( $ n - 1 ) ; } return $ count ; } function maxSumOfBits ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ i ] = bit ( $ arr [ $ i ] ) ; } $ incl = $ arr [ 0 ] ; $ excl = 0 ; $ excl_new ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ excl_new = ( $ incl > $ excl ) ? $ incl : $ excl ; $ incl = $ excl + $ arr [ $ i ] ; $ excl = $ excl_new ; } return ( ( $ incl > $ excl ) ? $ incl : $ excl ) ; } $ arr = array ( 1 , 2 , 4 , 5 , 6 , 7 , 20 , 25 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo maxSumOfBits ( $ arr , $ n ) ; #This  Code is Contributed by ajit
? >


"Maximum size of sub | Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Print the required answer"
How can the above be solved in PHP?

< ? php function cmp ( $ a , $ b ) { return ( $ a > $ b ) - ( $ a < $ b ) ; } function maxSubarraySize ( $ arr ) { $ N = sizeof ( $ arr ) ; $ ans = 1 ; $ anchor = 0 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ c = cmp ( $ arr [ $ i - 1 ] , $ arr [ $ i ] ) ; if ( $ c == 0 ) $ anchor = $ i ; else if ( $ i == $ N - 1 or $ c * cmp ( $ arr [ $ i ] , $ arr [ $ i + 1 ] ) != -1 ) { $ ans = max ( $ ans , $ i - $ anchor + 1 ) ; $ anchor = $ i ; } } return $ ans ; } $ arr = array ( 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ) ; echo maxSubarraySize ( $ arr ) ; ? >


"Maximum steps to transform 0 to X with bitwise AND | Function to get no of set bits in binary representation of positive integer n ; Driver code"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count += $ n & 1 ; $ n >>= 1 ; } return $ count ; } $ i = 3 ; echo ( countSetBits ( $ i ) ) ; ? >


"Maximum students to pass after giving bonus to everybody and not exceeding 100 marks | Function to return the number of students that can pass ; maximum marks ; maximum bonus marks that can be given ; counting the number of students that can pass ; Driver code"
How can the above be solved in PHP?

< ? php function check ( $ n , $ marks ) { $ x = max ( $ marks ) ; $ bonus = 100 - $ x ; $ c = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ marks [ $ i ] + $ bonus >= 50 ) $ c += 1 ; } return $ c ; } $ n = 5 ; $ marks = array ( 0 , 21 , 83 , 45 , 64 ) ; echo check ( $ n , $ marks ) ;


"Maximum subarray sum in O ( n ) using prefix sum | Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array , keep track of minimum prefix sum so far and maximum subarray sum . ; Test case 1 ; Test case 2"
How can the above be solved in PHP?

< ? php function maximumSumSubarray ( $ arr , $ n ) { $ min_prefix_sum = 0 ; $ res = PHP_INT_MIN ; $ prefix_sum = array ( ) ; $ prefix_sum [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ prefix_sum [ $ i ] = $ prefix_sum [ $ i - 1 ] + $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ res = max ( $ res , $ prefix_sum [ $ i ] - $ min_prefix_sum ) ; $ min_prefix_sum = min ( $ min_prefix_sum , $ prefix_sum [ $ i ] ) ; } return $ res ; } $ arr1 = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n1 = count ( $ arr1 ) ; echo maximumSumSubarray ( $ arr1 , $ n1 ) , " " ; $ arr2 = array ( 4 , -8 , 9 , -4 , 1 , -8 , -1 , 6 ) ; $ n2 = count ( $ arr2 ) ; echo maximumSumSubarray ( $ arr2 , $ n2 ) ; ? >


"Maximum subsequence sum such that no three are consecutive | PHP program to find the maximum sum such that no three are consecutive using recursion . ; Returns maximum subsequence sum such that no three elements are consecutive ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases ; Driver code"
How can the above be solved in PHP?

< ? php $ arr = array ( 100 , 1000 , 100 , 1000 , 1 ) ; $ sum = array_fill ( 0 , count ( $ arr ) + 1 , -1 ) ; function maxSumWO3Consec ( $ n ) { global $ sum , $ arr ; if ( $ sum [ $ n ] != -1 ) return $ sum [ $ n ] ; if ( $ n == 0 ) return $ sum [ $ n ] = 0 ; if ( $ n == 1 ) return $ sum [ $ n ] = $ arr [ 0 ] ; if ( $ n == 2 ) return $ sum [ $ n ] = $ arr [ 1 ] + $ arr [ 0 ] ; return $ sum [ $ n ] = max ( max ( maxSumWO3Consec ( $ n - 1 ) , maxSumWO3Consec ( $ n - 2 ) + $ arr [ $ n ] ) , $ arr [ $ n ] + $ arr [ $ n - 1 ] + maxSumWO3Consec ( $ n - 3 ) ) ; } $ n = count ( $ arr ) ; echo maxSumWO3Consec ( $ n ) ; ? >


"Maximum subsequence sum such that no three are consecutive | Returns maximum subsequence sum such that no three elements are consecutive ; Stores result for subarray arr [ 0. . i ] , i . e . , maximum possible sum in subarray arr [ 0. . i ] such that no three elements are consecutive$ . ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases 1 ) Exclude arr [ i ] , i . e . , sum [ i ] = sum [ i - 1 ] 2 ) Exclude arr [ i - 1 ] , i . e . , sum [ i ] = sum [ i - 2 ] + arr [ i ] 3 ) Exclude arr [ i - 2 ] , i . e . , sum [ i - 3 ] + arr [ i ] + arr [ i - 1 ] ; Driver code"
How can the above be solved in PHP?


"Maximum subset with bitwise OR equal to k | function to find the maximum subset with bitwise OR equal to k ; If the bitwise OR of k and element is equal to k , then include that element in the subset ; Store the bitwise OR of elements in v ; If ans is not equal to k , subset doesn 't exist ; Driver Code"
How can the above be solved in PHP?

 < ? php function subsetBitwiseORk ( $ arr , $ n , $ k ) { $ v = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ( $ arr [ $ i ] $ k ) == $ k ) array_push ( $ v , $ arr [ $ i ] ) ; } $ ans = 0 ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) $ ans |= $ v [ $ i ] ; if ( $ ans != $ k ) { echo ( " Subset ▁ does ▁ not ▁ exist
 " ) ; return ; } for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) echo ( $ v [ $ i ] . " ▁ " ) ; } $ k = 3 ; $ arr = array ( 1 , 4 , 2 ) ; $ n = count ( $ arr ) ; subsetBitwiseORk ( $ arr , $ n , $ k ) ; ? >


"Maximum sum after repeatedly dividing N by a divisor | Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code"
How can the above be solved in PHP?

 < ? php function smallestDivisor ( $ n ) { $ mx = sqrt ( $ n ) ; for ( $ i = 2 ; $ i <= $ mx ; $ i ++ ) if ( $ n % $ i == 0 ) return $ i ; return $ n ; } function maxSum ( $ n ) { $ res = $ n ; while ( $ n > 1 ) { $ divi = smallestDivisor ( $ n ) ; $ n /= $ divi ; $ res += $ n ; } return $ res ; } $ n = 34 ; echo maxSum ( $ n ) ; #This  code is contributed by akt_mit.
? >


"Maximum sum alternating subsequence | Return sum of maximum sum alternating sequence starting with arr [ 0 ] and is first decreasing . ; handling the edge case ; stores sum of decreasing and increasing sub - sequence ; store sum of increasing and decreasing sun - sequence ; As per question , first element must be part of solution . ; Traverse remaining elements of array ; IF current sub - sequence is decreasing the update dec [ j ] if needed . dec [ i ] by current inc [ j ] + arr [ i ] ; Revert the flag , if first decreasing is found ; If next element is greater but flag should be 1 i . e . this element should be counted after the first decreasing element gets counted ; If current sub - sequence is increasing then update inc [ i ] ; find maximum sum in b / w inc [ ] and dec [ ] ; return maximum sum alternate sun - sequence ; Driver Code"
How can the above be solved in PHP?

< ? php function maxAlternateSum ( $ arr , $ n ) { if ( $ n == 1 ) return $ arr [ 0 ] ; $ min = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ min = max ( $ min , $ arr [ $ i ] ) ; } if ( $ arr [ 0 ] == $ min ) return $ arr [ 0 ] ; $ dec = array_fill ( 0 , $ n , 0 ) ; $ inc = array_fill ( 0 , $ n , 0 ) ; $ dec [ 0 ] = $ inc [ 0 ] = $ arr [ 0 ] ; $ flag = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( $ arr [ $ j ] > $ arr [ $ i ] ) { $ dec [ $ i ] = max ( $ dec [ $ i ] , $ inc [ $ j ] + $ arr [ $ i ] ) ; $ flag = 1 ; } else if ( $ arr [ $ j ] < $ arr [ $ i ] && $ flag == 1 ) $ inc [ $ i ] = max ( $ inc [ $ i ] , $ dec [ $ j ] + $ arr [ $ i ] ) ; } } $ result = - ( PHP_INT_MAX - 1 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ result < $ inc [ $ i ] ) $ result = $ inc [ $ i ] ; if ( $ result < $ dec [ $ i ] ) $ result = $ dec [ $ i ] ; } return $ result ; } $ arr = array ( 8 , 2 , 3 , 5 , 7 , 9 , 10 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ sum ▁ = ▁ " , maxAlternateSum ( $ arr , $ n ) ; ? >


"Maximum sum and product of the M consecutive digits in a number | Function to find the maximum product ; Driver code"
How can the above be solved in PHP?

< ? php function maxProductSum ( $ str , $ m ) { $ n = strlen ( $ str ) ; $ maxProd = PHP_INT_MIN ; $ maxSum = PHP_INT_MIN ; for ( $ i = 0 ; $ i <= ( $ n - $ m ) ; $ i ++ ) { $ product = 1 ; $ sum = 0 ; for ( $ j = $ i ; $ j < ( $ m + $ i ) ; $ j ++ ) { $ product = $ product * ( $ str [ $ j ] - '0' ) ; $ sum = $ sum + ( $ str [ $ j ] - '0' ) ; } $ maxProd = max ( $ maxProd , $ product ) ; $ maxSum = max ( $ maxSum , $ sum ) ; } echo " Maximum ▁ Product ▁ = ▁ " , $ maxProd ; echo " Maximum Sum = " } $ str = "3605356297" ; $ m = 3 ; maxProductSum ( $ str , $ m ) ; ? >


"Maximum sum bitonic subarray | function to find the maximum sum bitonic subarray ; ' msis [ ] ' to store the maximum sum increasing subarray up to each index of ' arr ' from the beginning ' msds [ ] ' to store the maximum sum decreasing subarray from each index of ' arr ' up to the end ; to store the maximum sum bitonic subarray ; building up the maximum sum increasing subarray for each array index ; building up the maximum sum decreasing subarray for each array index ; for each array index , calculating the maximum sum of bitonic subarray of which it is a part of ; if true , then update ' max ' bitonic subarray sum ; required maximum sum ; Driver Code"
How can the above be solved in PHP?

< ? php function maxSumBitonicSubArr ( $ arr , $ n ) { $ msis = array ( ) ; $ msds = array ( ) ; $ max_sum = PHP_INT_MIN ; $ msis [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] ) $ msis [ $ i ] = $ msis [ $ i - 1 ] + $ arr [ $ i ] ; else $ msis [ $ i ] = $ arr [ $ i ] ; $ msds [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) if ( $ arr [ $ i ] > $ arr [ $ i + 1 ] ) $ msds [ $ i ] = $ msds [ $ i + 1 ] + $ arr [ $ i ] ; else $ msds [ $ i ] = $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max_sum < ( $ msis [ $ i ] + $ msds [ $ i ] - $ arr [ $ i ] ) ) $ max_sum = $ msis [ $ i ] + $ msds [ $ i ] - $ arr [ $ i ] ; return $ max_sum ; } $ arr = array ( 5 , 3 , 9 , 2 , 7 , 6 , 4 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ Sum ▁ = ▁ " , maxSumBitonicSubArr ( $ arr , $ n ) ; ? >


"Maximum sum by adding numbers with same number of set bits | count the number of bits for each element of array ; Count the number of set bits ; Function to return the the maximum sum ; Calculate the ; Assuming the number to be a maximum of 32 bits ; Add the number to the number of set bits ; Find the maximum sum ; Driver code"
How can the above be solved in PHP?

< ? php function bit_count ( $ n ) { $ count = 0 ; while ( $ n ) { $ count ++ ; $ n = $ n & ( $ n - 1 ) ; } return $ count ; } function maxsum ( $ arr , $ n ) { $ bits = array ( $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ bits [ $ i ] = bit_count ( $ arr [ $ i ] ) ; } $ sum = array_fill ( 0 , 32 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum [ $ bits [ $ i ] ] += $ arr [ $ i ] ; } $ maximum = 0 ; for ( $ i = 0 ; $ i < 32 ; $ i ++ ) { $ maximum = max ( $ sum [ $ i ] , $ maximum ) ; } return $ maximum ; } $ arr = array ( 2 , 3 , 8 , 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) ; echo maxsum ( $ arr , $ n ) ; ? >


"Maximum sum in a 2 x n grid such that no two elements are adjacent | Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't  to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver code"
How can the above be solved in PHP?

< ? php function maxSum ( $ grid , $ n ) { $ incl = max ( $ grid [ 0 ] [ 0 ] , $ grid [ 1 ] [ 0 ] ) ; $ excl = 0 ; $ excl_new ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ excl_new = max ( $ excl , $ incl ) ; $ incl = $ excl + max ( $ grid [ 0 ] [ $ i ] , $ grid [ 1 ] [ $ i ] ) ; $ excl = $ excl_new ; } return max ( $ excl , $ incl ) ; } $ grid = array ( array ( 1 , 2 , 3 , 4 , 5 ) , array ( 6 , 7 , 8 , 9 , 10 ) ) ; $ n = 5 ; echo maxSum ( $ grid , $ n ) ; ? >


"Maximum sum increasing subsequence from a prefix and a given element after prefix is must | PHP program to find maximum sum increasing subsequence till i - th index and including k - th index . ; Initializing the first row of the dp [ ] [ ] . ; Creating the dp [ ] [ ] matrix . ; To calculate for i = 4 and k = 6. ; Driver Code"
How can the above be solved in PHP?


"Maximum sum of a path in a Right Number Triangle | function to find maximum sum path ; Adding the element of row 1 to both the elements of row 2 to reduce a step from the loop ; Traverse remaining rows ; Loop to traverse columns ; tri [ i ] would store the possible combinations of sum of the paths ; array at n - 1 index ( tri [ i ] ) stores all possible adding combination , finding the maximum one out of them ; Driver Code"
How can the above be solved in PHP?


"Maximum sum of absolute difference of any permutation | PHP implementation of above algorithm ; final sequence stored in the vector ; sort the original array so that we can retrieve the large elements from the end of array elements ; In this loop first we will insert one smallest element not entered till that time in final sequence and then enter a highest element ( not entered till that time ) in final sequence so that we have large difference value . This process is repeated till all array has completely entered in sequence . Here , we have loop till n / 2 because we are inserting two elements at a time in loop . ; If there are odd elements , push the middle element at the end . ; variable to store the maximum sum of absolute difference ; In this loop absolute difference of elements for the final sequence is calculated . ; absolute difference of last element and 1 st element ; return the value ; Driver Code"
How can the above be solved in PHP?

 < ? php function MaxSumDifference ( & $ a , $ n ) { $ finalSequence = array ( ) ; sort ( $ a ) ; for ( $ i = 0 ; $ i < $ n / 2 ; ++ $ i ) { array_push ( $ finalSequence , $ a [ $ i ] ) ; array_push ( $ finalSequence , $ a [ $ n - $ i - 1 ] ) ; } if ( $ n % 2 != 0 ) array_push ( $ finalSequence , $ a [ $ n - 1 ] ) ; $ MaximumSum = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { $ MaximumSum = $ MaximumSum + abs ( $ finalSequence [ $ i ] - $ finalSequence [ $ i + 1 ] ) ; } $ MaximumSum = $ MaximumSum + abs ( $ finalSequence [ $ n - 1 ] - $ finalSequence [ 0 ] ) ; return $ MaximumSum ; } $ a = array ( 1 , 2 , 4 , 8 ) ; $ n = sizeof ( $ a ) ; echo MaxSumDifference ( $ a , $ n ) . "
 " ; ? >


"Maximum sum of all elements of array after performing given operations | Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array"
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 maxSum ( $ a , $ n ) { $ S = 0 ; $ S1 = maxSubArraySum ( $ a , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ S += $ a [ $ i ] ; return ( 2 * $ S1 - $ S ) ; } $ a = array ( -35 , 32 , -24 , 0 , 27 , -10 , 0 , -19 ) ; $ n = sizeof ( $ a ) ; echo ( maxSum ( $ a , $ n ) ) ;


"Maximum sum of cocktail glass in a 2D matrix | PHP implementation of the approach ; Function to return the maximum sum of a cocktail glass ; If no cocktail glass is possible ; Initialize max_sum with the mini ; Here loop runs ( R - 2 ) * ( C - 2 ) times considering different top left cells of cocktail glasses ; Considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; Update the max_sum ; Driver code"
How can the above be solved in PHP?


"Maximum sum of difference of adjacent elements | To find max sum of permutation ; Base case ; Otherwise max sum will be ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function maxSum ( $ n ) { if ( $ n == 1 ) return 1 ; else return ( $ n * ( $ n - 1 ) / 2 ) - 1 + $ n / 2 ; } $ n = 3 ; echo intval ( maxSum ( $ n ) ) ; ? >


"Maximum sum of distinct numbers such that LCM of these numbers is N | Returns maximum sum of numbers with LCM as N ; Initialize result ; Finding a divisor of n and adding it to max_sum ; Driver code"
How can the above be solved in PHP?

< ? php function maxSumLCM ( $ n ) { $ max_sum = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ max_sum += $ i ; if ( $ n / $ i != $ i ) $ max_sum += ( $ n / $ i ) ; } } return $ max_sum ; } $ n = 2 ; echo MaxSumLCM ( $ n ) ; ? >


"Maximum sum of distinct numbers with LCM as N | Returns maximum sum of numbers with LCM as N ; Finding a divisor of n and adding it to max_sum ; if ' i ' is divisor of ' N ' ; if both divisors are same then add it only once else add both ; Driver code"
How can the above be solved in PHP?

 < ? php function maxSumLCM ( $ n ) { $ max_sum = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ max_sum += $ i ; if ( $ n / $ i != $ i ) $ max_sum += ( $ n / $ i ) ; } } return $ max_sum ; } $ n = 2 ; echo MaxSumLCM ( $ n ) , "
 " ; ? >


"Maximum sum of elements from each row in the matrix | PHP Program to find row - wise maximum element sum considering elements in increasing order . ; Function to perform given task ; Getting the maximum element from last row ; Comparing it with the elements of above rows ; Maximum of current row . ; If we could not an element smaller than prev_max . ; Driver code"
How can the above be solved in PHP?

 < ? php $ N = 3 ; function getGreatestSum ( $ a ) { global $ N ; $ prev_max = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ prev_max < $ a [ $ N - 1 ] [ $ j ] ) $ prev_max = $ a [ $ N - 1 ] [ $ j ] ; $ sum = $ prev_max ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { $ curr_max = PHP_INT_MIN ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ prev_max > $ a [ $ i ] [ $ j ] and $ a [ $ i ] [ $ j ] > $ curr_max ) $ curr_max = $ a [ $ i ] [ $ j ] ; if ( $ curr_max == PHP_INT_MIN ) return -1 ; $ prev_max = $ curr_max ; $ sum += $ prev_max ; } return $ sum ; } $ a = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; echo getGreatestSum ( $ a ) , "
 " ; $ b = array ( array ( 4 , 5 , 6 ) , array ( 4 , 5 , 6 ) , array ( 4 , 5 , 6 ) ) ; echo getGreatestSum ( $ b ) , "
 " ; ? >


"Maximum sum of i * arr [ i ] among all rotations of a given array | An efficient PHP program to compute maximum sum of i * arr [ i ] ; Compute sum of all array elements ; Compute sum of i * arr [ i ] for initial configuration . ; Initialize result ; Compute values for other iterations ; Compute next value using previous value in O ( 1 ) time ; Update current value ; Update result if required ; Driver code"
How can the above be solved in PHP?

< ? php function maxSum ( $ arr , $ n ) { $ cum_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ cum_sum += $ arr [ $ i ] ; $ curr_val = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ curr_val += $ i * $ arr [ $ i ] ; $ res = $ curr_val ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ next_val = $ curr_val - ( $ cum_sum - $ arr [ $ i - 1 ] ) + $ arr [ $ i - 1 ] * ( $ n - 1 ) ; $ curr_val = $ next_val ; $ res = max ( $ res , $ next_val ) ; } return $ res ; } $ arr = array ( 8 , 3 , 1 , 2 ) ; $ n = sizeof ( $ arr ) ; echo maxSum ( $ arr , $ n ) ; ? >


"Maximum sum of i * arr [ i ] among all rotations of a given array | Returns maximum value of i * arr [ i ] ; Initialize result ; Consider rotation beginning with i for all possible values of i . ; Initialize sum of current rotation ; Compute sum of all values . We don 't actually rotate  the array, but compute sum  by finding indexes when  arr[i] is first element ; Update result if required ; Driver code"
How can the above be solved in PHP?

 < ? php function maxSum ( $ arr , $ n ) { $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_sum = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ index = ( $ i + $ j ) % $ n ; $ curr_sum += $ j * $ arr [ $ index ] ; } $ res = max ( $ res , $ curr_sum ) ; } return $ res ; } $ arr = array ( 8 , 3 , 1 , 2 ) ; $ n = sizeof ( $ arr ) ; echo maxSum ( $ arr , $ n ) , "
 " ; ? >


"Maximum sum of increasing order elements from n arrays | PHP program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Sort each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; j = - 1 means no element is found in a [ i ] so return 0 ; Driver Code"
How can the above be solved in PHP?


"Maximum sum of pairs with specific difference | Method to return maximum sum we can get by finding less than K difference pairs ; Sort elements to ensure every i and i - 1 is closest possible pair ; To get maximum possible sum , iterate from largest to smallest , giving larger numbers priority over smaller numbers . ; Case I : Diff of arr [ i ] and arr [ i - 1 ] is less then K , add to maxSum Case II : Diff between arr [ i ] and arr [ i - 1 ] is not less then K , move to next i since with sorting we know , arr [ i ] - arr [ i - 1 ] < arr [ i ] - arr [ i - 2 ] and so on . ; Assuming only positive numbers . ; When a match is found skip this pair ; Driver Code"
How can the above be solved in PHP?

< ? php function maxSumPairWithDifferenceLessThanK ( $ arr , $ N , $ k ) { $ maxSum = 0 ; sort ( $ arr ) ; for ( $ i = $ N - 1 ; $ i > 0 ; -- $ i ) { if ( $ arr [ $ i ] - $ arr [ $ i - 1 ] < $ k ) { $ maxSum += $ arr [ $ i ] ; $ maxSum += $ arr [ $ i - 1 ] ; -- $ i ; } } return $ maxSum ; } $ arr = array ( 3 , 5 , 10 , 15 , 17 , 12 , 9 ) ; $ N = sizeof ( $ arr ) ; $ K = 4 ; echo maxSumPairWithDifferenceLessThanK ( $ arr , $ N , $ K ) ; ? >


"Maximum sum of pairs with specific difference | method to return maximum sum we can get by finding less than K difference pair ; Sort input array in ascending order . ; dp [ i ] denotes the maximum disjoint pair sum we can achieve using first i elements ; if no element then dp value will be 0 ; first give previous value to dp [ i ] i . e . no pairing with ( i - 1 ) th element ; if current and previous element can form a pair ; update dp [ i ] by choosing maximum between pairing and not pairing ; last index will have the result ; Driver code"
How can the above be solved in PHP?

< ? php function maxSumPairWithDifferenceLessThanK ( $ arr , $ N , $ K ) { sort ( $ arr ) ; $ dp = array ( ) ; $ dp [ 0 ] = 0 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ dp [ $ i ] = $ dp [ $ i - 1 ] ; if ( $ arr [ $ i ] - $ arr [ $ i - 1 ] < $ K ) { if ( $ i >= 2 ) $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ i - 2 ] + $ arr [ $ i ] + $ arr [ $ i - 1 ] ) ; else $ dp [ $ i ] = max ( $ dp [ $ i ] , $ arr [ $ i ] + $ arr [ $ i - 1 ] ) ; } } return $ dp [ $ N - 1 ] ; } $ arr = array ( 3 , 5 , 10 , 15 , 17 , 12 , 9 ) ; $ N = sizeof ( $ arr ) ; $ K = 4 ; echo maxSumPairWithDifferenceLessThanK ( $ arr , $ N , $ K ) ; ? >


"Maximum sum of pairwise product in an array with negative allowed | PHP program for above implementation ; Function to find the maximum sum ; Sort the array first ; First multiply negative numbers pairwise and sum up from starting as to get maximum sum . ; Second multiply positive numbers pairwise and summed up from the last as to get maximum sum . ; To handle case if positive and negative numbers both are odd in counts . ; If one of them occurs odd times ; Driver code"
How can the above be solved in PHP?


"Maximum sum path in a matrix from top to bottom | PHP implementation to find the maximum sum path in a matrix ; function to find the maximum sum path in a matric ; if there is a single element only ; dp [ ] [ ] matrix to store the results of each iteration ; base case , copying elements of last row ; building up the dp [ ] [ ] matrix from bottom to the top row ; finding the maximum diagonal element in the ( i + 1 ) th row if that cell exists ; adding that ' max ' element to the mat [ i ] [ j ] element ; finding the maximum value from the first row of dp [ ] [ ] ; required maximum sum ; Driver Code"
How can the above be solved in PHP?


"Maximum sum subarray removing at most one element | Method returns maximum sum of all subarray where removing one element is also allowed ; Maximum sum subarrays in forward and backward directions ; Initialize current max and max so far . ; calculating maximum sum subarrays in forward direction ; storing current maximum till ith , in forward array ; calculating maximum sum subarrays in backward direction ; storing current maximum from ith , in backward array ; Initializing final ans by max_so_far so that , case when no element is removed to get max sum subarray is also handled ; choosing maximum ignoring ith element ; Driver Code"
How can the above be solved in PHP?

< ? php function maxSumSubarrayRemovingOneEle ( $ arr , $ n ) { $ fw = array ( ) ; $ bw = array ( ) ; $ cur_max = $ arr [ 0 ] ; $ max_so_far = $ arr [ 0 ] ; $ fw [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ cur_max = max ( $ arr [ $ i ] , $ cur_max + $ arr [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ cur_max ) ; $ fw [ $ i ] = $ cur_max ; } $ cur_max = $ max_so_far = $ bw [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ cur_max = max ( $ arr [ $ i ] , $ cur_max + $ arr [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ cur_max ) ; $ bw [ $ i ] = $ cur_max ; } $ fans = $ max_so_far ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) $ fans = max ( $ fans , $ fw [ $ i - 1 ] + $ bw [ $ i + 1 ] ) ; return $ fans ; } $ arr = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = count ( $ arr ) ; echo maxSumSubarrayRemovingOneEle ( $ arr , $ n ) ; ? >


"Maximum sum subsequence with at | PHP program to find maximum sum subsequence such that elements are at least k distance away . ; We fill MS from right to left . ; Driver code"
How can the above be solved in PHP?


"Maximum sum such that no two elements are adjacent | Set 2 | PHP program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code"
How can the above be solved in PHP?

< ? php $ maxLen = 10 ; $ dp = array_fill ( 0 , $ GLOBALS [ ' axLen ' , 0 ) ; $ v = array_fill ( 0 , $ GLOBALS [ ' axLen ' , 0 ) ; function maxSum ( $ arr , $ i , $ n ) { if ( $ i >= $ n ) return 0 ; if ( $ GLOBALS [ ' v ' ] [ $ i ] ) return $ GLOBALS [ ' dp ' ] [ $ i ] ; $ GLOBALS [ ' v ' ] [ $ i ] = 1 ; $ GLOBALS [ ' dp ' ] [ $ i ] = max ( maxSum ( $ arr , $ i + 1 , $ n ) , $ arr [ $ i ] + maxSum ( $ arr , $ i + 2 , $ n ) ) ; return $ GLOBALS [ ' dp ' ] [ $ i ] ; } $ arr = array ( 12 , 9 , 7 , 33 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , 0 , $ n ) ; ? >


"Maximum trace possible for any sub | PHP implementation of the approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code"
How can the above be solved in PHP?

< ? php $ N = 3 ; function MaxTraceSub ( $ mat ) { global $ N ; $ max_trace = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { $ r = $ i ; $ s = $ j ; $ trace = 0 ; while ( $ r < $ N && $ s < $ N ) { $ trace += $ mat [ $ r ] [ $ s ] ; $ r ++ ; $ s ++ ; $ max_trace = max ( $ trace , $ max_trace ) ; } } } return $ max_trace ; } $ mat = array ( array ( 10 , 2 , 5 ) , array ( 6 , 10 , 4 ) , array ( 2 , 7 , -10 ) ) ; print ( MaxTraceSub ( $ mat ) ) ; ? >


"Maximum value of an integer for which factorial can be calculated on a machine | PHP program to find maximum value of an integer for which factorial can be calculated on your system ; when fact crosses its size , it gives negative value ; Driver Code"
How can the above be solved in PHP?

< ? php function findMaxValue ( ) { $ res = 2 ; $ fact = 2 ; $ pos = -1 ; while ( true ) { $ mystring = $ fact ; $ pos = strpos ( $ mystring , ' E ' ) ; if ( $ pos > 0 ) break ; $ res ++ ; $ fact = $ fact * $ res ; } return $ res - 1 ; } echo " Maximum ▁ value ▁ of " . " ▁ integer ▁ " . findMaxValue ( ) ; ? >


"Maximum value of | arr [ 0 ] | Function to return the maximum required value ; Driver code"
How can the above be solved in PHP?

< ? php function maxValue ( $ n ) { if ( $ n == 1 ) return 0 ; return ( ( $ n * $ n / 2 ) - 1 ) ; } $ n = 4 ; echo maxValue ( $ n ) ; ? >


"Maximum value of | arr [ i ] | PHP program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Iterating two for loop , one for i and another for j . ; Evaluating | arr [ i ] - arr [ j ] | + | i - j | and compare with previous maximum . ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 10 ; function findValue ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ ans = max ( $ ans , abs ( $ arr [ $ i ] - $ arr [ $ j ] ) + abs ( $ i - $ j ) ) ; return $ ans ; } $ arr = array ( 1 , 2 , 3 , 1 ) ; $ n = count ( $ arr ) ; echo findValue ( $ arr , $ n ) ; ? >


"Maximum value of | arr [ i ] | Return maximum | arr [ i ] - arr [ j ] | + | i - j | ; Calculating first_array and second_array ; Finding maximum and minimum value in first_array ; Storing the difference between maximum and minimum value in first_array ; Finding maximum and minimum value in second_array ; Storing the difference between maximum and minimum value in second_array ; Driver Code"
How can the above be solved in PHP?


"Maximum value with the choice of either dividing or considering as it is | function for calculating max possible result ; Compute remaining values in bottom up manner . ; Driver Code"
How can the above be solved in PHP?


"Maximum weight path ending at any element of last row in a matrix | Function which return the maximum weight path sum ; creat 2D matrix to store the sum of the path ; Initialize first column of total weight array ( dp [ i to N ] [ 0 ] ) ; Calculate rest path sum of weight matrix ; find the max weight path sum to rech the last row ; return maximum weight path sum ; Driver program"
How can the above be solved in PHP?


"Mean of range in array | To find mean of range in l to r ; Both sum and count are initialize to 0 ; To calculate sum and number of elements in range l to r ; Calculate floor value of mean ; Returns mean of array in range l to r ; Driver Code"
How can the above be solved in PHP?

 < ? php function findMean ( $ arr , $ l , $ r ) { $ sum = 0 ; $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ count ++ ; } $ mean = floor ( $ sum / $ count ) ; return $ mean ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; echo findMean ( $ arr , 0 , 2 ) , "
 " ; echo findMean ( $ arr , 1 , 3 ) , "
 " ; echo findMean ( $ arr , 0 , 4 ) , "
 " ; ? >


"Mean | Function for calculating mean ; Driver Code"
How can the above be solved in PHP?

 < ? php function findMean ( & $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; return ( double ) $ sum / ( double ) $ n ; } $ a = array ( 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ) ; $ n = sizeof ( $ a ) ; echo " Mean ▁ = ▁ " . findMean ( $ a , $ n ) . "
 " ; ? >


"Median after K additional integers | Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; Driver Code"
How can the above be solved in PHP?

< ? php function printMedian ( $ arr , $ n , $ K ) { sort ( $ arr ) ; echo $ arr [ ( $ n + $ K ) / 2 ] ; } $ arr = array ( 5 , 3 , 2 , 8 ) ; $ k = 3 ; $ n = count ( $ arr ) ; printMedian ( $ arr , $ n , $ k ) ; ? >


"Median and Mode using Counting Sort | Function that sort input array a [ ] and calculate mode and median using counting sort . ; The output array b [ ] will have sorted array ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Update count [ ] array with sum ; Sorted output array b [ ] to calculate median ; Median according to odd and even array size respectively . ; Output the result ; Driver Code"
How can the above be solved in PHP?


"Median of two sorted arrays of different sizes | Set 1 ( Linear ) | This function returns median of a [ ] and b [ ] . Assumptions in this function : Both a [ ] and b [ ] are sorted arrays ; Current index of i / p array a [ ] ; Current index of i / p array b [ ] ; Below is to handle the case where all elements of a [ ] are smaller than smallest ( or first ) element of b [ ] or a [ ] is empty ; Below is to handle case where all elements of b [ ] are smaller than smallest ( or first ) element of a [ ] or b [ ] is empty ; Below is to handle the case where sum of number of elements of the arrays is even ; Below is to handle the case where sum of number of elements of the arrays is odd ; Driver Code"
How can the above be solved in PHP?


"Median of two sorted arrays of same size | This function returns median of ar1 [ ] and ar2 [ ] . Assumptions in this function : Both ar1 [ ] and ar2 [ ] are sorted arrays Both have n elements ; Since there are 2 n elements , median will be average of elements at index n - 1 and n in the array obtained after merging ar1 and ar2 ; Below is to handle case where all elements of ar1 [ ] are smaller than smallest ( or first ) element of ar2 [ ] ; Below is to handle case where all elements of ar2 [ ] are smaller than smallest ( or first ) element of ar1 [ ] ; equals sign because if two arrays have some common elements ; Store the prev median ; Store the prev median ; Driver Code"
How can the above be solved in PHP?


"Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | PHP code for median with case of returning double value when even number of elements are present in both array combinely ; Function to find max ; Function to find minimum ; Function to find median of two sorted arrays ; if i = n , it means that Elements from a [ ] in the second half is an empty set . and if j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . Searching on right ; if i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have any elements  in the first half from a[]  so we returning the last  element in b[] from the  first half. ; and this condition happens when we don 't have any  elements in the first half  from b[] so we returning the  last element in a[] from the  first half. ; calculating the median . If number of elements is odd there is one middle element . ; Elements from a [ ] in the second half is an empty set . ; Elements from b [ ] in the second half is an empty set . ; Driver code ; we need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )"
How can the above be solved in PHP?

< ? php $ median = 0 ; $ i = 0 ; $ j = 0 ; function maximum ( $ a , $ b ) { return $ a > $ b ? $ a : $ b ; } function minimum ( $ a , $ b ) { return $ a < $ b ? $ a : $ b ; } function findMedianSortedArrays ( & $ a , $ n , & $ b , $ m ) { global $ median , $ i , $ j ; $ min_index = 0 ; $ max_index = $ n ; while ( $ min_index <= $ max_index ) { $ i = intval ( ( $ min_index + $ max_index ) / 2 ) ; $ j = intval ( ( ( $ n + $ m + 1 ) / 2 ) - $ i ) ; if ( $ i < $ n && $ j > 0 && $ b [ $ j - 1 ] > $ a [ $ i ] ) $ min_index = $ i + 1 ; else if ( $ i > 0 && $ j < $ m && $ b [ $ j ] < $ a [ $ i - 1 ] ) $ max_index = $ i - 1 ; else { if ( $ i == 0 ) $ median = $ b [ $ j - 1 ] ; else if ( $ j == 0 ) $ median = $ a [ $ i - 1 ] ; else $ median = maximum ( $ a [ $ i - 1 ] , $ b [ $ j - 1 ] ) ; break ; } } if ( ( $ n + $ m ) % 2 == 1 ) return $ median ; if ( $ i == $ n ) return ( ( $ median + $ b [ $ j ] ) / 2.0 ) ; if ( $ j == $ m ) return ( ( $ median + $ a [ $ i ] ) / 2.0 ) ; return ( ( $ median + minimum ( $ a [ $ i ] , $ b [ $ j ] ) ) / 2.0 ) ; } $ a = array ( 900 ) ; $ b = array ( 10 , 13 , 14 ) ; $ n = count ( $ a ) ; $ m = count ( $ b ) ; if ( $ n < $ m ) echo ( " The ▁ median ▁ is ▁ : ▁ " . findMedianSortedArrays ( $ a , $ n , $ b , $ m ) ) ; else echo ( " The ▁ median ▁ is ▁ : ▁ " . findMedianSortedArrays ( $ b , $ m , $ a , $ n ) ) ; ? >


"Median | Function for calculating median ; First we sort the array ; check for even case ; Driver Code"
How can the above be solved in PHP?

< ? php function findMedian ( & $ a , $ n ) { sort ( $ a ) ; if ( $ n % 2 != 0 ) return ( double ) $ a [ $ n / 2 ] ; return ( double ) ( $ a [ ( $ n - 1 ) / 2 ] + $ a [ $ n / 2 ] ) / 2.0 ; } $ a = array ( 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ) ; $ n = sizeof ( $ a ) ; echo " Median ▁ = ▁ " . findMedian ( $ a , $ n ) ; ? >


"Memoization ( 1D , 2D and 3D ) | Fibonacci Series using Recursion ; Base case ; recursive calls ; 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 = 6 ; echo fib ( $ n ) ; ? >


"Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | PHP program to merge K sorted arrays ; Function to perform merge operation ; to store the starting point of left and right array ; to store the size of left and right array ; array to temporarily store left and right array ; storing data in left array ; storing data in right array ; to store the current index of temporary left and right array ; to store the current index for output array ; two pointer merge for two sorted arrays ; Code to drive merge - sort and create recursion tree ; base step to initialize the output array before performing merge operation ; to sort left half ; to sort right half ; merge the left and right half ; input 2D - array ; Number of arrays ; Output array ; Print merged array"
How can the above be solved in PHP?


"Merge an array of size n into another array of size m + n | Assuming - 1 is filled for the places where element is not available ; Function to move m elements at the end of array mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN"
How can the above be solved in PHP?

 < ? php $ NA = -1 ; function moveToEnd ( & $ mPlusN , $ size ) { global $ NA ; $ j = $ size - 1 ; for ( $ i = $ size - 1 ; $ i >= 0 ; $ i -- ) if ( $ mPlusN [ $ i ] != $ NA ) { $ mPlusN [ $ j ] = $ mPlusN [ $ i ] ; $ j -- ; } } function merge ( & $ mPlusN , & $ N , $ m , $ n ) { $ i = $ n ; $ j = 0 ; $ k = 0 ; while ( $ k < ( $ m + $ n ) ) { if ( ( $ j == $ n ) || ( $ i < ( $ m + $ n ) && $ mPlusN [ $ i ] <= $ N [ $ j ] ) ) { $ mPlusN [ $ k ] = $ mPlusN [ $ i ] ; $ k ++ ; $ i ++ ; } else { $ mPlusN [ $ k ] = $ N [ $ j ] ; $ k ++ ; $ j ++ ; } } } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ mPlusN = array ( 2 , 8 , $ NA , $ NA , $ NA , 13 , $ NA , 15 , 20 ) ; $ N = array ( 5 , 7 , 9 , 25 ) ; $ n = sizeof ( $ N ) ; $ m = sizeof ( $ mPlusN ) - $ n ; moveToEnd ( $ mPlusN , $ m + $ n ) ; merge ( $ mPlusN , $ N , $ m , $ n ) ; printArray ( $ mPlusN , $ m + $ n ) ; ? >


"Merge two sorted arrays with O ( 1 ) extra space | Merge ar1 [ ] and ar2 [ ] with O ( 1 ) extra space ; Iterate through all elements of ar2 [ ] starting from the last element ; Find the smallest element greater than ar2 [ i ] . Move all elements one position ahead till the smallest greater element is not found ; If there was a greater element ; Driver program"
How can the above be solved in PHP?


"Merge two sorted arrays | Merge $arr1 [ 0. . $n1 - 1 ] and $arr2 [ 0. . $n2 - 1 ] into $arr3 [ 0. . $n1 + $n2 - 1 ] ; Traverse both array ; Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array ; Store remaining elements of first array ; Store remaining elements of second array ; Driver code"
How can the above be solved in PHP?


"Mersenne Prime | Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in prime [ i ] will finally be false if i is Not a prime , else true ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to generate mersenne primes less than or equal to n ; Generating primes using Sieve ; Generate all numbers of the form 2 ^ k - 1 and smaller than or equal to n . ; Checking whether number is prime and is one less then the power of 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function SieveOf ( $ n ) { $ prime = array ( $ n + 1 ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ prime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } return $ prime ; } function mersennePrimes ( $ n ) { $ prime = SieveOf ( $ n ) ; for ( $ k = 2 ; ( ( 1 << $ k ) - 1 ) <= $ n ; $ k ++ ) { $ num = ( 1 << $ k ) - 1 ; if ( $ prime [ $ num ] ) echo $ num . " " ; } } $ n = 31 ; echo " Mersenne ▁ prime ▁ numbers ▁ smaller ▁ " . " than ▁ or ▁ equal ▁ to ▁ $ n ▁ " . mersennePrimes ( $ n ) ; ? >


"Meta Binary Search | One | Function to show the working of Meta binary search ; Set number of bits to represent ; largest array index This is redundant and will cause error for some case while ( ( 1 << $lg ) < $n - 1 ) $lg += 1 ; ; Incrementally construct the index of the target value ; find the element in one direction and update $position ; if element found return $pos otherwise - 1 ; Driver code"
How can the above be solved in PHP?

< ? php function bsearch ( $ A , $ key_to_search , $ n ) { $ lg = log ( $ n - 1 , 2 ) + 1 ; $ pos = 0 ; for ( $ i = $ lg - 1 ; $ i >= 0 ; $ i -- ) { if ( $ A [ $ pos ] == $ key_to_search ) return $ pos ; $ new_pos = $ pos | ( 1 << $ i ) ; if ( ( $ new_pos < $ n ) && ( $ A [ $ new_pos ] <= $ key_to_search ) ) $ pos = $ new_pos ; } return ( ( $ A [ $ pos ] == $ key_to_search ) ? $ pos : -1 ) ; } $ A = [ -2 , 10 , 100 , 250 , 32315 ] ; $ ans = bsearch ( $ A , 10 , 5 ) ; echo $ ans ; ? >


"Midpoint ellipse drawing algorithm | PHP program for implementing Mid - Point Ellipse Drawing Algorithm ; Initial decision parameter of region 1 ; For region 1 ; Print points based on 4 - way symmetry ; Checking and updating value of decision parameter based on algorithm ; Decision parameter of region 2 ; Plotting points of region 2 ; printing points based on 4 - way symmetry ; Checking and updating parameter value based on algorithm ; To draw a ellipse of major and minor radius 15 , 10 centred at ( 50 , 50 )"
How can the above be solved in PHP?