"Min Cost Path | DP | A Naive recursive implementation of MCP ( Minimum Cost Path ) problem ; A utility function that returns minimum of 3 integers ; Returns cost of minimum cost path from ( 0 , 0 ) to ( m , n ) in mat [ R ] [ C ] ; Driver Code"
How can the above be solved in PHP?

< ? php $ R = 3 ; $ C = 3 ; function min1 ( $ x , $ y , $ z ) { if ( $ x < $ y ) return ( $ x < $ z ) ? $ x : $ z ; else return ( $ y < $ z ) ? $ y : $ z ; } function minCost ( $ cost , $ m , $ n ) { global $ R ; global $ C ; if ( $ n < 0 $ m < 0 ) return PHP_INT_MAX ; else if ( $ m == 0 && $ n == 0 ) return $ cost [ $ m ] [ $ n ] ; else return $ cost [ $ m ] [ $ n ] + min1 ( minCost ( $ cost , $ m - 1 , $ n - 1 ) , minCost ( $ cost , $ m - 1 , $ n ) , minCost ( $ cost , $ m , $ n - 1 ) ) ; } $ cost = array ( array ( 1 , 2 , 3 ) , array ( 4 , 8 , 2 ) , array ( 1 , 5 , 3 ) ) ; echo minCost ( $ cost , 2 , 2 ) ; ? >


"Min Cost Path | DP | DP implementation of MCP problem ; Instead of following line , we can use int tc [ m + 1 ] [ n + 1 ] or dynamically allocate memory to save space . The following line is used to keep the program simple and make it working on all compilers . ; Initialize first column of total cost ( tc ) array ; Initialize first row of tc array ; Construct rest of the tc array ; returns minimum of 3 integers ; Driver Code"
How can the above be solved in PHP?


"Min flips of continuous characters to make all characters same in a string | To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver Code"
How can the above be solved in PHP?

< ? php function findFlips ( $ str , $ n ) { $ last = ' ▁ ' ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ last != $ str [ $ i ] ) $ res ++ ; $ last = $ str [ $ i ] ; } return intval ( $ res / 2 ) ; } $ str = "00011110001110" ; $ n = strlen ( $ str ) ; echo findFlips ( $ str , $ n ) ; ? >


"Minimal moves to form a string by adding characters or appending string itself | function to return the minimal number of moves ; initializing dp [ i ] to INT_MAX ; initialize both strings to null ; base case ; check if it can be appended ; addition of character takes one step ; appending takes 1 step , and we directly reach index i * 2 + 1 after appending so the number of steps is stord in i * 2 + 1 ; Driver Code ; function call to return minimal number of moves"
How can the above be solved in PHP?


"Minimal operations to make a number magical | function to calculate the minimal changes ; maximum digits that can be changed ; nested loops to generate all 6 digit numbers ; counter to count the number of change required ; if first digit is equal ; if 2 nd digit is equal ; if 3 rd digit is equal ; if 4 th digit is equal ; if 5 th digit is equal ; if 6 th digit is equal ; checks if less then the previous calculate changes ; returns the answer ; number stored in string ; prints the minimum operations"
How can the above be solved in PHP?


"Minimize ( max ( A [ i ] , B [ j ] , C [ k ] ) | PHP code for above approach ; calculating min difference from last index of lists ; checking condition ; calculating max term from list ; Moving to smaller value in the array with maximum out of three . ; Driver code"
How can the above be solved in PHP?


"Minimize Cost with Replacement with other allowed | this function returns the minimum cost of the array ; Code driven"
How can the above be solved in PHP?

 < ? php function getMinCost ( $ arr , $ n ) { $ min_ele = min ( $ arr ) ; return $ min_ele * ( $ n - 1 ) ; } $ arr = array ( 4 , 2 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo getMinCost ( $ arr , $ n ) ; #This  code contributed by ajit
? >


"Minimize sum of adjacent difference with removal of one element from array | Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Drivers code"
How can the above be solved in PHP?

< ? php function findMinRemoval ( $ arr , $ n ) { $ value = 0 ; $ maximum = 0 ; if ( $ n == 1 ) return 0 ; $ temp = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i != 0 && $ i != $ n - 1 ) { $ value = $ value + abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; $ temp = abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) + abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) - abs ( $ arr [ $ i - 1 ] - $ arr [ $ i + 1 ] ) ; } else if ( $ i == 0 ) { $ value = $ value + abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; $ temp = abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; } else $ temp = abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) ; $ maximum = max ( $ maximum , $ temp ) ; } return ( $ value - $ maximum ) ; } $ arr = array ( 1 , 5 , 3 , 2 , 10 ) ; $ n = count ( $ arr ) ; echo findMinRemoval ( $ arr , $ n ) ; ? >


"Minimize the absolute difference of sum of two subsets | function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; Driver code"
How can the above be solved in PHP?

< ? php function subsetDifference ( $ n ) { $ s = $ n * ( $ n + 1 ) / 2 ; if ( $ n % 4 == 0 ) { echo " First ▁ subset ▁ sum ▁ = ▁ " , floor ( $ s / 2 ) ; echo " Second subset sum = " , floor ( $ s / 2 ) ; echo " Difference = " } else { if ( $ n % 4 == 1 $ n % 4 == 2 ) { echo " First ▁ subset ▁ sum ▁ = ▁ " , floor ( $ s / 2 ) ; echo " Second subset sum = " , floor ( $ s / 2 + 1 ) ; echo " Difference = " } else { echo " First ▁ subset ▁ sum ▁ = ▁ " , floor ( $ s / 2 ) ; echo " Second subset sum = " , floor ( $ s / 2 ) ; echo " Difference = " ▁ , ▁ 0 ; } } } $ n = 6 ; subsetDifference ( $ n ) ; ? >


"Minimize the cost to split a number | check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code"
How can the above be solved in PHP?

< ? php function isPrime ( $ x ) { for ( $ i = 2 ; $ i * $ i <= $ x ; $ i ++ ) { if ( $ x % $ i == 0 ) return 0 ; } return 1 ; } function minimumCost ( $ n ) { if ( isPrime ( $ n ) ) return 1 ; if ( $ n % 2 == 1 && isPrime ( $ n - 2 ) ) return 2 ; if ( $ n % 2 == 0 ) return 2 ; return 3 ; } $ n = 6 ; echo ( minimumCost ( $ n ) ) ;


"Minimize the difference between minimum and maximum elements | Function to minimize the difference between minimum and maximum elements ; Find max and min elements of the array ; Check whether the difference between the max and min element is less than or equal to k or not ; Calculate average of max and min ; If the array element is greater than the average then decrease it by k ; If the array element is smaller than the average then increase it by k ; Find max and min of the modified array ; return the new difference ; Driver code"
How can the above be solved in PHP?

 < ? php function minimizeDiff ( & $ arr , $ n , $ k ) { $ max = max ( $ arr ) ; $ min = min ( $ arr ) ; if ( ( $ max - $ min ) <= $ k ) { return ( $ max - $ min ) ; } $ avg = ( $ max + $ min ) / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ avg ) $ arr [ $ i ] -= $ k ; else $ arr [ $ i ] += $ k ; } $ max = max ( $ arr ) ; $ min = min ( $ arr ) ; return ( $ max - $ min ) ; } $ arr = array ( 3 , 16 , 12 , 9 , 20 ) ; $ n = 5 ; $ k = 3 ; echo " Max ▁ height ▁ difference ▁ = ▁ " . minimizeDiff ( $ arr , $ n , $ k ) . "
 " ; ? >


"Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference ; If current element is greater than max ; max will become secondMax ; Update the max ; If current element is greater than secondMax but smaller than max ; Update the secondMax ; If current element is smaller than min ; min will become secondMin ; Update the min ; If current element is smaller than secondMin but greater than min ; Update the secondMin ; Minimum of the two possible differences ; Driver code"
How can the above be solved in PHP?

< ? php function findMinDifference ( $ arr , $ n ) { $ min__ = $ secondMax = ( $ arr [ 0 ] < $ arr [ 1 ] ) ? $ arr [ 0 ] : $ arr [ 1 ] ; $ max__ = $ secondMin = ( $ arr [ 0 ] < $ arr [ 1 ] ) ? $ arr [ 1 ] : $ arr [ 0 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ max__ ) { $ secondMax = $ max__ ; $ max__ = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ secondMax ) { $ secondMax = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] < $ min__ ) { $ secondMin = $ min__ ; $ min__ = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] < $ secondMin ) { $ secondMin = $ arr [ $ i ] ; } } $ diff = min ( $ max__ - $ secondMin , $ secondMax - $ min__ ) ; return $ diff ; } $ arr = array ( 1 , 2 , 4 , 3 , 4 ) ; $ n = count ( $ arr ) ; print ( findMinDifference ( $ arr , $ n ) ) ; ? >


"Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference ; Sort the given array ; When minimum element is removed ; When maximum element is removed ; Return the minimum of diff1 and diff2 ; Driver Code"
How can the above be solved in PHP?

< ? php function findMinDifference ( $ arr , $ n ) { sort ( $ arr , 0 ) ; $ diff1 = $ arr [ $ n - 1 ] - $ arr [ 1 ] ; $ diff2 = $ arr [ $ n - 2 ] - $ arr [ 0 ] ; return min ( $ diff1 , $ diff2 ) ; } $ arr = array ( 1 , 2 , 4 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo findMinDifference ( $ arr , $ n ) ;


"Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | Function to count numbers ; Count the number of ' a ' , ' b ' and ' c ' in string ; If equal previously ; If not a multiple of 3 ; Increase the number of a ' s ▁ by ▁ ▁ ▁ removing ▁ extra ▁ ' b ' and ;c;  ; Check if it is ' b ' and it is more than n / 3 ; Check if it is ' c ' and it more than n / 3 ; Increase the number of b ' s ▁ by ▁ ▁ ▁ removing ▁ extra ▁ ' c '  ; Check if it is ' c ' and it more than n / 3 ; Increase the number of c 's from back  ; Check if it is ' a ' and it is more than n / 3 ; Increase the number of b 's from back  ; Check if it is ' a ' and it is more than n / 3 ; Increase the number of c 's from back  ; Check if it is ' b ' and it more than n / 3 ; Driver Code"
How can the above be solved in PHP?


"Minimize the sum of product of two arrays with permutations allowed | Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driver Code"
How can the above be solved in PHP?

< ? php function minValue ( $ A , $ B , $ n ) { sort ( $ A ) ; sort ( $ A , $ n ) ; sort ( $ B ) ; sort ( $ B , $ n ) ; $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ result += ( $ A [ $ i ] * $ B [ $ n - $ i - 1 ] ) ; return $ result ; } $ A = array ( 3 , 1 , 1 ) ; $ B = array ( 6 , 5 , 4 ) ; $ n = sizeof ( $ A ) / sizeof ( $ A [ 0 ] ) ; echo minValue ( $ A , $ B , $ n ) ; ? >


"Minimize the sum of the squares of the sum of elements of each group the array is divided into | Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code"
How can the above be solved in PHP?

< ? php function findAnswer ( $ n , $ arr ) { sort ( $ arr ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n / 2 ; ++ $ i ) { $ sum += ( $ arr [ $ i ] + $ arr [ $ n - $ i - 1 ] ) * ( $ arr [ $ i ] + $ arr [ $ n - $ i - 1 ] ) ; } return $ sum ; } $ arr = array ( 53 , 28 , 143 , 5 ) ; $ n = count ( $ arr ) ; echo findAnswer ( $ n , $ arr ) ; ? >


"Minimize the value of N by applying the given operations | function to return the product of distinct prime factors of a number ; find distinct prime ; Driver code"
How can the above be solved in PHP?

 < ? php function minimum ( $ n ) { $ product = 1 ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { while ( $ n % $ i == 0 ) $ n = $ n / $ i ; $ product = $ product * $ i ; } } if ( $ n >= 2 ) $ product = $ product * $ n ; return $ product ; } $ n = 20 ; echo minimum ( $ n ) , "
 " ; ? >


"Minimum Cost To Make Two Strings Identical | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; Returns cost of making X [ ] and Y [ ] identical . costX is cost of removing a character from X [ ] and costY is cost of removing a character from Y [ ] / ; Find LCS of X [ ] and Y [ ] ; Cost of making two strings identical is SUM of following two 1 ) Cost of removing extra characters from first string 2 ) Cost of removing extra characters from second string ; Driver program to test above function"
How can the above be solved in PHP?

< ? php function lcs ( $ X , $ Y , $ m , $ n ) { $ L = array_fill ( 0 , ( $ m + 1 ) , array_fill ( 0 , ( $ n + 1 ) , NULL ) ) ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) $ L [ $ i ] [ $ j ] = 0 ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) $ L [ $ i ] [ $ j ] = $ L [ $ i - 1 ] [ $ j - 1 ] + 1 ; else $ L [ $ i ] [ $ j ] = max ( $ L [ $ i - 1 ] [ $ j ] , $ L [ $ i ] [ $ j - 1 ] ) ; } } return $ L [ $ m ] [ $ n ] ; } function findMinCost ( & $ X , & $ Y , $ costX , $ costY ) { $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; $ len_LCS = lcs ( $ X , $ Y , $ m , $ n ) ; return $ costX * ( $ m - $ len_LCS ) + $ costY * ( $ n - $ len_LCS ) ; } $ X = " ef " ; $ Y = " gh " ; echo " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings ▁ " . " identical is = " return 0 ; ? >


"Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts | function that calculates the maximum no . of cuts ; Driver Code ; Calling function ."
How can the above be solved in PHP?

< ? php function numberOfCuts ( $ M , $ N ) { $ result = 0 ; $ result = ( $ M - 1 ) * ( $ N - 1 ) ; return $ result ; } $ M = 4 ; $ N = 4 ; $ Cuts = numberOfCuts ( $ M , $ N ) ; echo " Maximum ▁ cuts ▁ = ▁ " , $ Cuts ; ? >


"Minimum De | function to count Dearrangement ; create a copy of original array ; sort the array ; traverse sorted array for counting mismatches ; reverse the sorted array ; traverse reverse sorted array for counting mismatches ; return minimum mismatch count ; Driver Code"
How can the above be solved in PHP?

< ? php function countDe ( $ arr , $ n ) { $ v = $ arr ; sort ( $ arr ) ; $ count1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] != $ v [ $ i ] ) $ count1 ++ ; rsort ( $ arr ) ; $ count2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] != $ v [ $ i ] ) $ count2 ++ ; return ( min ( $ count1 , $ count2 ) ) ; } $ arr = array ( 5 , 9 , 21 , 17 , 13 ) ; $ n = count ( $ arr ) ; echo " Minimum ▁ Dearrangement ▁ = ▁ " . countDe ( $ arr , $ n ) ; ? >


"Minimum Increment / decrement to make array elements equal | Function to return minimum operations need to be make each element of array equal ; Initialize cost to 0 ; Sort the array ; Middle element ; Find Cost ; If n , is even . Take minimum of the Cost obtained by considering both middle elements ; Find cost again ; Take minimum of two cost ; Return total cost ; Driver Code"
How can the above be solved in PHP?

< ? php function minCost ( $ A , $ n ) { $ cost = 0 ; sort ( $ A ) ; $ K = $ A [ $ n / 2 ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ cost += abs ( $ A [ $ i ] - $ K ) ; if ( $ n % 2 == 0 ) { $ tempCost = 0 ; $ K = $ A [ ( $ n / 2 ) - 1 ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ tempCost += abs ( $ A [ $ i ] - $ K ) ; $ cost = min ( $ cost , $ tempCost ) ; } return $ cost ; } $ A = array ( 1 , 6 , 7 , 10 ) ; $ n = sizeof ( $ A ) ; echo minCost ( $ A , $ n ) ; ? >


"Minimum Initial Points to Reach Destination | PHP program to find minimum initial points to reach destination ; dp [ i ] [ j ] represents the minimum initial points player should have so that when starts with cell ( i , j ) successfully reaches the destination cell ( m - 1 , n - 1 ) ; Base case ; Fill last row and last column as base to fill entire table ; fill the table in bottom - up fashion ; Driver Code"
How can the above be solved in PHP?


"Minimum K such that every substring of length atleast K contains a character c | This function checks if there exists some character which appears in all K length substrings ; Iterate over all possible characters ; stores the last occurrence ; set answer as true ; ; No occurrence found of current character in first substring of length K ; Check for every last substring of length K where last occurr - ence exists in substring ; If last occ is not present in substring ; current character is K amazing ; This function performs binary search over the answer to minimise it ; Check if answer is found try to minimise it ; Driver Code"
How can the above be solved in PHP?

 < ? php function check ( $ s , $ K ) { for ( $ ch = 0 ; $ ch < 26 ; $ ch ++ ) { $ c = chr ( ord ( ' a ' ) + $ ch ) ; $ last = -1 ; $ found = true ; for ( $ i = 0 ; $ i < $ K ; $ i ++ ) if ( $ s [ $ i ] == $ c ) $ last = $ i ; if ( $ last == -1 ) continue ; for ( $ i = $ K ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == $ c ) $ last = $ i ; if ( $ last <= ( $ i - $ K ) ) { $ found = false ; break ; } } if ( $ found ) return 1 ; } return 0 ; } function binarySearch ( $ s ) { $ low = 1 ; $ high = strlen ( $ s ) ; while ( $ low <= $ high ) { $ mid = ( $ high + $ low ) >> 1 ; if ( check ( $ s , $ mid ) ) { $ ans = $ mid ; $ high = $ mid - 1 ; } else $ low = $ mid + 1 ; } return $ ans ; } $ s = " abcde " ; echo binarySearch ( $ s ) . "
 " ; $ s = " aaaa " ; echo binarySearch ( $ s ) . "
 " ; ? >


"Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code"
How can the above be solved in PHP?

< ? php function countManipulations ( $ s1 , $ s2 ) { $ count = 0 ; $ char_count = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) $ char_count [ ord ( $ s1 [ $ i ] ) - ord ( ' a ' ) ] += 1 ; for ( $ i = 0 ; $ i < strlen ( $ s2 ) ; $ i ++ ) { $ char_count [ ord ( $ s2 [ $ i ] ) - ord ( ' a ' ) ] -= 1 ; } for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ char_count [ i ] != 0 ) { $ count += abs ( $ char_count [ i ] ) ; } } return $ count ; } $ s1 = " ddcf " ; $ s2 = " cedk " ; echo countManipulations ( $ s1 , $ s2 ) ; ? >


"Minimum Perimeter of n blocks | PHP program to find minimum perimeter using n blocks . ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver code"
How can the above be solved in PHP?

< ? php function minPerimeter ( $ n ) { $ l = floor ( sqrt ( $ n ) ) ; $ sq = $ l * $ l ; if ( $ sq == $ n ) return $ l * 4 ; else { $ row = floor ( $ n / $ l ) ; $ perimeter = 2 * ( $ l + $ row ) ; if ( $ n % $ l != 0 ) $ perimeter += 2 ; return $ perimeter ; } } $ n = 10 ; echo minPerimeter ( $ n ) ; ? >


"Minimum Players required to win the game | function to calculate ( a ^ b ) % ( 10 ^ 9 + 7 ) . ; function to find the minimum required player ; computing the nenomenator ; computing modulo inverse of denominator ; final result ; Driver code"
How can the above be solved in PHP?

< ? php function power ( $ a , $ b ) { $ mod = 1000000007 ; $ res = 1 ; while ( $ b ) { if ( $ b & 1 ) { $ res *= $ a ; $ res %= $ mod ; } $ b /= 2 ; $ a *= $ a ; $ a %= $ mod ; } return $ res ; } function minPlayer ( $ n , $ k ) { $ mod = 1000000007 ; $ num = ( ( power ( $ k , $ n ) - 1 ) + $ mod ) % $ mod ; $ den = ( power ( $ k - 1 , $ mod - 2 ) + $ mod ) % $ mod ; $ ans = ( ( ( $ num * $ den ) % $ mod ) * $ k ) % $ mod ; return $ ans ; } $ n = 3 ; $ k = 3 ; echo minPlayer ( $ n , $ k ) ; ? >


"Minimum Possible value of | ai + aj | function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is abs value is smaller than smallest update smallest and reset count to 1 ; if abs value is equal to smallest increment count value ; print result ; Driver Code"
How can the above be solved in PHP?

 < ? php function pairs ( $ arr , $ n , $ k ) { $ smallest = PHP_INT_MAX ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( abs ( $ arr [ $ i ] + $ arr [ $ j ] - $ k ) < $ smallest ) { $ smallest = abs ( $ arr [ $ i ] + $ arr [ $ j ] - $ k ) ; $ count = 1 ; } else if ( abs ( $ arr [ $ i ] + $ arr [ $ j ] - $ k ) == $ smallest ) $ count ++ ; } echo " Minimal Value = " ▁ , ▁ $ smallest ▁ , ▁ " " ;
 	 	 echo ▁ " Total Pairs = " , ▁ $ count ▁ , ▁ " " } $ arr = array ( 3 , 5 , 7 , 5 , 1 , 9 , 9 ) ; $ k = 12 ; $ n = sizeof ( $ arr ) ; pairs ( $ arr , $ n , $ k ) ; ? >


"Minimum Sum Path in a Triangle | Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; return the top element ; Driver Code"
How can the above be solved in PHP?


"Minimum absolute difference between N and a power of 2 | Function to return the minimum difference between N and a power of 2 ; Power of 2 closest to n on its left ; Power of 2 closest to n on its right ; Return the minimum abs difference ; Driver code"
How can the above be solved in PHP?

< ? php function minAbsDiff ( $ n ) { $ left = 1 << ( ( floor ( log ( $ n ) / log ( 2 ) ) ) ) ; $ right = $ left * 2 ; return min ( ( $ n - $ left ) , ( $ right - $ n ) ) ; } $ n = 15 ; echo minAbsDiff ( $ n ) ; ? >


"Minimum absolute difference of a number and its closest prime | Function to check if a number is prime or not ; Function to find the minimum absolute difference between a number and its closest prime ; Variables to store first prime above and below N ; Finding first prime number greater than N ; Finding first prime number less than N ; Variables to store the differences ; Driver code"
How can the above be solved in PHP?

 < ? php function isPrime ( $ N ) { for ( $ i = 2 ; $ i <= sqrt ( $ N ) ; $ i ++ ) { if ( $ N % $ i == 0 ) return false ; } return true ; } function getDifference ( $ N ) { if ( $ N == 0 ) return 2 ; else if ( $ N == 1 ) return 1 ; else if ( isPrime ( $ N ) ) return 0 ; $ aboveN = -1 ; $ belowN = -1 ; $ n1 = $ N + 1 ; while ( true ) { if ( isPrime ( $ n1 ) ) { $ aboveN = $ n1 ; break ; } else $ n1 ++ ; } $ n1 = $ N - 1 ; while ( true ) { if ( isPrime ( $ n1 ) ) { $ belowN = $ n1 ; break ; } else $ n1 -- ; } $ diff1 = $ aboveN - $ N ; $ diff2 = $ N - $ belowN ; return min ( $ diff1 , $ diff2 ) ; } $ N = 25 ; echo getDifference ( $ N ) . "
 " ;


"Minimum absolute difference of adjacent elements in a circular array | PHP program to find maximum difference between adjacent elements in a circular array . ; Checking normal adjacent elements ; Checking circular link ; Driver Code"
How can the above be solved in PHP?

< ? php function minAdjDifference ( $ arr , $ n ) { if ( $ n < 2 ) return ; $ res = abs ( $ arr [ 1 ] - $ arr [ 0 ] ) ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) $ res = min ( $ res , abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) ) ; $ res = min ( $ res , abs ( $ arr [ $ n - 1 ] - $ arr [ 0 ] ) ) ; echo " Min ▁ Difference ▁ = ▁ " , $ res ; } $ a = array ( 10 , 12 , 13 , 15 , 10 ) ; $ n = count ( $ a ) ; minAdjDifference ( $ a , $ n ) ; ? >


"Minimum adjacent swaps to move maximum and minimum to corners | Function that returns the minimum swaps ; Index of leftmost largest element ; Index of rightmost smallest element ; Driver Code"
How can the above be solved in PHP?


"Minimum and Maximum element of an array which is divisible by a given number k | Function to find the minimum element ; Function to find the maximum element ; Driver code"
How can the above be solved in PHP?

 < ? php function getMin ( $ arr , $ n , $ k ) { $ res = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % $ k == 0 ) $ res = min ( $ res , $ arr [ $ i ] ) ; } return $ res ; } function getMax ( $ arr , $ n , $ k ) { $ res = PHP_INT_MIN ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % $ k == 0 ) $ res = max ( $ res , $ arr [ $ i ] ) ; } return $ res ; } $ arr = array ( 10 , 1230 , 45 , 67 , 1 ) ; $ k = 10 ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ " . " divisible ▁ by ▁ k : ▁ " , getMin ( $ arr , $ n , $ k ) , "
 " ; echo " Maximum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ " . " divisible ▁ by ▁ k : ▁ " , getMax ( $ arr , $ n , $ k ) ; ? >


"Minimum and Maximum number of pairs in m teams of n people | PHP program to find minimum and maximum no . of pairs ; Driver code"
How can the above be solved in PHP?

 < ? php function MinimumMaximumPairs ( $ n , $ m ) { $ max_pairs = ( ( $ n - $ m + 1 ) * ( $ n - $ m ) ) / 2 ; $ min_pairs = $ m * ( int ) ( ( ( ( int ) ( $ n - $ m ) / $ m + 1 ) * ( ( int ) ( $ n - $ m ) / $ m ) ) / 2 ) + ( int ) ceil ( ( $ n - $ m ) / $ m ) * ( ( $ n - $ m ) % $ m ) ; echo ( " Minimum ▁ no . ▁ of ▁ pairs ▁ = ▁ " . " $ min _ pairs " . "
 " ) ; echo ( " Maximum ▁ no . ▁ of ▁ pairs ▁ = ▁ " . " $ max _ pairs " ) ; } $ n = 5 ; $ m = 2 ; MinimumMaximumPairs ( $ n , $ m ) ; ? >


"Minimum and maximum number of N chocolates after distribution among K students | Driver code"
How can the above be solved in PHP?


"Minimum array insertions required to make consecutive difference <= K | Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver Code"
How can the above be solved in PHP?

< ? php function minInsertions ( $ H , $ n , $ K ) { $ inser = 0 ; for ( $ i = 1 ; $ i < $ n ; ++ $ i ) { $ diff = abs ( $ H [ $ i ] - $ H [ $ i - 1 ] ) ; if ( $ diff <= $ K ) continue ; else $ inser += ceil ( $ diff / $ K ) - 1 ; } return $ inser ; } $ H = array ( 2 , 4 , 8 , 16 ) ; $ K = 3 ; $ n = sizeof ( $ H ) ; echo minInsertions ( $ H , $ n , $ K ) ; ? >


"Minimum boxes required to carry all gifts | Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver program"
How can the above be solved in PHP?


"Minimum cells required to reach destination with jumps equal to cell values | function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != INT_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; Driver Code"
How can the above be solved in PHP?


"Minimum changes required such that the string satisfies the given condition | Function to return the minimum changes required ; To store the count of minimum changes , number of ones and the number of zeroes ; First character has to be '1' ; If condition fails changes need to be made ; Return the required count ; Driver code"
How can the above be solved in PHP?

< ? php function minChanges ( $ str , $ n ) { $ count = $ zeros = $ ones = 0 ; if ( $ str [ 0 ] != '1' ) { $ count ++ ; $ ones ++ ; } for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == '0' ) $ zeros ++ ; else $ ones ++ ; if ( $ zeros > $ ones ) { $ zeros -- ; $ ones ++ ; $ count ++ ; } } return $ count ; } $ str = "0000" ; $ n = strlen ( $ str ) ; echo minChanges ( $ str , $ n ) ; ? >


"Minimum changes required to make first string substring of second string | Function to find the minimum number of characters to be replaced in string S2 , such that S1 is a substring of S2 ; Get the sizes of both strings ; Traverse the string S2 ; From every index in S2 , check the number of mis - matching characters in substring of length of S1 ; Take minimum of prev and current mis - match ; return answer ; Driver Code"
How can the above be solved in PHP?

< ? php function minimumChar ( $ S1 , $ S2 ) { $ n = strlen ( $ S1 ) ; $ m = strlen ( $ S2 ) ; $ ans = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ m - $ n + 1 ; $ i ++ ) { $ minRemovedChar = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ S1 [ $ j ] != $ S2 [ $ i + $ j ] ) { $ minRemovedChar ++ ; } } $ ans = min ( $ minRemovedChar , $ ans ) ; } return $ ans ; } $ S1 = " abc " ; $ S2 = " paxzk " ; echo minimumChar ( $ S1 , $ S2 ) ; ? >


"Minimum changes to a string to make all substrings distinct | Returns minimum changes to str so that no substring is repeated . ; If length is more than maximum allowed characters , we cannot get the required string . ; Variable to store count of distinct characters ; To store counts of different characters ; Answer is , n - number of distinct char ; Driver Code"
How can the above be solved in PHP?

< ? php function minChanges ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n > 26 ) return -1 ; $ dist_count = 0 ; $ count = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ count [ ord ( $ str [ $ i ] ) - 97 ] == 0 ) $ dist_count ++ ; $ count [ ord ( $ str [ $ i ] ) - 97 ] ++ ; } return ( $ n - $ dist_count ) ; } $ str = " aebaecedabbee " ; echo minChanges ( $ str ) ; ? >


"Minimum characters to be replaced to remove the given substring | function to calculate minimum characters to replace ; mismatch occurs ; if all characters matched , i . e , there is a substring of ' a ' which is same as string ' b ' ; increment i to index m - 1 such that minimum characters are replaced in ' a ' ; Driver Code"
How can the above be solved in PHP?

< ? php function replace ( $ A , $ B ) { $ n = strlen ( $ A ) ; $ m = strlen ( $ B ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ i + $ j >= $ n ) { break ; } else if ( $ A [ $ i + $ j ] != $ B [ $ j ] ) { break ; } } if ( $ j == $ m ) { $ count ++ ; $ i = $ i + $ m - 1 ; } } return $ count ; } $ str1 = " aaaaaaaa " ; $ str2 = " aaa " ; echo ( replace ( $ str1 , $ str2 ) ) ; ? >


"Minimum cost for acquiring all coins with k extra coins allowed with every coin | Converts coin [ ] to prefix sum array ; sort the coins value ; Maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver Code"
How can the above be solved in PHP?

 < ? php function preprocess ( & $ coin , $ n ) { sort ( $ coin ) ; for ( $ i = 1 ; $ i <= $ n - 1 ; $ i ++ ) $ coin [ $ i ] += $ coin [ $ i - 1 ] ; } function minCost ( & $ coin , $ n , $ k ) { $ coins_needed = ceil ( 1.0 * $ n / ( $ k + 1 ) ) ; return $ coin [ $ coins_needed - 1 ] ; } $ coin = array ( 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ) ; $ n = sizeof ( $ coin ) ; preprocess ( $ coin , $ n ) ; $ k = 3 ; echo minCost ( $ coin , $ n , $ k ) . "
 " ; $ k = 7 ; echo minCost ( $ coin , $ n , $ k ) . "
 " ; ? >


"Minimum cost for acquiring all coins with k extra coins allowed with every coin | function to calculate min cost ; sort the coins value ; calculate no . of coins needed ; calculate sum of all selected coins ; Driver Code"
How can the above be solved in PHP?

< ? php function minCost ( $ coin , $ n , $ k ) { sort ( $ coin ) ; sort ( $ coin , $ n ) ; $ coins_needed = ceil ( 1.0 * $ n / ( $ k + 1 ) ) ; $ ans = 0 ; for ( $ i = 0 ; $ i <= $ coins_needed - 1 ; $ i ++ ) $ ans += $ coin [ $ i ] ; return $ ans ; } { $ coin = array ( 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ) ; $ n = sizeof ( $ coin ) / sizeof ( $ coin [ 0 ] ) ; $ k = 3 ; echo minCost ( $ coin , $ n , $ k ) ; return 0 ; } ? >


"Minimum cost to convert string into palindrome | Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code"
How can the above be solved in PHP?

< ? php function cost ( $ str ) { $ len = strlen ( $ str ) ; $ res = 0 ; for ( $ i = 0 , $ j = $ len - 1 ; $ i < $ j ; $ i ++ , $ j -- ) if ( $ str [ $ i ] != $ str [ $ j ] ) $ res += ( min ( ord ( $ str [ $ i ] ) , ord ( $ str [ $ j ] ) ) - ord ( ' a ' ) + 1 ) ; return $ res ; } $ str = " abcdef " ; echo cost ( $ str ) ; ? >


"Minimum cost to fill given weight in a bag | PHP program to find minimum cost to get exactly W Kg with given packets ; cost [ ] initial cost array including unavailable packet W capacity of bag ; val [ ] and wt [ ] arrays val [ ] array to store cost of ' i ' kg packet of orange wt [ ] array weight of packet of orange ; traverse the original cost [ ] array and skip unavailable packets and make val [ ] and wt [ ] array . size variable tells the available number of distinct weighted packets ; fill 0 th row with infinity ; fill 0 'th column with 0 ; now check for each weight one by one and fill the matrix according to the condition ; wt [ i - 1 ] > j means capacity of bag is less then weight of item ; here we check we get minimum cost either by including it or excluding it ; exactly weight W can not be made by given weights ; Driver Code"
How can the above be solved in PHP?

< ? php $ INF = 1000000 ; function MinimumCost ( & $ cost , $ n , $ W ) { global $ INF ; $ val = array ( ) ; $ wt = array ( ) ; $ size = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ cost [ $ i ] != -1 ) { array_push ( $ val , $ cost [ $ i ] ) ; array_push ( $ wt , $ i + 1 ) ; $ size ++ ; } } $ n = $ size ; $ min_cost = array_fill ( 0 , $ n + 1 , array_fill ( 0 , $ W + 1 , NULL ) ) ; for ( $ i = 0 ; $ i <= $ W ; $ i ++ ) $ min_cost [ 0 ] [ $ i ] = $ INF ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ min_cost [ $ i ] [ 0 ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ W ; $ j ++ ) { if ( $ wt [ $ i - 1 ] > $ j ) $ min_cost [ $ i ] [ $ j ] = $ min_cost [ $ i - 1 ] [ $ j ] ; else $ min_cost [ $ i ] [ $ j ] = min ( $ min_cost [ $ i - 1 ] [ $ j ] , $ min_cost [ $ i ] [ $ j - $ wt [ $ i - 1 ] ] + $ val [ $ i - 1 ] ) ; } } if ( $ min_cost [ $ n ] [ $ W ] == $ INF ) return -1 ; else return $ min_cost [ $ n ] [ $ W ] ; } $ cost = array ( 1 , 2 , 3 , 4 , 5 ) ; $ W = 5 ; $ n = sizeof ( $ cost ) ; echo MinimumCost ( $ cost , $ n , $ W ) ; ? >


"Minimum cost to form a number X by adding up powers of 2 | Function to return the minimum cost ; Re - compute the array ; Add answers for set bits ; If bit is set ; Increase the counter ; Right shift the number ; Driver code"
How can the above be solved in PHP?


"Minimum cost to make array size 1 by removing larger of pairs | function to calculate the minimum cost ; Minimum cost is n - 1 multiplied with minimum element . ; Driver Code"
How can the above be solved in PHP?

< ? php function cost ( $ a , $ n ) { return ( $ n - 1 ) * ( min ( $ a ) ) ; } $ a = array ( 4 , 3 , 2 ) ; $ n = count ( $ a ) ; echo cost ( $ a , $ n ) ; ? >


"Minimum cost to make two strings identical by deleting the digits | Function to returns cost of removing the identical characters in LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains cost of removing identical characters in LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; If both characters are same , add both of them ; Otherwise find the maximum cost among them ; Returns cost of making X [ ] and Y [ ] identical ; Find LCS of X [ ] and Y [ ] ; Initialize the cost variable ; Find cost of all characters in both strings ; Driver code"
How can the above be solved in PHP?


"Minimum cost to modify a string | Function to return the minimum cost ; Initialize result ; To store the frequency of characters of the string Initialize array with 0 ; Update the frequencies of the characters of the string ; Loop to check all windows from a - z where window size is K ; Starting index of window ; Ending index of window ; Check if the string contains character ; Check if the character is on left side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Check if the character is on right side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Find the minimum of all costs for modifying the string ; Loop to check all windows Here window contains characters before z and after z of window size K ; Starting index of window ; Ending index of window ; Check if the string contains character ; If characters are outside window find the cost for modifying character add value to count ; Find the minimum of all costs for modifying the string ; Driver code"
How can the above be solved in PHP?


"Minimum cost to reach end of array array when a maximum jump of K index is allowed | Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code"
How can the above be solved in PHP?

< ? php function FindMinimumCost ( $ ind , $ a , $ n , $ k , $ dp ) { if ( $ ind == ( $ n - 1 ) ) return 0 ; else if ( $ dp [ $ ind ] != -1 ) return $ dp [ $ ind ] ; else { $ ans = PHP_INT_MAX ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { if ( $ ind + $ i < $ n ) $ ans = min ( $ ans , abs ( $ a [ $ ind + $ i ] - $ a [ $ ind ] ) + FindMinimumCost ( $ ind + $ i , $ a , $ n , $ k , $ dp ) ) ; else break ; } return $ dp [ $ ind ] = $ ans ; } } $ a = array ( 10 , 30 , 40 , 50 , 20 ) ; $ k = 3 ; $ n = sizeof ( $ a ) ; $ dp = array ( ) ; $ dp = array_fill ( 0 , $ n , -1 ) ; echo ( FindMinimumCost ( 0 , $ a , $ n , $ k , $ dp ) ) ; ? >


"Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code"
How can the above be solved in PHP?


"Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code"
How can the above be solved in PHP?


"Minimum cost to sort a matrix of numbers from 0 to n ^ 2 | function to find the total energy required to rearrange the numbers ; nested loops to access the elements of the given matrix ; store quotient ; final destination location ( i_des , j_des ) of the element mat [ i ] [ j ] is being calculated ; energy required for the movement of the element mat [ i ] [ j ] is calculated and then accumulated in the ' tot _ energy ' ; required total energy ; Driver Code"
How can the above be solved in PHP?

< ? php function calculateEnergy ( $ mat , $ n ) { $ i_des ; $ j_des ; $ q ; $ tot_energy = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ q = ( int ) ( $ mat [ $ i ] [ $ j ] / $ n ) ; $ i_des = $ q ; $ j_des = $ mat [ $ i ] [ $ j ] - ( $ n * $ q ) ; $ tot_energy += abs ( $ i_des - $ i ) + abs ( $ j_des - $ j ) ; } } return $ tot_energy ; } $ mat = array ( array ( 4 , 7 , 0 , 3 ) , array ( 8 , 5 , 6 , 1 ) , array ( 9 , 11 , 10 , 2 ) , array ( 15 , 13 , 14 , 12 ) ) ; $ n = 4 ; echo " Total ▁ energy ▁ required ▁ = ▁ " , calculateEnergy ( $ mat , $ n ) , " ▁ units " ; ? >


"Minimum difference between adjacent elements of array which contain elements from each row of a matrix | PHP program to find the minimum absolute difference between any of the adjacent elements of an array which is created by picking one element from each row of the matrix . ; Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driver Code"
How can the above be solved in PHP?

 < ? php $ R = 2 ; $ C = 2 ; function bsearch ( $ low , $ high , $ n , $ arr ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ low <= $ high ) { if ( $ arr [ $ mid ] < $ n ) return bsearch ( $ mid + 1 , $ high , $ n , $ arr ) ; return bsearch ( $ low , $ mid - 1 , $ n , $ arr ) ; } return $ low ; } function mindiff ( $ arr , $ n , $ m ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) sort ( $ arr ) ; $ ans = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { $ p = bsearch ( 0 , $ m - 1 , $ arr [ $ i ] [ $ j ] , $ arr [ $ i + 1 ] ) ; $ ans = min ( $ ans , abs ( $ arr [ $ i + 1 ] [ $ p ] - $ arr [ $ i ] [ $ j ] ) ) ; if ( $ p - 1 >= 0 ) $ ans = min ( $ ans , abs ( $ arr [ $ i + 1 ] [ $ p - 1 ] - $ arr [ $ i ] [ $ j ] ) ) ; } } return $ ans ; } $ m = array ( 8 , 5 , 6 , 8 ) ; echo mindiff ( $ m , $ R , $ C ) , "
 " ; ? >


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

< ? php function calculate ( $ a , $ n ) { sort ( $ a ) ; $ s = array ( ) ; for ( $ i = 0 , $ j = $ n - 1 ; $ i < $ j ; $ i ++ , $ j -- ) array_push ( $ s , ( $ a [ $ i ] + $ a [ $ j ] ) ) ; $ mini = min ( $ s ) ; $ maxi = max ( $ s ) ; return abs ( $ maxi - $ mini ) ; } $ a = array ( 2 , 6 , 4 , 3 ) ; $ n = sizeof ( $ a ) ; echo calculate ( $ a , $ n ) ; ? >


"Minimum difference between max and min of all K | returns min difference between max and min of any K - size subset ; sort the array so that close elements come together . ; initialize result by a big integer number ; loop over first ( N - K ) elements of the array only ; get difference between max and min of current K - sized segment ; Driver Code"
How can the above be solved in PHP?

< ? php function minDifferenceAmongMaxMin ( $ arr , $ N , $ K ) { $ INT_MAX = 2 ; sort ( $ arr ) ; sort ( $ arr , $ N ) ; $ res = $ INT_MAX ; for ( $ i = 0 ; $ i <= ( $ N - $ K ) ; $ i ++ ) { $ curSeqDiff = $ arr [ $ i + $ K - 1 ] - $ arr [ $ i ] ; $ res = min ( $ res , $ curSeqDiff ) ; } return $ res ; } $ arr = array ( 10 , 20 , 30 , 100 , 101 , 102 ) ; $ N = sizeof ( $ arr ) ; $ K = 3 ; echo minDifferenceAmongMaxMin ( $ arr , $ N , $ K ) ; ? >


"Minimum digits to remove to make a number Perfect Square | function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code"
How can the above be solved in PHP?


"Minimum distance between two occurrences of maximum | function to return min distance ; case a ; case b ; case c ; Driver Code"
How can the above be solved in PHP?

< ? php function minDistance ( $ arr , $ n ) { $ maximum_element = $ arr [ 0 ] ; $ min_dis = $ n ; $ index = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ maximum_element == $ arr [ $ i ] ) { $ min_dis = min ( $ min_dis , ( $ i - $ index ) ) ; $ index = $ i ; } else if ( $ maximum_element < $ arr [ $ i ] ) { $ maximum_element = $ arr [ $ i ] ; $ min_dis = $ n ; $ index = $ i ; } else continue ; } return $ min_dis ; } $ arr = array ( 6 , 3 , 1 , 3 , 6 , 4 , 6 ) ; $ n = count ( $ arr ) ; echo " Minimum ▁ distance ▁ = ▁ " . minDistance ( $ arr , $ n ) ; ? >


"Minimum element whose n | function to find the minimum element ; loop to traverse and store the sum of log ; computes sum ; calculates the elements according to formula . ; returns the minimal element ; initialised array ; computes the size of array ; prints out the minimal element"
How can the above be solved in PHP?

< ? php function findMin ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += log ( $ a [ $ i ] ) ; $ x = exp ( $ sum / $ n ) ; return ( int ) ( $ x + 1 ) ; } $ a = array ( 3 , 2 , 1 , 4 ) ; $ n = sizeof ( $ a ) ; echo ( findMin ( $ a , $ n ) ) ; ? >


"Minimum elements to be added in a range so that count of elements is divisible by K | PHP implementation of the approach ; Total elements in the range ; If total elements are already divisible by k ; Value that must be added to count in order to make it divisible by k ; Driver Program to test above function"
How can the above be solved in PHP?

< ? php function minimumMoves ( $ k , $ l , $ r ) { $ count = $ r - $ l + 1 ; if ( $ count % $ k == 0 ) return 0 ; return ( $ k - ( $ count % $ k ) ) ; } $ k = 3 ; $ l = 10 ; $ r = 10 ; echo minimumMoves ( $ k , $ l , $ r ) ; ? >


"Minimum elements to be removed such that sum of adjacent elements is always odd | Returns the minimum number of eliminations ; Stores the previous element ; Stores the new value ; Check if the previous and current values are of same parity ; Previous value is now the current value ; Return the counter variable ; Driver code"
How can the above be solved in PHP?

< ? php function min_elimination ( $ n , $ arr ) { $ count = 0 ; $ prev_val = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr_val = $ arr [ $ i ] ; if ( $ curr_val % 2 == $ prev_val % 2 ) $ count ++ ; $ prev_val = $ curr_val ; } return $ count ; } $ arr = array ( 1 , 2 , 3 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; echo min_elimination ( $ n , $ arr ) ; ? >


"Minimum flip required to make Binary Matrix symmetric | PHP Program to find minimum flip required to make ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; finding the transpose of the matrix ; Finding the number of position where element are not same . ; Driver Code"
How can the above be solved in PHP?

 < ? php $ N = 3 ; function minimumflip ( $ mat , $ n ) { global $ N ; $ transpose ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ transpose [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ flip = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ transpose [ $ i ] [ $ j ] != $ mat [ $ i ] [ $ j ] ) $ flip ++ ; return $ flip / 2 ; } $ n = 3 ; $ mat = array ( array ( 0 , 0 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 0 , 0 ) ) ; echo minimumflip ( $ mat , $ n ) , "
 " ; ? >


"Minimum flip required to make Binary Matrix symmetric | PHP Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; Comparing elements across diagonal ; Driver Code"
How can the above be solved in PHP?

 < ? php $ N = 3 ; function minimumflip ( $ mat , $ n ) { $ flip = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != $ mat [ $ j ] [ $ i ] ) $ flip ++ ; return $ flip ; } $ n = 3 ; $ mat = array ( array ( 0 , 0 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 0 , 0 ) ) ; echo minimumflip ( $ mat , $ n ) , "
 " ; ? >


"Minimum flips required to maximize a number with k set bits | function for finding set bit ; return count of set bit ; function for finding min flip ; number of bits in n ; Find the largest number of same size with k set bits ; Count bit differences to find required flipping . ; Driver Code"
How can the above be solved in PHP?

< ? php function setBit ( $ xorValue ) { $ count = 0 ; while ( $ xorValue ) { if ( $ xorValue % 2 ) $ count ++ ; $ xorValue /= 2 ; } return $ count ; } function minFlip ( $ n , $ k ) { $ size = log ( $ n ) + 1 ; $ max = pow ( 2 , $ k ) - 1 ; $ max = $ max << ( $ size - $ k ) ; $ xorValue = ( $ n ^ $ max ) ; return ( setBit ( $ xorValue ) ) ; } $ n = 27 ; $ k = 3 ; echo " Min ▁ Flips ▁ = ▁ " , minFlip ( $ n , $ k ) ; ? >


"Minimum gcd operations to make all array elements one | __gcd function ; Function to count number of moves . ; Counting Number of ones . ; If there is a one ; Find smallest subarray with GCD equals to one . ; to calculate GCD ; Not Possible ; Final answer ; Driver code"
How can the above be solved in PHP?

< ? php function __gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return __gcd ( $ b % $ a , $ a ) ; } function minimumMoves ( $ A , $ N ) { $ one = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( $ A [ $ i ] == 1 ) $ one ++ ; if ( $ one != 0 ) return $ N - $ one ; $ minimum = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ g = $ A [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { $ g = __gcd ( $ A [ $ j ] , $ g ) ; if ( $ g == 1 ) { $ minimum = min ( $ minimum , $ j - $ i ) ; break ; } } } if ( $ minimum == PHP_INT_MAX ) return -1 ; else return $ N + $ minimum - 1 ; } $ A = array ( 2 , 4 , 3 , 9 ) ; $ N = sizeof ( $ A ) ; echo ( minimumMoves ( $ A , $ N ) ) ; ? >


"Minimum in an array which is first decreasing then increasing | Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Driver Code ; Print the smallest number"
How can the above be solved in PHP?

< ? php function minimal ( $ a , $ n ) { $ lo = 0 ; $ hi = $ n - 1 ; while ( $ lo < $ hi ) { $ mid = ( $ lo + $ hi ) >> 1 ; if ( $ a [ $ mid ] < $ a [ $ mid + 1 ] ) { $ hi = $ mid ; } else { $ lo = $ mid + 1 ; } } return $ lo ; } $ a = array ( 8 , 5 , 4 , 3 , 4 , 10 ) ; $ n = sizeof ( $ a ) ; $ ind = minimal ( $ a , $ n ) ; echo $ a [ $ ind ] ; ? >


"Minimum increment by k operations to make all elements equal | function for calculating min operations ; max elements of array ; iterate for all elements ; check if element can make equal to max or not if not then return - 1 ; else update res for required operations ; return result ; Driver Code"
How can the above be solved in PHP?

< ? php function minOps ( $ arr , $ n , $ k ) { $ max = max ( $ arr ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ( $ max - $ arr [ $ i ] ) % $ k != 0 ) return -1 ; else $ res += ( $ max - $ arr [ $ i ] ) / $ k ; } return $ res ; } $ arr = array ( 21 , 33 , 9 , 45 , 63 ) ; $ n = count ( $ arr ) ; $ k = 6 ; print ( minOps ( $ arr , $ n , $ k ) ) ; ? >


"Minimum increment in the sides required to get non | Function to return the minimum increase in side lengths of the triangle ; push the three sides to a array ; sort the array ; check if sum is greater than third side ; Driver Code"
How can the above be solved in PHP?

< ? php function minimumIncrease ( $ a , $ b , $ c ) { $ arr = array ( $ a , $ b , $ c ) ; sort ( $ arr ) ; if ( $ arr [ 0 ] + $ arr [ 1 ] >= $ arr [ 2 ] ) return 0 ; else return $ arr [ 2 ] - ( $ arr [ 0 ] + $ arr [ 1 ] ) ; } $ a = 3 ; $ b = 5 ; $ c = 10 ; echo minimumIncrease ( $ a , $ b , $ c ) ; ? >


"Minimum increment operations to make the array in increasing order | function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code"
How can the above be solved in PHP?


"Minimum index i such that all the elements from index i to given index are equal | Function to return the minimum required index ; Start from arr [ pos - 1 ] ; All elements are equal from arr [ i + 1 ] to arr [ pos ] ; Driver code ; Function Call"
How can the above be solved in PHP?

< ? php function minIndex ( $ arr , $ n , $ pos ) { $ num = $ arr [ $ pos ] ; $ i = $ pos - 1 ; while ( $ i >= 0 ) { if ( $ arr [ $ i ] != $ num ) break ; $ i -- ; } return $ i + 1 ; } $ arr = array ( 2 , 1 , 1 , 1 , 5 , 2 ) ; $ n = sizeof ( $ arr ) ; $ pos = 4 ; echo minIndex ( $ arr , $ n , $ pos ) ; ? >


"Minimum insertions to form a palindrome with permutations allowed | Function will return number of characters to be added ; To store string length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if string is already palindrome return 0 ; Driver program"
How can the above be solved in PHP?

< ? php function minInsertion ( $ str ) { $ n = strlen ( $ str ) ; $ res = 0 ; $ count = array ( 26 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ count [ $ i ] % 2 == 1 ) $ res ++ ; } return ( $ res == 0 ) ? 0 : $ res - 1 ; } $ str = " geeksforgeeks " ; echo ( minInsertion ( $ str ) ) ; ? >


"Minimum insertions to sort an array | method returns min steps of insertion we need to perform to sort array ' arr ' ; lis [ i ] is going to store length of lis that ends with i . ; Initialize lis values for all indexes ; Compute optimized lis values in bottom up manner ; The overall LIS must end with of the array elements . Pick maximum of all lis values ; return size of array minus length of LIS as final result ; Driver code"
How can the above be solved in PHP?

< ? php function minInsertionStepToSortArray ( $ arr , $ N ) { $ lis [ $ N ] = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ lis [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] >= $ arr [ $ j ] && $ lis [ $ i ] < $ lis [ $ j ] + 1 ) $ lis [ $ i ] = $ lis [ $ j ] + 1 ; $ max = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( $ max < $ lis [ $ i ] ) $ max = $ lis [ $ i ] ; return ( $ N - $ max ) ; } $ arr = array ( 2 , 3 , 5 , 1 , 4 , 7 , 6 ) ; $ N = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo minInsertionStepToSortArray ( $ arr , $ N ) ; ? >


"Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code"
How can the above be solved in PHP?

< ? php function getMinNum ( $ N ) { $ lcm = 1 ; for ( $ i = 2 ; $ i <= $ N ; $ i ++ ) $ lcm = ( ( $ i * $ lcm ) / ( __gcd ( $ i , $ lcm ) ) ) ; return ( $ lcm + 1 ) ; } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ N = 5 ; echo ( getMinNum ( $ N ) ) ; ? >


"Minimum jumps to reach last building in a matrix | Recursive PHP program to find minimum jumps to reach last building from first . ; Returns minimum jump path from ( 0 , 0 ) to ( m , n ) in height [ R ] [ C ] ; base case ; Find minimum jumps if we go through diagonal ; Find minimum jumps if we go through down ; Find minimum jumps if we go through right ; return minimum jumps ; Driver Code"
How can the above be solved in PHP?

< ? php $ R = 4 ; $ C = 3 ; function isSafe ( $ x , $ y ) { global $ R , $ C ; return ( $ x < $ R and $ y < $ C ) ; } function minJump ( $ height , $ x , $ y ) { global $ R , $ C ; if ( $ x == $ R - 1 and $ y == $ C - 1 ) return 0 ; $ diag = PHP_INT_MAX ; if ( isSafe ( $ x + 1 , $ y + 1 ) ) $ diag = minJump ( $ height , $ x + 1 , $ y + 1 ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x + 1 ] [ $ y + 1 ] ) ; $ down = PHP_INT_MAX ; if ( isSafe ( $ x + 1 , $ y ) ) $ down = minJump ( $ height , $ x + 1 , $ y ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x + 1 ] [ $ y ] ) ; $ right = PHP_INT_MAX ; if ( isSafe ( $ x , $ y + 1 ) ) $ right = minJump ( $ height , $ x , $ y + 1 ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x ] [ $ y + 1 ] ) ; return min ( $ down , min ( $ right , $ diag ) ) ; } $ height = array ( array ( 5 , 4 , 2 ) , array ( 9 , 2 , 1 ) , array ( 2 , 5 , 9 ) , array ( 1 , 3 , 11 ) ) ; echo minJump ( $ height , 0 , 0 ) ; ? >


"Minimum length of square to contain at least half of the given Coordinates | Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code"
How can the above be solved in PHP?

 < ? php function mod ( $ x ) { if ( $ x >= 0 ) return $ x ; return - $ x ; } function findSquare ( $ n ) { $ points = array ( array ( 1 , 2 ) , array ( -3 , 4 ) , array ( 1 , 78 ) , array ( -3 , -7 ) ) ; $ a [ $ n ] = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x ; $ y ; $ x = $ points [ $ i ] [ 0 ] ; $ y = $ points [ $ i ] [ 1 ] ; $ a [ $ i ] = max ( mod ( $ x ) , mod ( $ y ) ) ; } sort ( $ a ) ; $ index = floor ( $ n / 2 ) - 1 ; echo " Minimum ▁ M ▁ required ▁ is : ▁ " , $ a [ $ index ] , "
 " ; } $ N = 4 ; findSquare ( $ N ) ; ? >


"Minimum matches the team needs to win to qualify | Function to return the minimum number of matches to win to qualify for next round ; Do a binary search to find ; Find mid element ; Check for condition$ to qualify for next round ; Driver Code"
How can the above be solved in PHP?

< ? php function findMinimum ( $ x , $ y ) { $ low = 0 ; $ high = $ y ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) >> 1 ; if ( ( $ mid * 2 + ( $ y - $ mid ) ) >= $ x ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return $ low ; } $ x = 6 ; $ y = 5 ; echo findMinimum ( $ x , $ y ) ; ? >


"Minimum moves to reach from i to j in a cyclic string | Function to return the count of steps required to move from i to j ; Starting from i + 1 ; Count of steps ; Current character ; If current character is different from previous ; Increment steps ; Update current character ; Return total steps ; Function to return the minimum number of steps required to reach j from i ; Swap the values so that i <= j ; Steps to go from i to j ( left to right ) ; While going from i to j ( right to left ) First go from i to 0 then from ( n - 1 ) to j ; If first and last character is different then it 'll add a step to stepsToLeft  ; Return the minimum of two paths ; Driver code"
How can the above be solved in PHP?

< ? php function getSteps ( $ str , $ i , $ j , $ n ) { $ k = $ i + 1 ; $ steps = 0 ; $ ch = $ str [ $ i ] ; while ( $ k <= $ j ) { if ( $ str [ $ k ] != $ ch ) { $ steps ++ ; $ ch = $ str [ $ k ] ; } $ k ++ ; } return $ steps ; } function getMinSteps ( $ str , $ i , $ j , $ n ) { if ( $ j < $ i ) { $ temp = $ i ; $ i = $ j ; $ j = $ temp ; } $ stepsToRight = getSteps ( $ str , $ i , $ j , $ n ) ; $ stepsToLeft = getSteps ( $ str , 0 , $ i , $ n ) + getSteps ( $ str , $ j , $ n - 1 , $ n ) ; if ( $ str [ 0 ] != $ str [ $ n - 1 ] ) $ stepsToLeft ++ ; return min ( $ stepsToLeft , $ stepsToRight ) ; } $ str = " SSNSS " ; $ n = strlen ( $ str ) ; $ i = 0 ; $ j = 3 ; echo getMinSteps ( $ str , $ i , $ j , $ n ) ; ? >


"Minimum moves to reach target on a infinite line | Set 2 | Function to find minimum steps to reach target ; Handling negatives$ by symmetry$ ; Keep moving while sum is smaller i . e calculating n ; case 1 : d is even ; d is odd ; Driver code ; Function call"
How can the above be solved in PHP?

< ? php function StepstoReachTarget ( $ target ) { $ target = abs ( $ target ) ; $ n = ceil ( ( -1.0 + sqrt ( 1 + 8.0 * $ target ) ) / 2 ) ; $ sum = $ n * ( $ n + 1 ) / 2 ; if ( $ sum == $ target ) return $ n ; $ d = $ sum - $ target ; if ( ( $ d & 1 ) == 0 ) return n ; else return $ n + ( ( $ n & 1 ) ? 2 : 1 ) ; } $ target = 5 ; echo StepstoReachTarget ( $ target ) ; ? >


"Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code"
How can the above be solved in PHP?

< ? php function Divisors ( $ x ) { $ c = 0 ; $ v = array ( ) ; while ( $ x % 2 == 0 ) { $ c ++ ; $ x = floor ( $ x / 2 ) ; } array_push ( $ v , $ c ) ; $ c = 0 ; while ( $ x % 3 == 0 ) { $ c ++ ; $ x = floor ( $ x / 3 ) ; } array_push ( $ v , $ c ) ; $ c = 0 ; while ( $ x % 7 == 0 ) { $ c ++ ; $ x = floor ( $ x / 7 ) ; } array_push ( $ v , $ c ) ; array_push ( $ v , $ x ) ; return $ v ; } function MinOperations ( $ a , $ b ) { $ va = Divisors ( $ a ) ; $ vb = Divisors ( $ b ) ; if ( $ va [ 3 ] != $ vb [ 3 ] ) return -1 ; $ minOperations = abs ( $ va [ 0 ] - $ vb [ 0 ] ) + abs ( $ va [ 1 ] - $ vb [ 1 ] ) + abs ( $ va [ 2 ] - $ vb [ 2 ] ) ; return $ minOperations ; } $ a = 14 ; $ b = 28 ; echo MinOperations ( $ a , $ b ) ; ? >


"Minimum number N such that total set bits of all numbers from 1 to N is at | Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code"
How can the above be solved in PHP?

< ? php function getSetBitsFromOneToN ( $ N ) { $ two = 2 ; $ ans = 0 ; $ n = $ N ; while ( $ n ) { $ ans += ( int ) ( $ N / $ two ) * ( $ two >> 1 ) ; if ( ( $ N & ( $ two - 1 ) ) > ( $ two >> 1 ) - 1 ) $ ans += ( $ N & ( $ two - 1 ) ) - ( $ two >> 1 ) + 1 ; $ two <<= 1 ; $ n >>= 1 ; } return $ ans ; } function findMinimum ( $ x ) { $ low = 0 ; $ high = 100000 ; $ ans = $ high ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) >> 1 ; if ( getSetBitsFromOneToN ( $ mid ) >= $ x ) { $ ans = min ( $ ans , $ mid ) ; $ high = $ mid - 1 ; } else $ low = $ mid + 1 ; } return $ ans ; } $ x = 20 ; echo findMinimum ( $ x ) ; ? >


"Minimum number greater than the maximum of array which cannot be formed using the numbers in the array | Function that returns the minimum number greater than maximum of the array that cannot be formed using the elements of the array ; Sort the given array ; Maximum number in the array ; table [ i ] will store the minimum number of elements from the array to form i ; Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * max ) ; If there exists a number greater than the maximum element of the array that can be formed using the numbers of array ; Driver code"
How can the above be solved in PHP?

< ? php function findNumber ( $ arr , $ n ) { sort ( $ arr ) ; $ max = $ arr [ $ n - 1 ] ; $ table = array ( ( 2 * $ max ) + 1 ) ; $ table [ 0 ] = 0 ; for ( $ i = 1 ; $ i < ( 2 * $ max ) + 1 ; $ i ++ ) $ table [ $ i ] = PHP_INT_MAX ; $ ans = -1 ; for ( $ i = 1 ; $ i < ( 2 * $ max ) + 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] <= $ i ) { $ res = $ table [ $ i - $ arr [ $ j ] ] ; if ( $ res != PHP_INT_MAX && $ res + 1 < $ table [ $ i ] ) $ table [ $ i ] = $ res + 1 ; } } if ( $ i > $ arr [ $ n - 1 ] && $ table [ $ i ] == PHP_INT_MAX ) { $ ans = $ i ; break ; } } return $ ans ; } { $ arr = array ( 6 , 7 , 15 ) ; $ n = sizeof ( $ arr ) ; echo ( findNumber ( $ arr , $ n ) ) ; }


"Minimum number of 1 's to be replaced in a binary array | Function to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; return final answer ; Driver Code"
How can the above be solved in PHP?


"Minimum number of Parentheses to be added to make it valid | Function to return required minimum number ; maintain balance of string ; It is guaranteed bal >= - 1 ; Driver code ; Function to print required answer"
How can the above be solved in PHP?

< ? php function minParentheses ( $ p ) { $ bal = 0 ; $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ p ) ; ++ $ i ) { if ( $ p [ $ i ] == ' ( ' ) $ bal += 1 ; else $ bal += -1 ; if ( $ bal == -1 ) { $ ans += 1 ; $ bal += 1 ; } } return $ bal + $ ans ; } $ p = " ( ) ) " ; echo minParentheses ( $ p ) ; ? >


"Minimum number of bombs | function to print where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; Driver Code"
How can the above be solved in PHP?


"Minimum number of changes such that elements are first Negative and then Positive | Function to return the count of minimum operations required ; To store the count of negative integers on the right of the current index ( inclusive ) ; Find the count of negative integers on the right ; If current element is negative ; To store the count of positive elements ; Find the positive integers on the left ; If current element is positive ; Update the answer ; Return the required answer ; Driver code"
How can the above be solved in PHP?


"Minimum number of characters to be removed to make a binary string alternate | Returns count of minimum characters to be removed to make s alternate . ; if two alternating characters of string are same ; then need to delete a character ; Driver code"
How can the above be solved in PHP?

< ? php function countToMake0lternate ( $ s ) { $ result = 0 ; for ( $ i = 0 ; $ i < ( strlen ( $ s ) - 1 ) ; $ i ++ ) if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ result ++ ; return $ result ; } echo countToMake0lternate ( " 000111 " ) , " " ; echo countToMake0lternate ( " 11111 " ) , " " ; echo countToMake0lternate ( " 01010101 " ) ; ? >


"Minimum number of consecutive sequences that can be formed in an array | PHP program find the minimum number of consecutive sequences in an array ; Driver Code ; function call to print required answer"
How can the above be solved in PHP?

< ? php function countSequences ( $ arr , $ n ) { $ count = 1 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] + 1 != $ arr [ $ i + 1 ] ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 7 , 3 , 5 , 10 ) ; $ n = count ( $ arr ) ; echo countSequences ( $ arr , $ n ) ; ? >


"Minimum number of cubes whose sum equals to given number N | Function to return the minimum number of cubes whose sum is k ; If k is less than the 2 ^ 3 ; Initialize with the maximum number of cubes required ; Driver code"
How can the above be solved in PHP?

< ? php function MinOfCubed ( $ k ) { if ( $ k < 8 ) return $ k ; $ res = $ k ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { if ( ( $ i * $ i * $ i ) > $ k ) return $ res ; $ res = min ( $ res , MinOfCubed ( $ k - ( $ i * $ i * $ i ) ) + 1 ) ; } return $ res ; } $ num = 15 ; echo MinOfCubed ( $ num ) ; ? >


"Minimum number of cubes whose sum equals to given number N | Function to return the minimum number of cubes whose sum is k ; While current perfect cube is less than current element ; If i is a perfect cube ; i = ( i - 1 ) + 1 ^ 3 ; Next perfect cube ; Re - initialization for next element ; Driver code"
How can the above be solved in PHP?


"Minimum number of cuts required to make circle segments equal sized | Recursive function to return gcd of two nos ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver Code"
How can the above be solved in PHP?

< ? php function findgcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return findgcd ( $ b , $ a % $ b ) ; } function minimumCuts ( $ a , $ n ) { sort ( $ a ) ; $ gcd = $ a [ 1 ] - $ a [ 0 ] ; $ s = $ gcd ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ gcd = findgcd ( $ gcd , $ a [ $ i ] - $ a [ $ i - 1 ] ) ; $ s += $ a [ $ i ] - $ a [ $ i - 1 ] ; } if ( 360 - $ s > 0 ) $ gcd = findgcd ( $ gcd , 360 - $ s ) ; return ( 360 / $ gcd ) - $ n ; } $ arr = array ( 30 , 60 , 180 ) ; $ n = sizeof ( $ arr ) ; echo ( minimumCuts ( $ arr , $ n ) ) ; ? >


"Minimum number of deletions so that no two consecutive are same | Function for counting deletions ; If two consecutive characters are the same , delete one of them . ; Driver Code ; Function call to print answer"
How can the above be solved in PHP?

< ? php function countDeletions ( $ str ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) - 1 ; $ i ++ ) if ( $ str [ $ i ] == $ str [ $ i + 1 ] ) $ ans ++ ; return $ ans ; } $ str = " AAABBB " ; echo countDeletions ( $ str ) ; ? >


"Minimum number of deletions to make a sorted sequence | lis ( ) returns the length of the longest increasing subsequence in arr [ ] of size n ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Pick resultimum of all LIS values ; function to calculate minimum number of deletions ; Find longest increasing subsequence ; After removing elements other than the lis , we get sorted sequence . ; Driver Code"
How can the above be solved in PHP?

< ? php function lis ( $ arr , $ n ) { $ result = 0 ; $ lis [ $ n ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ lis [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ lis [ $ i ] < $ lis [ $ j ] + 1 ) $ lis [ $ i ] = $ lis [ $ j ] + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ result < $ lis [ $ i ] ) $ result = $ lis [ $ i ] ; return $ result ; } function minimumNumberOfDeletions ( $ arr , $ n ) { $ len = lis ( $ arr , $ n ) ; return ( $ n - $ len ) ; } $ arr = array ( 30 , 40 , 2 , 5 , 1 , 7 , 45 , 50 , 8 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Minimum ▁ number ▁ of ▁ deletions ▁ = ▁ " , minimumNumberOfDeletions ( $ arr , $ n ) ; ? >


"Minimum number of deletions to make a string palindrome | Returns the length of the longest palindromic subsequence in ' str ' ; Create a table to store results of subproblems ; Strings of length 1 are palindrome of length 1 ; Build the table . Note that the lower diagonal values of table are useless and not filled in the process . c1 is length of substring ; length of longest palindromic subseq ; function to calculate minimum number of deletions ; Find longest palindromic subsequence ; After removing characters other than the lps , we get palindrome . ; Driver Code"
How can the above be solved in PHP?

< ? php function lps ( $ str ) { $ n = strlen ( $ str ) ; $ L ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ L [ $ i ] [ $ i ] = 1 ; for ( $ cl = 2 ; $ cl <= $ n ; $ cl ++ ) { for ( $ i = 0 ; $ i < $ n - $ cl + 1 ; $ i ++ ) { $ j = $ i + $ cl - 1 ; if ( $ str [ $ i ] == $ str [ $ j ] && $ cl == 2 ) $ L [ $ i ] [ $ j ] = 2 ; else if ( $ str [ $ i ] == $ str [ $ j ] ) $ L [ $ i ] [ $ j ] = $ L [ $ i + 1 ] [ $ j - 1 ] + 2 ; else $ L [ $ i ] [ $ j ] = max ( $ L [ $ i ] [ $ j - 1 ] , $ L [ $ i + 1 ] [ $ j ] ) ; } } return $ L [ 0 ] [ $ n - 1 ] ; } function minimumNumberOfDeletions ( $ str ) { $ n = strlen ( $ str ) ; $ len = lps ( $ str ) ; return ( $ n - $ len ) ; } { $ str = " geeksforgeeks " ; echo " Minimum ▁ number ▁ of ▁ deletions ▁ = ▁ " , minimumNumberOfDeletions ( $ str ) ; return 0 ; } ? >


"Minimum number of elements to add to make median equals x | PHP program to find minimum number of elements to add so that its median equals x . ; no . of elements equals to x , that is , e . ; no . of elements greater than x , that is , h . ; no . of elements smaller than x , that is , l . ; subtract the no . of elements that are equal to x . ; Driver code"
How can the above be solved in PHP?


"Minimum number of elements to add to make median equals x | Returns count of elements to be added to make median x . This function assumes that a [ ] has enough extra space . ; to sort the array in increasing order . ; Driver code"
How can the above be solved in PHP?


"Minimum number of elements to be removed so that pairwise consecutive elements are same | Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code"
How can the above be solved in PHP?

< ? php function countConsecutive ( $ s ) { $ count = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ i ++ ; else $ count ++ ; } return $ count ; } $ str = "44522255" ; echo countConsecutive ( $ str ) ; ? >


"Minimum number of elements to be removed to make XOR maximum | PHP implementation to find minimum number of elements to remove to get maximum XOR value ; First n in the below condition is for the case where n is 0 ; Function to find minimum number of elements to be removed . ; Driver code ; print minimum number of elements to be removed"
How can the above be solved in PHP?

< ? php function nextPowerOf2 ( $ n ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ; while ( $ n != 0 ) { $ n >>= 1 ; $ count += 1 ; } return 1 << $ count ; } function removeElement ( $ n ) { if ( $ n == 1 $ n == 2 ) return 0 ; $ a = nextPowerOf2 ( $ n ) ; if ( $ n == $ a $ n == $ a - 1 ) return 1 ; else if ( $ n == $ a - 2 ) return 0 ; else if ( $ n % 2 == 0 ) return 1 ; else return 2 ; } $ n = 5 ; echo removeElement ( $ n ) ; ? >


"Minimum number of given moves required to make N divisible by 25 | Function to return the minimum number of moves required to make n divisible by 25 ; Convert number into string ; To store required answer ; Length of the string ; To check all possible pairs ; Make a duplicate string ; Number of swaps required to place ith digit in last position ; Number of swaps required to place jth digit in 2 nd last position ; Find first non zero digit ; Place first non zero digit in the first position ; Convert string to number ; If this number is divisible by 25 then cur is one of the possible answer ; If not possible ; Driver code"
How can the above be solved in PHP?


"Minimum number of given operation required to convert n to m | Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code"
How can the above be solved in PHP?

< ? php function minOperations ( $ n , $ m ) { if ( $ m % $ n != 0 ) return -1 ; $ minOperations = 0 ; $ q = $ m / $ n ; while ( $ q % 2 == 0 ) { $ q = $ q / 2 ; $ minOperations ++ ; } while ( $ q % 3 == 0 ) { $ q = $ q / 3 ; $ minOperations ++ ; } if ( $ q == 1 ) return $ minOperations ; return -1 ; } $ n = 120 ; $ m = 51840 ; echo ( minOperations ( $ n , $ m ) ) ; ? >


"Minimum number of increment / decrement operations such that array contains all elements from 1 to N | Function to find the minimum operations ; Sort the given array ; Count operations by assigning a [ i ] = i + 1 ; Driver Code"
How can the above be solved in PHP?

< ? php function minimumMoves ( $ a , $ n ) { $ operations = 0 ; sort ( $ a ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ operations += abs ( $ a [ $ i ] - ( $ i + 1 ) ) ; return $ operations ; } $ arr = array ( 5 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; echo minimumMoves ( $ arr , $ n ) ; ? >


"Minimum number of jumps to reach end | Returns Minimum number of jumps to reach end ; jumps [ 0 ] will hold the result ; Minimum number of jumps needed to reach last element from last elements itself is always 0 ; Start from the second element , move from right to left and construct the jumps [ ] array where jumps [ i ] represents minimum number of jumps needed to reach arr [ m - 1 ] from arr [ i ] ; If arr [ i ] is 0 then arr [ n - 1 ] can 't be reached from here ; If we can direcly reach to the end point from here then jumps [ i ] is 1 ; Otherwise , to find out the minimum number of jumps needed to reach arr [ n - 1 ] , check all the points reachable from here and jumps [ ] value for those points ; initialize min value ; following loop checks with all reachable points and takes the minimum ; Handle overflow ; or INT_MAX ; Driver Code"
How can the above be solved in PHP?

< ? php function minJumps ( $ arr , $ n ) { $ jumps [ $ n ] = array ( ) ; $ min ; $ jumps [ $ n - 1 ] = array ( 0 ) ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] == 0 ) $ jumps [ $ i ] = PHP_INT_MAX ; else if ( $ arr [ $ i ] >= ( $ n - $ i ) - 1 ) $ jumps [ $ i ] = 1 ; else { $ min = PHP_INT_MAX ; for ( $ j = $ i + 1 ; $ j < $ n && $ j <= $ arr [ $ i ] + $ i ; $ j ++ ) { if ( $ min > $ jumps [ $ j ] ) $ min = $ jumps [ $ j ] ; } if ( $ min != PHP_INT_MAX ) $ jumps [ $ i ] = $ min + 1 ; else $ jumps [ $ i ] = $ min ; } } return $ jumps [ 0 ] ; } $ arr = array ( 1 , 3 , 6 , 1 , 0 , 9 ) ; $ size = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach " , " ▁ end ▁ is ▁ " , minJumps ( $ arr , $ size ) ; ? >


"Minimum number of jumps to reach end | Returns minimum number of jumps to reach arr [ h ] from arr [ l ] ; Base case : when source and destination are same ; When nothing is reachable from the given source ; Traverse through all the points reachable from arr [ l ] . Recursively get the minimum number of jumps needed to reach arr [ h ] from these reachable points . ; Driver program to test above function"
How can the above be solved in PHP?

< ? php function minJumps ( $ arr , $ l , $ h ) { if ( $ h == $ l ) return 0 ; if ( $ arr [ $ l ] == 0 ) return INT_MAX ; $ min = 999999 ; for ( $ i = $ l + 1 ; $ i <= $ h && $ i <= $ l + $ arr [ $ l ] ; $ i ++ ) { $ jumps = minJumps ( $ arr , $ i , $ h ) ; if ( $ jumps != 999999 && $ jumps + 1 < $ min ) $ min = $ jumps + 1 ; } return $ min ; } $ arr = array ( 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 ) ; $ n = count ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach ▁ " . " end ▁ is ▁ " . minJumps ( $ arr , 0 , $ n - 1 ) ; ? >


"Minimum number of letters needed to make a total of n | Function to return the minimum letters required to make a total of n ; Driver code"
How can the above be solved in PHP?

< ? php function minLettersNeeded ( $ n ) { if ( $ n % 26 == 0 ) return floor ( ( $ n / 26 ) ) ; else return floor ( ( $ n / 26 ) + 1 ) ; } $ n = 52 ; echo minLettersNeeded ( $ n ) ; ? >


"Minimum number of mails required to distribute all the questions | Function returns the min no of mails required ; Using the formula derived above ; no of questions ; no of students ; maximum no of questions a mail can hold ; Calling function"
How can the above be solved in PHP?


"Minimum number of moves required to reach the destination by the king in a chess board | function to Find the minimum number of moves required to reach the destination by the king in a chess board ; minimum number of steps ; while the king is not in the same row or column as the destination ; Go up ; Go down ; Go left ; Go right ; Driver code"
How can the above be solved in PHP?

 < ? php function MinSteps ( $ SourceX , $ SourceY , $ DestX , $ DestY ) { echo max ( abs ( $ SourceX - $ DestX ) , abs ( $ SourceY - $ DestY ) ) . " " ; while ( ( $ SourceX != $ DestX ) || ( $ SourceY != $ DestY ) ) { if ( $ SourceX < $ DestX ) { echo ' U ' ; $ SourceX ++ ; } if ( $ SourceX > $ DestX ) { echo ' D ' ; $ SourceX -- ; } if ( $ SourceY > $ DestY ) { echo ' L ' ; $ SourceY -- ; } if ( $ SourceY < $ DestY ) { echo ' R ' ; $ SourceY ++ ; } echo "
 " ; } } $ sourceX = 4 ; $ sourceY = 4 ; $ destinationX = 7 ; $ destinationY = 0 ; MinSteps ( $ sourceX , $ sourceY , $ destinationX , $ destinationY ) ; ? >


"Minimum number of operations required to delete all elements of the array | function to find minimum operations ; sort array ; prepare hash of array ; Driver Code"
How can the above be solved in PHP?

< ? php function minOperations ( & $ arr , $ n ) { $ hashTable = array ( ) ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hashTable [ $ arr [ $ i ] ] ++ ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ hashTable [ $ arr [ $ i ] ] ) { for ( $ j = $ i ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] % $ arr [ $ i ] == 0 ) $ hashTable [ $ arr [ $ j ] ] = 0 ; $ res ++ ; } } return $ res ; } $ arr = array ( 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ) ; $ n = sizeof ( $ arr ) ; echo minOperations ( $ arr , $ n ) ; ? >


"Minimum number of operations required to reduce N to 1 | Function that returns the minimum number of operations to be performed to reduce the number to 1 ; To stores the total number of operations to be performed ; if n is divisible by 3 then reduce it to n / 3 ; if n modulo 3 is 1 decrement it by 1 ; if n modulo 3 is 2 then increment it by 1 ; update the counter ; Driver code"
How can the above be solved in PHP?

 < ? php function count_minimum_operations ( $ n ) { $ count = 0 ; while ( $ n > 1 ) { if ( $ n % 3 == 0 ) $ n /= 3 ; else if ( $ n % 3 == 1 ) $ n -- ; else { if ( $ n == 2 ) $ n -- ; else $ n ++ ; } $ count ++ ; } return $ count ; } $ n = 4 ; $ ans = count_minimum_operations ( $ n ) ; echo $ ans , "
 " ; ? >


"Minimum number of operations required to sum to binary string S | Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 ] [ 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i ] [ 1 ] ; Transition from dp [ i - 1 ] [ 0 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 ] [ 1 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of adding 2 ^ i ; Driver Code"
How can the above be solved in PHP?


"Minimum number of power terms with sum equal to n | Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's added ; Repeat the process for reduced n ; Driver Code"
How can the above be solved in PHP?

< ? php function minPower ( $ n , $ x ) { if ( $ x == 1 ) return $ n ; $ ans = 0 ; while ( $ n > 0 ) { $ ans += ( $ n % $ x ) ; $ n /= $ x ; } return $ ans ; } $ n = 5 ; $ x = 3 ; echo ( minPower ( $ n , $ x ) ) ; ? >


"Minimum number of replacements to make the binary string alternating | Set 2 | Function to return the minimum number of characters of the given binary string to be replaced to make the string alternating ; If there is 1 at even index positions ; If there is 0 at odd index positions ; Driver code"
How can the above be solved in PHP?

< ? php function minReplacement ( $ s , $ len ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ i % 2 == 0 && $ s [ $ i ] == '1' ) $ ans ++ ; if ( $ i % 2 == 1 && $ s [ $ i ] == '0' ) $ ans ++ ; } return min ( $ ans , $ len - $ ans ) ; } $ s = "1100" ; $ len = strlen ( $ s ) ; echo minReplacement ( $ s , $ len ) ; ? >


"Minimum number of sets with numbers less than Y | Function to find the minimum number of shets ; Variable to count the number of sets ; Iterate in the string ; Add the number to string ; Mark that we got a number ; else Every time it exceeds ; Check if previous was anytime less than Y ; Current number ; Check for current number ; Check for last added number ; Driver Code"
How can the above be solved in PHP?


"Minimum number of single digit primes required whose sum is equal to N | function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number N . ; Not possible ; Driver Code"
How can the above be solved in PHP?

< ? php function check ( $ i , $ val ) { if ( $ i - $ val < 0 ) return false ; return true ; } function MinimumPrimes ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = 1e9 ; $ dp [ 0 ] = $ dp [ 2 ] = $ dp [ 3 ] = $ dp [ 5 ] = $ dp [ 7 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( check ( $ i , 2 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 2 ] ) ; if ( check ( $ i , 3 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 3 ] ) ; if ( check ( $ i , 5 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 5 ] ) ; if ( check ( $ i , 7 ) ) $ dp [ $ i ] = min ( $ dp [ $ i ] , 1 + $ dp [ $ i - 7 ] ) ; } if ( $ dp [ $ n ] == ( 1e9 ) ) return -1 ; else return $ dp [ $ n ] ; } $ n = 12 ; $ minimal = MinimumPrimes ( $ n ) ; if ( $ minimal != -1 ) { echo ( " Minimum ▁ number ▁ of ▁ single ▁ " . " digit ▁ primes ▁ required ▁ : " ) ; echo ( $ minimal ) ; } else { echo ( " Not ▁ possible " ) ; } ? >


"Minimum number of square tiles required to fill the rectangular floor | Function to find the number of tiles ; if breadth is divisible by side of square ; tiles required is N / s ; one more tile required ; if length is divisible by side of square ; tiles required is M / s ; one more tile required ; input length and breadth of rectangle and side of square"
How can the above be solved in PHP?


"Minimum number of square tiles required to fill the rectangular floor | Function to find the number of tiles ; no of tiles ; input length and breadth of rectangle and side of square"
How can the above be solved in PHP?

< ? php function solve ( $ M , $ N , $ s ) { $ ans = ( ( int ) ( ceil ( $ M / $ s ) ) * ( int ) ( ceil ( $ N / $ s ) ) ) ; return $ ans ; } $ N = 12 ; $ M = 13 ; $ s = 4 ; echo solve ( $ M , $ N , $ s ) ; ? >


"Minimum number of squares whose sum equals to given number n | Returns count of minimum squares that sum to n ; Create a dynamic programming table to store sq ; getMinSquares table for base case entries ; getMinSquares rest of the table using recursive formula ; max value is i as i can always be represented as 1 * 1 + 1 * 1 + ... ; Go through all smaller numbers to recursively find minimum ; Store result and free dp [ ] ; Driver Code"
How can the above be solved in PHP?

< ? php function getMinSquares ( $ n ) { $ dp ; $ dp [ 0 ] = 0 ; $ dp [ 1 ] = 1 ; $ dp [ 2 ] = 2 ; $ dp [ 3 ] = 3 ; for ( $ i = 4 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] = $ i ; for ( $ x = 1 ; $ x <= ceil ( sqrt ( $ i ) ) ; $ x ++ ) { $ temp = $ x * $ x ; if ( $ temp > $ i ) break ; else $ dp [ $ i ] = min ( $ dp [ $ i ] , ( 1 + $ dp [ $ i - $ temp ] ) ) ; } } $ res = $ dp [ $ n ] ; return $ res ; } echo getMinSquares ( 6 ) ; ? >


"Minimum number of squares whose sum equals to given number n | Returns count of minimum squares that sum to n ; base cases ; getMinSquares rest of the table using recursive formula Maximum squares required is n ( 1 * 1 + 1 * 1 + . . ) ; Go through all smaller numbers to recursively find minimum ; Driver Code"
How can the above be solved in PHP?

< ? php function getMinSquares ( $ n ) { if ( $ n <= 3 ) return $ n ; $ res = $ n ; for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) { $ temp = $ x * $ x ; if ( $ temp > $ n ) break ; else $ res = min ( $ res , 1 + getMinSquares ( $ n - $ temp ) ) ; } return $ res ; } echo getMinSquares ( 6 ) ; ? >


"Minimum number of sub | Function that returns true if n is a power of 5 ; Function to return the decimal value of binary equivalent ; Function to return the minimum cuts required ; Allocating memory for dp [ ] array ; From length 1 to n ; If previous character is '0' then ignore to avoid number with leading 0 s . ; Ignore s [ j ] = '0' starting numbers ; Number formed from s [ j ... . i ] ; Check for power of 5 ; Assigning min value to get min cut possible ; ( n + 1 ) to check if all the strings are traversed and no divisible by 5 is obtained like 000000 ; Driver code"
How can the above be solved in PHP?


"Minimum number of subtract operation to make an array decreasing | Function to count minimum no of operation ; Count how many times we have to subtract . ; Check an additional subtraction is required or not . ; Modify the value of arr [ i ] . ; Count total no of operation / subtraction . ; Driver Code"
How can the above be solved in PHP?

< ? php function min_noOf_operation ( $ arr , $ n , $ k ) { $ noOfSubtraction ; $ res = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ noOfSubtraction = 0 ; if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] ) { $ noOfSubtraction = ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) / $ k ; if ( ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) % $ k != 0 ) $ noOfSubtraction ++ ; $ arr [ $ i ] = $ arr [ $ i ] - $ k * $ noOfSubtraction ; } $ res = $ res + $ noOfSubtraction ; } return floor ( $ res ) ; } $ arr = array ( 1 , 1 , 2 , 3 ) ; $ N = count ( $ arr ) ; $ k = 5 ; echo min_noOf_operation ( $ arr , $ N , $ k ) ; ? >


"Minimum number of towers required such that every house is in the range of at least one tower | Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; given elements ; print number of towers"
How can the above be solved in PHP?

< ? php function number_of_tower ( $ house , $ range , $ n ) { sort ( $ house ) ; $ numOfTower = 0 ; $ i = 0 ; while ( $ i < $ n ) { $ numOfTower ++ ; $ loc = $ house [ $ i ] + $ range ; while ( $ i < $ n && $ house [ $ i ] <= $ loc ) $ i ++ ; -- $ i ; $ loc = $ house [ $ i ] + $ range ; while ( $ i < $ n && $ house [ $ i ] <= $ loc ) $ i ++ ; } return $ numOfTower ; } $ house = array ( 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 ) ; $ range = 2 ; $ n = sizeof ( $ house ) / sizeof ( $ house [ 0 ] ) ; echo number_of_tower ( $ house , $ range , $ n ) ; ? >


"Minimum number operations required to convert n to m | Set | PHP implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code"
How can the above be solved in PHP?

< ? php $ MAXN = 10000000 ; function minimumSteps ( $ n , $ m , $ a , $ b ) { global $ MAXN ; if ( $ n > $ m ) return $ MAXN ; if ( $ n == $ m ) return 0 ; return min ( 1 + minimumSteps ( $ n * $ a , $ m , $ a , $ b ) , 1 + minimumSteps ( $ n * $ b , $ m , $ a , $ b ) ) ; } $ n = 120 ; $ m = 51840 ; $ a = 2 ; $ b = 3 ; echo minimumSteps ( $ n , $ m , $ a , $ b ) ; ? >


"Minimum number with digits as 4 and 7 only and given sum | Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code"
How can the above be solved in PHP?


"Minimum numbers ( smaller than or equal to N ) with sum S | Function to find the minimum numbers required to get to S ; Driver Code"
How can the above be solved in PHP?

< ? php function minimumNumbers ( $ n , $ s ) { if ( $ s % $ n ) return round ( $ s / $ n + 1 ) ; else return round ( $ s / $ n ) ; } $ n = 5 ; $ s = 11 ; echo minimumNumbers ( $ n , $ s ) ; ? >


"Minimum numbers needed to express every integer below N as a sum | function to count length of binary expression of n ; Driver code"
How can the above be solved in PHP?

 < ? php function countBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count ++ ; $ n >>= 1 ; } return $ count ; } $ n = 32 ; echo " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " , countBits ( $ n ) , "
 " ; ? >


"Minimum odd cost path in a matrix | PHP program to find Minimum odd cost path in a matrix ; Function to find the minimum cost ; leftmost element ; rightmost element ; Any element except leftmost and rightmost element of a row is reachable from direct upper or left upper or right upper row 's block ; Counting the minimum cost ; Find the minimum cost ; Driver code"
How can the above be solved in PHP?


"Minimum operations of the given type required to make a complete graph | Function to return the minimum number of steps required ; Driver Code"
How can the above be solved in PHP?

< ? php function minOperations ( $ N ) { $ x = log ( $ N , 2 ) ; $ ans = ceil ( $ x ) ; return $ ans ; } $ N = 10 ; echo minOperations ( $ N ) ; ? >


"Minimum operations required to make all the array elements equal | Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code"
How can the above be solved in PHP?


"Minimum operations required to modify the array such that parity of adjacent elements is different | Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code"
How can the above be solved in PHP?

< ? php function parity ( $ a ) { return $ a % 3 ; } function solve ( $ array , $ size ) { $ operations = 0 ; for ( $ i = 0 ; $ i < $ size - 1 ; $ i ++ ) { if ( parity ( $ array [ $ i ] ) == parity ( $ array [ $ i + 1 ] ) ) { $ operations ++ ; if ( $ i + 2 < $ size ) { $ pari1 = parity ( $ array [ $ i ] ) ; $ pari2 = parity ( $ array [ $ i + 2 ] ) ; if ( $ pari1 == $ pari2 ) { if ( $ pari1 == 0 ) $ array [ $ i + 1 ] = 1 ; else if ( $ pari1 == 1 ) $ array [ $ i + 1 ] = 0 ; else $ array [ $ i + 1 ] = 1 ; } else { if ( ( $ pari1 == 0 && $ pari2 == 1 ) || ( $ pari1 == 1 && $ pari2 == 0 ) ) $ array [ $ i + 1 ] = 2 ; if ( ( $ pari1 == 1 && $ pari2 == 2 ) || ( $ pari1 == 2 && $ pari2 == 1 ) ) $ array [ $ i + 1 ] = 0 ; if ( ( $ pari1 == 2 && $ pari2 == 0 ) || ( $ pari1 == 0 && $ pari2 == 2 ) ) $ array [ $ i + 1 ] = 1 ; } } } } return $ operations ; } $ array = array ( 2 , 1 , 3 , 0 ) ; $ size = count ( $ array ) ; echo solve ( $ array , $ size ) ; ? >


"Minimum operations required to set all elements of binary matrix | PHP program to find minimum operations required to set all the element of binary matrix ; Return minimum operation required to make all 1 s . ; check if this cell equals 0 ; increase the number of moves ; flip from this cell to the start point ; flip the cell ; Driver Code"
How can the above be solved in PHP?


"Minimum operations to make GCD of array a multiple of k | PHP program to make GCD of array a multiple of k . ; If array value is not 1 and it is greater than k then we can increase the or decrease the remainder obtained by dividing k from the ith value of array so that we get the number which is either closer to k or its multiple ; Else we only have one choice which is to increment the value to make equal to k ; Driver code"
How can the above be solved in PHP?

< ? php function MinOperation ( $ a , $ n , $ k ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ a [ $ i ] != 1 && $ a [ $ i ] > $ k ) { $ result = $ result + min ( $ a [ $ i ] % $ k , $ k - $ a [ $ i ] % $ k ) ; } else { $ result = $ result + $ k - $ a [ $ i ] ; } } return $ result ; } $ arr = array ( 4 , 5 , 6 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ k = 5 ; echo MinOperation ( $ arr , $ n , $ k ) ; ? >


"Minimum partitions of maximum size 2 and sum limited by given value | PHP program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code"
How can the above be solved in PHP?

< ? php function minimumSets ( $ arr , $ n , $ key ) { $ i ; $ j ; sort ( $ arr ) ; for ( $ i = 0 , $ j = $ n - 1 ; $ i <= $ j ; ++ $ i ) if ( $ arr [ $ i ] + $ arr [ $ j ] <= $ key ) $ j -- ; return $ i ; } $ arr = array ( 3 , 5 , 3 , 4 ) ; $ n = count ( $ arr ) ; $ key = 5 ; echo minimumSets ( $ arr , $ n , $ key ) ; ? >


"Minimum positive integer divisible by C and is not in range [ A , B ] | Function to return the required number ; If doesn 't belong to the range  then c is the required number ; Else get the next multiple of c starting from b + 1 ; Driver code"
How can the above be solved in PHP?

< ? php function getMinNum ( $ a , $ b , $ c ) { if ( $ c < $ a $ c > $ b ) return $ c ; $ x = ( floor ( ( $ b / $ c ) ) * $ c ) + $ c ; return $ x ; } $ a = 2 ; $ b = 4 ; $ c = 4 ; echo getMinNum ( $ a , $ b , $ c ) ; ? >


"Minimum positive integer to divide a number such that the result is an odd | Function to find the value ; Return 1 if already odd ; Check how many times it is divided by 2 ; Driver code"
How can the above be solved in PHP?

< ? php function makeOdd ( $ n ) { if ( $ n % 2 != 0 ) return 1 ; $ resul = 1 ; while ( $ n % 2 == 0 ) { $ n /= 2 ; $ resul *= 2 ; } return $ resul ; } $ n = 36 ; echo makeOdd ( $ n ) ; ? >


"Minimum positive integer to divide a number such that the result is an odd | PHP program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code"
How can the above be solved in PHP?


"Minimum positive integer value possible of X for given A and B in X = P * A + Q * B | Function to return gcd of a and b ; Driver Code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } $ a = 2 ; $ b = 4 ; echo gcd ( $ a , $ b ) ; ? >


"Minimum possible final health of the last monster in a game | Function to return the gcd of two numbers ; Function to return the minimum possible health for the monster ; gcd of first and second element ; gcd for all subsequent elements ; Driver code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function solve ( $ health , $ n ) { $ currentgcd = gcd ( $ health [ 0 ] , $ health [ 1 ] ) ; for ( $ i = 2 ; $ i < $ n ; ++ $ i ) { $ currentgcd = gcd ( $ currentgcd , $ health [ $ i ] ) ; } return $ currentgcd ; } $ health = array ( 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ health ) ; echo solve ( $ health , $ n ) ; ? >