"Longest alternating sub | Function to calculate alternating sub - array for each index of array elements ; Initialize the base state of len [ ] ; Calculating value for each element ; If both elements are different then add 1 to next len [ i + 1 ] ; else initialize to 1 ; Print lengths of binary subarrays . ; Driver Code"
Solution in php:

< ? php function alternateSubarray ( & $ arr , $ n ) { $ len = array_fill ( 0 , $ n , NULL ) ; $ len [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; -- $ i ) { if ( $ arr [ $ i ] ^ $ arr [ $ i + 1 ] == 1 ) $ len [ $ i ] = $ len [ $ i + 1 ] + 1 ; else $ len [ $ i ] = 1 ; } for ( $ i = 0 ; $ i < $ n ; ++ $ i ) echo $ len [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 0 , 1 , 0 , 0 , 1 ) ; $ n = sizeof ( $ arr ) ; alternateSubarray ( $ arr , $ n ) ; ? >


"Longest common subsequence with permutations allowed | Function to calculate longest string str1 -- > first string str2 -- > second string count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest string whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; append character ( ' a ' + i ) in resultant string ' result ' by min ( count1 [ $i ] , count2 [ $i ] ) times ; Driver Code"
Solution in php:

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


"Longest rod that can be inserted within a right circular cylinder | Function to find the side of the cube ; height and radius cannot be negative ; length of rod ; Driver code"
Solution in php:


"Longest sub | Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code"
Solution in php:

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


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

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


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

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


"Longest subarray with elements divisible by k | function to find longest subarray ; This will contain length of longest subarray found ; Driver code"
Solution in php:

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


"Longest subarray with elements having equal modulo K | function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code"
Solution in php:

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


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

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


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

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


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

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


"Longest substring of vowels | PHP program to find the longest substring of vowels . ; Increment current count if s [ i ] is vowel ; check previous value is greater then or not ; Driver code"
Solution in php:

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


"Lucas Numbers | Iterative function ; declaring base values for positions 0 and 1 ; generating number ; Driver Code"
Solution in php:


"Lucas Numbers | recursive function ; Base cases ; recurrence relation ; Driver Code"
Solution in php:

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


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

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


"Lychrel Number Implementation | Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver Code"
Solution in php:

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


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


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

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


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

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


"Make all numbers of an array equal | Function that returns true if all the array elements can be made equal with the given operation ; Divide number by 2 ; Divide number by 3 ; Driver code"
Solution in php:

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


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

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


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

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


"Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Driver code"
Solution in php:

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


"Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code"
Solution in php:


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

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


"Making zero array by decrementing pairs of adjacent | PHP implementation of the above approach ; converting array element into number ; Check if divisible by 11 ; Driver Code"
Solution in php:

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


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

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


"Master Theorem For Subtract and Conquer Recurrences | PHP code for the above approach ; Driver Code"
Solution in php:

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


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

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


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


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


"Max occurring divisor in an interval | function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver code"
Solution in php:

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


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

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


"Maximize a value for a semicircle of given radius | Function to find the maximum value of F ; using the formula derived for getting the maximum value of F ; Drivers code"
Solution in php:

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


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

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


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

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


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

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


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


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


"Maximize the bitwise OR of an array | Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Driver Code"
Solution in php:


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


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

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


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

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


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

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


"Maximize the profit by selling at | Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code"
Solution in php:

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


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

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


"Maximize the sum of arr [ i ] * i | PHP program to find the maximum value of i * arr [ i ] ; Sort the array ; Finding the sum of arr [ i ] * i ; Driver Code"
Solution in php:

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


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


"Maximize the sum of products of the degrees between any two vertices of the tree | Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code"
Solution in php:

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


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

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


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


"Maximizing Probability of one type from N containers | Returns the Maximum probability for Drawing 1 copy of number A from N containers with N copies each of numbers A and B ; Pmax = N / ( N + 1 ) ; 1. N = 1 ; 2. N = 2 ; 3. N = 10"
Solution in php:

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


"Maximum AND value of a pair in an array | Function for finding maximum and value pair ; Driver Code"
Solution in php:

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


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

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


"Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; If there are only two values in the range [ L , R ] ; Driver code"
Solution in php:


"Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code"
Solution in php:

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


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

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


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


"Maximum OR sum of sub | function to find maximum OR sum ; OR sum of all the elements in both arrays ; Driver Code"
Solution in php:


"Maximum Primes whose sum is equal to given N | Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code"
Solution in php:

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


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

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


"Maximum Product Cutting | DP | Utility function to get the maximum of two and three integers ; The main function that returns maximum product obtainable from a rope of length n ; Base cases ; Make a cut at different places and take the maximum of all ; Return the maximum of all values ; Driver Code"
Solution in php:

< ? php function max_1 ( $ a , $ b , $ c ) { return max ( $ a , max ( $ b , $ c ) ) ; } function maxProd ( $ n ) { if ( $ n == 0 $ n == 1 ) return 0 ; $ max_val = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ max_val = max_1 ( $ max_val , $ i * ( $ n - $ i ) , maxProd ( $ n - $ i ) * $ i ) ; return $ max_val ; } echo " Maximum ▁ Product ▁ is ▁ " . maxProd ( 10 ) ; ? >


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

< ? php function findMaxProduct ( & $ arr , $ n ) { $ ans = 0 ; $ maxval = 1 ; $ minval = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) { $ maxval = $ maxval * $ arr [ i ] ; $ minval = min ( 1 , $ minval * $ arr [ $ i ] ) ; } else if ( $ arr [ $ i ] == 0 ) { $ minval = 1 ; $ maxval = 0 ; } else if ( $ arr [ $ i ] < 0 ) { $ prevMax = $ maxval ; $ maxval = $ minval * $ arr [ $ i ] ; $ minval = $ prevMax * $ arr [ $ i ] ; } $ ans = max ( $ ans , $ maxval ) ; if ( $ maxval <= 0 ) { $ maxval = 1 ; } } return $ ans ; } $ arr = array ( 0 , -4 , 0 , -2 ) ; $ n = sizeof ( $ arr ) ; echo findMaxProduct ( $ arr , $ n ) ; ? >


"Maximum Subarray Sum Excluding Certain Elements | Function to check the element present in array B ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call"
Solution in php:

 < ? php function isPresent ( $ B , $ m , $ x ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ B [ $ i ] == $ x ) return true ; return false ; } function findMaxSubarraySumUtil ( $ A , $ B , $ n , $ m ) { $ max_so_far = PHP_INT_MIN ; $ curr_max = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPresent ( $ B , $ m , $ A [ $ i ] ) ) { $ curr_max = 0 ; continue ; } $ curr_max = max ( $ A [ $ i ] , $ curr_max + $ A [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; } return $ max_so_far ; } function findMaxSubarraySum ( $ A , $ B , $ n , $ m ) { $ maxSubarraySum = findMaxSubarraySumUtil ( $ A , $ B , $ n , $ m ) ; if ( $ maxSubarraySum == PHP_INT_MIN ) { echo ( " Maximum ▁ Subarray ▁ " . " Sum ▁ cant ▁ be ▁ found
 " ) ; } else { echo ( " The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ▁ " . $ maxSubarraySum . "
 " ) ; } } $ A = array ( 3 , 4 , 5 , -4 , 6 ) ; $ B = array ( 1 , 8 , 5 ) ; $ n = count ( $ A ) ; $ m = count ( $ B ) ; findMaxSubarraySum ( $ A , $ B , $ n , $ m ) ; ? >


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


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

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


"Maximum Sum Increasing Subsequence | DP | maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code"
Solution in php:

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


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


"Maximum Sum of Products of Two Arrays | Function that calculates maximum sum of products of two arrays ; Variable to store the sum of products of array elements ; length of the arrays ; Sorting both the arrays ; Traversing both the arrays and calculating sum of product ; Driver code"
Solution in php:

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


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

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


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

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


"Maximum XOR value of a pair from a range | method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver Code"
Solution in php:

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


"Maximum XOR | function to calculate maximum XOR value ; Return ( 2 ^ c - 1 ) ; Driver Code"
Solution in php:


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

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


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

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


"Maximum absolute difference of value and index sums | Function to return maximum absolute difference in linear time . ; max and min variables as described in algorithm . ; Updating max and min variables as described in algorithm . ; Calculating maximum absolute difference . ; Driver Code"
Solution in php:

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


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


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


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

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


"Maximum area of quadrilateral | PHP program to find maximum are of a quadrilateral ; Calculating the semi - perimeter of the given quadrilateral ; Applying Brahmagupta 's formula to  get maximum area of quadrilateral ; Driver code"
Solution in php:

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


"Maximum area of rectangle possible with given perimeter | Function to find max area ; return area ; Driver code"
Solution in php:

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


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

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


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


"Maximum binomial coefficient term value | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return maximum binomial coefficient term value . ; if n is even ; if n is odd ; Driver Code"
Solution in php:

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


"Maximum consecutive repeating character in string | Returns the maximum repeating character in a given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code"
Solution in php:

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


"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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution in php: