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


"Minimum distance between two occurrences of maximum | function to return min distance ; case a ; case b ; case c ; Driver Code"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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 ) ; ? >


"Minimum possible sum of array elements after performing the given operation | Function to return the minimized sum ; To store the largest element from the array which is divisible by x ; Sum of array elements before performing any operation ; If current element is divisible by x and it is maximum so far ; Update the minimum element ; If no element can be reduced then there 's no point in performing the  operation as we will end up increasing  the sum when an element is multiplied by x ; Subtract the chosen elements from the sum and then add their updated values ; Return the minimized sum ; Driver code"
Solution in php:

< ? php function minSum ( $ arr , $ n , $ x ) { $ sum = 0 ; $ largestDivisible = -1 ; $ minimum = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; if ( $ arr [ $ i ] % $ x == 0 && $ largestDivisible < $ arr [ $ i ] ) $ largestDivisible = $ arr [ $ i ] ; if ( $ arr [ $ i ] < $ minimum ) $ minimum = $ arr [ $ i ] ; } if ( $ largestDivisible == -1 ) return $ sum ; $ sumAfterOperation = $ sum - $ minimum - $ largestDivisible + ( $ x * $ minimum ) + ( $ largestDivisible / $ x ) ; return min ( $ sum , $ sumAfterOperation ) ; } $ arr = array ( 5 , 5 , 5 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; $ x = 3 ; print ( minSum ( $ arr , $ n , $ x ) ) ; ? >


"Minimum product pair an array of positive Integers | Function to calculate minimum product of pair ; Initialize first and second minimums . It is assumed that the array has at least two elements . ; Traverse remaining array and keep track of two minimum elements ( Note that the two minimum elements may be same if minimum element appears more than once ) more than once ) ; Driver Code"
Solution in php:

< ? php function printMinimumProduct ( $ arr , $ n ) { $ first_min = min ( $ arr [ 0 ] , $ arr [ 1 ] ) ; $ second_min = max ( $ arr [ 0 ] , $ arr [ 1 ] ) ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < $ first_min ) { $ second_min = $ first_min ; $ first_min = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] < $ second_min ) $ second_min = $ arr [ $ i ] ; } return $ first_min * $ second_min ; } $ a = array ( 11 , 8 , 5 , 7 , 5 , 100 ) ; $ n = sizeof ( $ a ) ; echo ( printMinimumProduct ( $ a , $ n ) ) ; ? >


"Minimum product subset of an array | Function to find maximum product of a subset ; Find count of negative numbers , count of zeros , maximum valued negative number , minimum valued positive number and product of non - zero numbers ; If number is 0 , we don 't  multiply it with product. ; Count negatives and keep track of maximum valued negative . ; Track minimum positive number of array ; If there are all zeros or no negative number present ; If there are all positive ; If there are even number of negative numbers and count_neg not 0 ; Otherwise result is product of all non - zeros divided by maximum valued negative . ; Driver code"
Solution in php:

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


"Minimum reduce operations to convert a given string into a palindrome | Returns count of minimum character reduce operations to make palindrome . ; Compare every character of first half with the corresponding character of second half and add difference to result . ; Driver code"
Solution in php:

< ? php function countReduce ( $ str ) { $ n = strlen ( $ str ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) $ res += abs ( ord ( $ str [ $ i ] ) - ord ( $ str [ ( $ n - $ i - 1 ) ] ) ) ; return $ res ; } $ str = " abcd " ; echo countReduce ( $ str ) ; ? >


"Minimum removal to make palindrome permutation | function to find minimum removal of characters ; hash to store frequency of each character and to set hash array to zeros ; count frequency of each character ; count the odd frequency characters ; if count is - 1 return 0 otherwise return count ; Driver Code"
Solution in php:

 < ? php function minRemoval ( $ str ) { $ hash = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ hash [ ord ( $ str [ $ i ] ) - 97 ] ++ ; $ count = 0 ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) if ( $ hash [ $ i ] % 2 ) $ count ++ ; return ( $ count == 0 ) ? 0 : $ count - 1 ; } $ str = " geeksforgeeks " ; echo minRemoval ( $ str ) . "
 " ; ? >


"Minimum removals from array to make max | PHP program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver Code"
Solution in php:

< ? php $ MAX = 100 ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { for ( $ j = 0 ; $ j < $ MAX ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; } function countRemovals ( $ a , $ i , $ j , $ k ) { global $ dp ; if ( $ i >= $ j ) return 0 ; else if ( ( $ a [ $ j ] - $ a [ $ i ] ) <= $ k ) return 0 ; else if ( $ dp [ $ i ] [ $ j ] != -1 ) return $ dp [ $ i ] [ $ j ] ; else if ( ( $ a [ $ j ] - $ a [ $ i ] ) > $ k ) { $ dp [ $ i ] [ $ j ] = 1 + min ( countRemovals ( $ a , $ i + 1 , $ j , $ k ) , countRemovals ( $ a , $ i , $ j - 1 , $ k ) ) ; } return $ dp [ $ i ] [ $ j ] ; } function removals ( $ a , $ n , $ k ) { sort ( $ a ) ; if ( $ n == 1 ) return 0 ; else return countRemovals ( $ a , 0 , $ n - 1 , $ k ) ; } $ a = array ( 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ) ; $ n = count ( $ a ) ; $ k = 4 ; echo ( removals ( $ a , $ n , $ k ) ) ; ? >


"Minimum removals to make array sum odd | Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code"
Solution in php:

< ? php function findCount ( $ arr , $ n ) { $ countOdd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 == 1 ) $ countOdd ++ ; if ( $ countOdd % 2 == 0 ) return 1 ; else return 0 ; } $ arr = array ( 1 , 2 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; echo ( findCount ( $ arr , $ n ) ) ; ? >


"Minimum replacements to make adjacent characters unequal in a ternary string | Function to count the number of minimal replacements ; Find the length of the string ; Iterate in the string ; Check if adjacent is similar ; If not the last pair ; Check for character which is not same in i + 1 and i - 1 ; else Last pair ; Check for character which is not same in i - 1 index ; Driver Code"
Solution in php:


"Minimum revolutions to move center of a circle to a target | Minimum revolutions to move center from ( x1 , y1 ) to ( x2 , y2 ) ; Driver code"
Solution in php:


"Minimum rooms for m events of n batches with given schedule | Returns minimum number of rooms required to perform classes of n groups in m slots with given schedule . ; Store count of classes happening in every slot . ; initialize all values to zero ; Number of rooms required is equal to maximum classes happening in a particular slot . ; Driver Code"
Solution in php:

< ? php function findMinRooms ( $ slots , $ n , $ m ) { $ counts = array_fill ( 0 , $ m , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ slots [ $ i ] [ $ j ] == '1' ) $ counts [ $ j ] ++ ; return max ( $ counts ) ; } $ n = 3 ; $ m = 7 ; $ slots = array ( "0101011" , "0011001" , "0110111" ) ; echo findMinRooms ( $ slots , $ n , $ m ) ; ? >


"Minimum rotations to unlock a circular lock | function for min rotation ; iterate till input and unlock code become 0 ; input and unlock last digit as reminder ; find min rotation ; update code and input ; Driver Code"
Solution in php:

< ? php function minRotation ( $ input , $ unlock_code ) { $ rotation = 0 ; $ input_digit ; $ code_digit ; while ( $ input $ unlock_code ) { $ input_digit = $ input % 10 ; $ code_digit = $ unlock_code % 10 ; $ rotation += min ( abs ( $ input_digit - $ code_digit ) , 10 - abs ( $ input_digit - $ code_digit ) ) ; $ input /= 10 ; $ unlock_code /= 10 ; } return $ rotation ; } $ input = 28756 ; $ unlock_code = 98234 ; echo " Minimum ▁ Rotation ▁ = ▁ " , minRotation ( $ input , $ unlock_code ) ; ? >


"Minimum squares to cover a rectangle | PHP program to find the minimum number of squares to cover the surface of the rectangle with given dimensions ; function to count the number of squares that can cover the surface of the rectangle ; Driver code"
Solution in php:

< ? php function squares ( $ l , $ b , $ a ) { return ceil ( $ l / ( double ) $ a ) * ceil ( $ b / ( double ) $ a ) ; } $ l = 11 ; $ b = 23 ; $ a = 14 ; echo squares ( $ l , $ b , $ a ) ; ? >


"Minimum squares to evenly cut a rectangle | PHP program to find minimum number of squares to make a given rectangle . ; if we take gcd ( l , w ) , this will be largest possible side for square , hence minimum number of square . ; Number of squares . ; Driver code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { return $ b ? gcd ( $ b , $ a % $ b ) : $ a ; } function countRectangles ( $ l , $ w ) { $ squareSide = gcd ( $ l , $ w ) ; return ( $ l * $ w ) / ( $ squareSide * $ squareSide ) ; } $ l = 4 ; $ w = 6 ; echo countRectangles ( $ l , $ w ) . "
 " ; ? >


"Minimum steps to convert one binary string to other only using negation | Function to find the minimum steps to convert string a to string b ; array to mark the positions needed to be negated ; If two character are not same then they need to be negated ; To count the blocks of 1 ; To count the number of 1 ' s ▁ in ▁ ▁ each ▁ block ▁ of ▁ 1' s ; For the last block of 1 's ; Driver code"
Solution in php:


"Minimum steps to delete a string after repeated deletion of palindrome substrings | method returns minimum step for deleting the string , where in one step a palindrome is removed ; declare dp array and initialize it with 0 s ; loop for substring length we are considering ; loop with two variables i and j , denoting starting and ending of substrings ; If substring length is 1 , then 1 step will be needed ; delete the ith char individually and assign result for subproblem ( i + 1 , j ) ; if current and next char are same , choose min from current and subproblem ( i + 2 , j ) ; loop over all right characters and suppose Kth char is same as ith character then choose minimum from current and two substring after ignoring ith and Kth char ; Uncomment below snippet to print actual dp tablex for ( int i = 0 ; i < N ; i ++ , cout << endl ) for ( int j = 0 ; j < N ; j ++ ) cout << dp [ i ] [ j ] << " ▁ " ; ; Driver code"
Solution in php:


"Minimum steps to delete a string by deleting substring comprising of same characters | PHP implementation of the approach ; Function to return the minimum number of delete operations ; When a single character is deleted ; When a group of consecutive characters are deleted if any of them matches ; When both the characters are same then delete the letters in between them ; Memoize ; Driver code"
Solution in php:

< ? php $ GLOBALS [ ' N ' ] = 10 ; function findMinimumDeletion ( $ l , $ r , $ dp , $ s ) { if ( $ l > $ r ) return 0 ; if ( $ l == $ r ) return 1 ; if ( $ dp [ $ l ] [ $ r ] != -1 ) return $ dp [ $ l ] [ $ r ] ; $ res = 1 + findMinimumDeletion ( $ l + 1 , $ r , $ dp , $ s ) ; for ( $ i = $ l + 1 ; $ i <= $ r ; ++ $ i ) { if ( $ s [ $ l ] == $ s [ $ i ] ) $ res = min ( $ res , findMinimumDeletion ( $ l + 1 , $ i - 1 , $ dp , $ s ) + findMinimumDeletion ( $ i , $ r , $ dp , $ s ) ) ; } return $ dp [ $ l ] [ $ r ] = $ res ; } $ s = " abcddcba " ; $ n = strlen ( $ s ) ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) for ( $ j = 0 ; $ j < $ GLOBALS [ ' N ' ] ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo findMinimumDeletion ( 0 , $ n - 1 , $ dp , $ s ) ; ? >


"Minimum steps to minimize n as per given condition | A tabulation based solution in PHP ; Driver Code"
Solution in php:

< ? php function getMinSteps ( $ n ) { $ table = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ table [ $ i ] = $ n - $ i ; for ( $ i = $ n ; $ i >= 1 ; $ i -- ) { if ( ! ( $ i % 2 ) ) $ table [ $ i / 2 ] = min ( $ table [ $ i ] + 1 , $ table [ $ i / 2 ] ) ; if ( ! ( $ i % 3 ) ) $ table [ $ i / 3 ] = min ( $ table [ $ i ] + 1 , $ table [ $ i / 3 ] ) ; } return $ table [ 1 ] ; } $ n = 10 ; echo getMinSteps ( $ n ) ; ? >


"Minimum steps to minimize n as per given condition | function to calculate min steps ; base case ; store temp value for n as min ( f ( n - 1 ) , f ( n / 2 ) , f ( n / 3 ) ) + 1 ; store memo [ n ] and return ; This function mainly initializes memo [ ] and calls getMinSteps ( n , memo ) ; initialize memoized array ; Driver Code"
Solution in php:

< ? php function getMinSteps ( $ n , $ memo ) { if ( $ n == 1 ) return 0 ; if ( $ memo [ $ n ] != -1 ) return $ memo [ $ n ] ; $ res = getMinSteps ( $ n - 1 , $ memo ) ; if ( $ n % 2 == 0 ) $ res = min ( $ res , getMinSteps ( $ n / 2 , $ memo ) ) ; if ( $ n % 3 == 0 ) $ res = min ( $ res , getMinSteps ( $ n / 3 , $ memo ) ) ; $ memo [ $ n ] = 1 + $ res ; return $ memo [ $ n ] ; } function g_etMinSteps ( $ n ) { $ memo = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ memo [ $ i ] = -1 ; return getMinSteps ( $ n , $ memo ) ; } $ n = 10 ; echo g_etMinSteps ( $ n ) ; ? >


"Minimum steps to reach a destination | source -> source vertex step -> value of last step taken dest -> destination vertex ; base cases ; if we go on positive side ; if we go on negative side ; minimum of both cases ; Driver code"
Solution in php:

< ? php function steps ( $ source , $ step , $ dest ) { if ( abs ( $ source ) > ( $ dest ) ) return PHP_INT_MAX ; if ( $ source == $ dest ) return $ step ; $ pos = steps ( $ source + $ step + 1 , $ step + 1 , $ dest ) ; $ neg = steps ( $ source - $ step - 1 , $ step + 1 , $ dest ) ; return min ( $ pos , $ neg ) ; } $ dest = 11 ; echo " No . ▁ of ▁ steps ▁ required ▁ to ▁ reach ▁ " , $ dest , " ▁ is ▁ " , steps ( 0 , 0 , $ dest ) ; ? >


"Minimum steps to remove substring 010 from a binary string | Function to find the minimum steps ; substring "010" found ; Get the binary string ; Find the minimum steps"
Solution in php:


"Minimum sum after subtracting multiples of k from the elements of the array | function to calculate minimum sum after transformation ; no element can be reduced further ; if all the elements of the array are identical ; check if a [ i ] can be reduced to a [ 0 ] ; one of the elements cannot be reduced to be equal to the other elements ; if k = 1 then all elements can be reduced to 1 ; Driver code"
Solution in php:


"Minimum sum by choosing minimum of pairs from array | Returns minimum possible sum in array B [ ] ; Driver Code"
Solution in php:

< ? php function minSum ( $ A , $ n ) { $ min_val = min ( $ A ) ; return ( $ min_val * ( $ n - 1 ) ) ; } $ A = array ( 3 , 6 , 2 , 8 , 7 , 5 ) ; $ n = count ( $ A ) ; echo minSum ( $ A , $ n ) ; ? >


"Minimum sum of absolute difference of pairs of two arrays | Returns minimum possible pairwise absolute difference of two arrays . ; Sort both arrays ; Find sum of absolute differences ; Both a [ ] and b [ ] must be of same size ."
Solution in php:

< ? php function findMinSum ( $ a , $ b , $ n ) { sort ( $ a ) ; sort ( $ a , $ n ) ; sort ( $ b ) ; sort ( $ b , $ n ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + abs ( $ a [ $ i ] - $ b [ $ i ] ) ; return $ sum ; } $ a = array ( 4 , 1 , 8 , 7 ) ; $ b = array ( 2 , 3 , 6 , 5 ) ; $ n = sizeof ( $ a ) ; echo ( findMinSum ( $ a , $ b , $ n ) ) ; ? >


"Minimum sum of differences with an element in an array | function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code"
Solution in php:

< ? php function absSumDidd ( $ a , $ n ) { sort ( $ a ) ; $ midValue = $ a [ ( $ n / 2 ) ] ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + abs ( $ a [ $ i ] - $ midValue ) ; } return $ sum ; } $ arr = array ( 5 , 11 , 14 , 10 , 17 , 15 ) ; $ n = count ( $ arr ) ; echo absSumDidd ( $ arr , $ n ) ; ? >


"Minimum sum of multiplications of n numbers | Used in recursive memoized solution ; function to calculate the cumulative sum from a [ i ] to a [ j ] ; base case ; memoization , if the partition has been called before then return the stored value ; store a max value ; we break them into k partitions ; store the min of the formula thus obtained ; return the minimum ; Driver code"
Solution in php:


"Minimum sum of product of two arrays | Function to find the minimum product ; Find product of current elements and update result . ; If both product and b [ i ] are negative , we must increase value of a [ i ] to minimize result . ; If both product and a [ i ] are negative , we must decrease value of a [ i ] to minimize result . ; Similar to above two cases for positive product . ; Check if current difference becomes higher than the maximum difference so far . ; Driver Code"
Solution in php:


"Minimum sum of two numbers formed from digits of an array | Function to find and return minimum sum of two numbers formed from digits of the array . ; sort the array ; let two numbers be a and b ; fill a and b with every alternate digit of input array ; return the sum ; Driver code"
Solution in php:

< ? php function solve ( $ arr , $ n ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ a = 0 ; $ b = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i & 1 ) $ a = $ a * 10 + $ arr [ $ i ] ; else $ b = $ b * 10 + $ arr [ $ i ] ; } return $ a + $ b ; } $ arr = array ( 6 , 8 , 4 , 5 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Sum ▁ is ▁ " , solve ( $ arr , $ n ) ; ? >


"Minimum sum of two numbers formed from digits of an array | Returns sum of two numbers formed from all digits in a [ ] ; sort the elements ; Driver code"
Solution in php:

 < ? php function minSum ( $ a , $ n ) { sort ( $ a ) ; $ num1 = 0 ; $ num2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ num1 = $ num1 * 10 + $ a [ $ i ] ; else $ num2 = $ num2 * 10 + $ a [ $ i ] ; } return ( $ num2 + $ num1 ) ; } $ arr = array ( 5 , 3 , 0 , 7 , 4 ) ; $ n = sizeof ( $ arr ) ; echo " The ▁ required ▁ sum ▁ is ▁ " , minSum ( $ arr , $ n ) , "
 " ; ? >


"Minimum sum possible of any bracket sequence of length N | PHP program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; / Not a proper bracket expression ; If reaches at end ; / If proper bracket expression ; else if not , return max ; If already visited ; To find out minimum sum ; Driver Code"
Solution in php:

< ? php $ MAX_VAL = 10000000 ; $ dp = array_fill ( 0 , 100 , array_fill ( 0 , 100 , -1 ) ) ; function find ( $ index , $ openbrk , $ n , $ adj ) { global $ MAX_VAL ; global $ dp ; if ( $ openbrk < 0 ) return $ MAX_VAL ; if ( $ index == $ n ) { if ( $ openbrk == 0 ) { return 0 ; } return $ MAX_VAL ; } if ( $ dp [ $ index ] [ $ openbrk ] != -1 ) return $ dp [ $ index ] [ $ openbrk ] ; $ dp [ $ index ] [ $ openbrk ] = min ( $ adj [ $ index ] [ 1 ] + find ( $ index + 1 , $ openbrk + 1 , $ n , $ adj ) , $ adj [ $ index ] [ 0 ] + find ( $ index + 1 , $ openbrk - 1 , $ n , $ adj ) ) ; return $ dp [ $ index ] [ $ openbrk ] ; } $ n = 4 ; $ adj = array ( array ( 5000 , 3000 ) , array ( 6000 , 2000 ) , array ( 8000 , 1000 ) , array ( 9000 , 6000 ) ) ; echo find ( 1 , 1 , $ n , $ adj ) + $ adj [ 0 ] [ 1 ] ; ? >


"Minimum swaps required to bring all elements less than or equal to k together | Utility function to find minimum swaps required to club all elements less than or equals to k together ; Find count of elements which are less than equals to k ; Find unwanted elements in current window of size ' count ' ; Initialize answer with ' bad ' value of current window ; Decrement count of previous window ; Increment count of current window ; Update ans if count of ' bad ' is less in current window ; Driver code"
Solution in php:


"Minimum swaps required to make a binary string alternating | returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code"
Solution in php:

< ? php function countMinSwaps ( $ st ) { $ min_swaps = 0 ; $ odd_0 = 0 ; $ even_0 = 0 ; $ odd_1 = 0 ; $ even_1 = 0 ; $ n = strlen ( $ st ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) { if ( $ st [ $ i ] == '1' ) { $ even_1 ++ ; } else { $ even_0 ++ ; } } else { if ( $ st [ $ i ] == '1' ) { $ odd_1 ++ ; } else { $ odd_0 ++ ; } } } $ cnt_swaps_1 = min ( $ even_0 , $ odd_1 ) ; $ cnt_swaps_2 = min ( $ even_1 , $ odd_0 ) ; return min ( $ cnt_swaps_1 , $ cnt_swaps_2 ) ; } $ st = "000111" ; echo ( countMinSwaps ( $ st ) ) ; ? >


"Minimum time required to complete a work by N persons together | Function to calculate the time ; Driver Code"
Solution in php:

< ? php function calTime ( & $ arr , $ n ) { $ work = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ work += 1 / $ arr [ $ i ] ; return 1 / $ work ; } $ arr = array ( 6.0 , 3.0 , 4.0 ) ; $ n = sizeof ( $ arr ) ; echo calTime ( $ arr , $ n ) ; echo " ▁ Hours " ;


"Minimum time required to transport all the boxes from source to the destination under the given constraints | Function that returns true if it is possible to transport all the boxes in the given amount of time ; If all the boxes can be transported in the given time ; If all the boxes can 't be  transported in the given time ; Function to return the minimum time required ; Sort the two arrays ; Stores minimum time in which all the boxes can be transported ; Check for the minimum time in which all the boxes can be transported ; If it is possible to transport all the boxes in mid amount of time ; Driver code"
Solution in php:

< ? php function isPossible ( $ box , $ truck , $ n , $ m , $ min_time ) { $ temp = 0 ; $ count = 0 ; while ( $ count < $ m ) { for ( $ j = 0 ; $ j < $ min_time && $ temp < $ n && $ truck [ $ count ] >= $ box [ $ temp ] ; $ j += 2 ) $ temp ++ ; $ count ++ ; } if ( $ temp == $ n ) return true ; return false ; } function minTime ( $ box , $ truck , $ n , $ m ) { sort ( $ box ) ; sort ( $ truck ) ; $ l = 0 ; $ h = 2 * $ n ; $ min_time = 0 ; while ( $ l <= $ h ) { $ mid = intdiv ( ( $ l + $ h ) , 2 ) ; if ( isPossible ( $ box , $ truck , $ n , $ m , $ mid ) ) { $ min_time = $ mid ; $ h = $ mid - 1 ; } else $ l = $ mid + 1 ; } return $ min_time ; } $ box = array ( 10 , 2 , 16 , 19 ) ; $ truck = array ( 29 , 25 ) ; $ n = sizeof ( $ box ) ; $ m = sizeof ( $ truck ) ; echo minTime ( $ box , $ truck , $ n , $ m ) ; ? >


"Minimum time to finish tasks without skipping two consecutive | arr [ ] represents time taken by n given tasks ; Corner Cases ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code"
Solution in php:

 < ? php function minTime ( $ arr , $ n ) { if ( $ n <= 0 ) return 0 ; $ excl = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ incl_new = $ arr [ $ i ] + min ( $ excl , $ incl ) ; $ excl_new = $ incl ; $ incl = $ incl_new ; $ excl = $ excl_new ; } return min ( $ incl , $ excl ) ; } $ arr1 = array ( 10 , 5 , 2 , 7 , 10 ) ; $ n1 = sizeof ( $ arr1 ) ; echo minTime ( $ arr1 , $ n1 ) , "
 " ; $ arr2 = array ( 10 , 5 , 7 , 10 ) ; $ n2 = sizeof ( $ arr2 ) ; echo minTime ( $ arr2 , $ n2 ) , "
 " ; $ arr3 = array ( 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 ) ; $ n3 = sizeof ( $ arr3 ) ; echo minTime ( $ arr3 , $ n3 ) ; ? >


"Minimum time to reach a point with + t and | returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code"
Solution in php:

< ? php function cal_minimum_time ( $ X ) { $ t = 0 ; $ sum = 0 ; while ( $ sum < $ X ) { $ t ++ ; $ sum = $ sum + $ t ; } return $ t ; } $ n = 6 ; $ ans = cal_minimum_time ( $ n ) ; echo " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " . $ ans ; ? >


"Minimum toggles to partition a binary array so that it has first 0 s then 1 s | Function to calculate minimum toggling required by using Dynamic programming ; Fill entries in zero [ ] such that zero [ i ] stores count of zeroes to the left of i ( exl ; If zero found update zero [ ] array ; Finding the minimum toggle required from every index ( 0 to n - 1 ) ; Driver Program"
Solution in php:


"Minimum value among AND of elements of every subset of an array | PHP program for the above approach ; Find AND of whole array ; Print the answer ; Driver code"
Solution in php:

< ? php function minAND ( $ arr , $ n ) { $ s = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ s = $ s & $ arr [ $ i ] ; } print ( $ s . " " ) ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = count ( $ arr ) ; minAND ( $ arr , $ n ) ; ? >


"Minimum value of " max ▁ + ▁ min " in a subarray | PHP program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code"
Solution in php:

< ? php function maxSum ( $ arr , $ n ) { if ( $ n < 2 ) return -1 ; $ ans = $ arr [ 0 ] + $ arr [ 1 ] ; for ( $ i = 1 ; $ i + 1 < $ n ; $ i ++ ) $ ans = min ( $ ans , ( $ arr [ $ i ] + $ arr [ $ i + 1 ] ) ) ; return $ ans ; } $ arr = array ( 1 , 12 , 2 , 2 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , $ n ) ; ? >


"Minimum value of N such that xor from 1 to N is equal to K | Function to find the value of N ; variable to store the result ; handling case for '0' ; handling case for '1' ; when number is completely divided by 4 then minimum ' x ' will be ' k ' ; when number divided by 4 gives 3 as remainder then minimum ' x ' will be ' k - 1' ; else it is not possible to get k for any value of x ; let the given number be 7"
Solution in php:

< ? php function findN ( $ k ) { $ ans ; if ( $ k == 0 ) $ ans = 3 ; if ( $ k == 1 ) $ ans = 1 ; else if ( $ k % 4 == 0 ) $ ans = $ k ; else if ( $ k % 4 == 3 ) $ ans = $ k - 1 ; else $ ans = -1 ; return $ ans ; } $ k = 7 ; $ res = findN ( $ k ) ; if ( $ res == -1 ) echo " Not ▁ possible " ; else echo $ res ; ? >


"Minimum value possible of a given function from the given set | Function to find the value of F ( n ) ; Driver code"
Solution in php:

< ? php function findF_N ( $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ ans += ( $ i + 1 ) * ( $ n - $ i - 1 ) ; return $ ans ; } $ n = 3 ; echo findF_N ( $ n ) ;


"Mirror of a point through a 3 D plane | Function to mirror image ; Driver Code ; function call"
Solution in php:


"Mirror of matrix across diagonal | Efficient PHP program to find mirror of matrix across diagonal . ; traverse a matrix and swap mat [ i ] [ j ] with mat [ j ] [ i ] ; Utility function to print a matrix ; Driver Code"
Solution in php:


"Missing even and odd elements from the given arrays | Function to find the missing numbers ; To store the minimum and the maximum odd and even elements from the arrays ; To store the sum of the array elements ; Get the minimum and the maximum even elements from the array ; Get the minimum and the maximum odd elements from the array ; To store the total terms in the series and the required sum of the array ; Total terms from 2 to minEven ; Sum of all even numbers from 2 to minEven ; Total terms from 2 to maxEven ; Sum of all even numbers from 2 to maxEven ; Required sum for the even array ; Missing even number ; Total terms from 1 to minOdd ; Sum of all odd numbers from 1 to minOdd ; Total terms from 1 to maxOdd ; Sum of all odd numbers from 1 to maxOdd ; Required sum for the odd array ; Missing odd number ; Driver code"
Solution in php:

< ? php function findMissingNums ( $ even , $ sizeEven , $ odd , $ sizeOdd ) { $ minEven = PHP_INT_MAX ; $ maxEven = PHP_INT_MIN ; $ minOdd = PHP_INT_MAX ; $ maxOdd = PHP_INT_MIN ; $ sumEvenArr = $ sumOddArr = 0 ; for ( $ i = 0 ; $ i < $ sizeEven ; $ i ++ ) { $ minEven = min ( $ minEven , $ even [ $ i ] ) ; $ maxEven = max ( $ maxEven , $ even [ $ i ] ) ; $ sumEvenArr += $ even [ $ i ] ; } for ( $ i = 0 ; $ i < $ sizeOdd ; $ i ++ ) { $ minOdd = min ( $ minOdd , $ odd [ $ i ] ) ; $ maxOdd = max ( $ maxOdd , $ odd [ $ i ] ) ; $ sumOddArr += $ odd [ $ i ] ; } $ totalTerms = $ reqSum = 0 ; $ totalTerms = ( int ) ( $ minEven / 2 ) ; $ evenSumMin = $ totalTerms * ( $ totalTerms + 1 ) ; $ totalTerms = ( int ) ( $ maxEven / 2 ) ; $ evenSumMax = $ totalTerms * ( $ totalTerms + 1 ) ; $ reqSum = ( $ evenSumMax - $ evenSumMin + $ minEven ) ; echo " Even = " ▁ . ▁ ( $ reqSum ▁ - ▁ $ sumEvenArr ) ▁ . ▁ " " $ totalTerms = ( int ) ( ( $ minOdd / 2 ) + 1 ) ; $ oddSumMin = $ totalTerms * $ totalTerms ; $ totalTerms = ( int ) ( ( $ maxOdd / 2 ) + 1 ) ; $ oddSumMax = $ totalTerms * $ totalTerms ; $ reqSum = ( $ oddSumMax - $ oddSumMin + $ minOdd ) ; echo " Odd = " } $ even = array ( 6 , 4 , 8 , 14 , 10 ) ; $ sizeEven = count ( $ even ) ; $ odd = array ( 7 , 5 , 3 , 11 , 13 ) ; $ sizeOdd = count ( $ odd ) ; findMissingNums ( $ even , $ sizeEven , $ odd , $ sizeOdd ) ; ? >


"Modify a binary array to Bitwise AND of all elements as 1 | Function to check if it is possible or not ; Driver code"
Solution in php:

 < ? php function check ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] ) return true ; return false ; } $ a = array ( 0 , 1 , 0 , 1 ) ; $ n = sizeof ( $ a ) ; if ( check ( $ a , $ n ) ) echo " YES
 " ; else echo " NO
 " ; ? >


"Modify a bit at a given position | Returns modified n . ; Driver code"
Solution in php:

 < ? php function modifyBit ( $ n , $ p , $ b ) { $ mask = 1 << $ p ; return ( $ n & ~ $ mask ) | ( ( $ b << $ p ) & $ mask ) ; } echo modifyBit ( 6 , 2 , 0 ) , "
 " ; echo modifyBit ( 6 , 5 , 1 ) , "
 " ; ? >


"Modify array to maximize sum of adjacent differences | Returns maximum - difference - sum with array modifications allowed . ; Initialize dp [ ] [ ] with 0 values . ; for [ i + 1 ] [ 0 ] ( i . e . current modified value is 1 ) , choose maximum from dp [ $i ] [ 0 ] + abs ( 1 - 1 ) = dp [ i ] [ 0 ] and dp [ $i ] [ 1 ] + abs ( 1 - arr [ i ] ) ; for [ i + 1 ] [ 1 ] ( i . e . current modified value is arr [ i + 1 ] ) , choose maximum from dp [ i ] [ 0 ] + abs ( arr [ i + 1 ] - 1 ) and dp [ i ] [ 1 ] + abs ( arr [ i + 1 ] - arr [ i ] ) ; Driver Code"
Solution in php:


"Modify the string by swapping continuous vowels or consonants | Function to check if a character is a vowel ; Function to swap two consecutively repeated vowels or consonants ; Traverse through the length of the string ; Check if the two consecutive characters are vowels or consonants ; swap the two characters ; Driver code"
Solution in php:

< ? php function isVowel ( $ c ) { $ c = strtolower ( $ c ) ; if ( $ c == ' a ' $ c == ' e ' $ c == ' i ' $ c == ' o ' $ c == ' u ' ) return true ; return false ; } function swapRepeated ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) - 1 ; $ i ++ ) { if ( ( isVowel ( $ str [ $ i ] ) && isVowel ( $ str [ $ i + 1 ] ) ) || ( ! isVowel ( $ str [ $ i ] ) && ! isVowel ( $ str [ $ i + 1 ] ) ) ) { $ t = $ str [ $ i ] ; $ str [ $ i ] = $ str [ $ i + 1 ] ; $ str [ $ i + 1 ] = $ t ; } } return $ str ; } $ str = " geeksforgeeks " ; echo swapRepeated ( $ str ) ; return 0 ; ? >


"Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Driver Code"
Solution in php:


"Modular exponentiation ( Recursive ) | Recursive PHP program to compute modular power ; Base cases ; If B is even ; If B is odd ; Driver Code"
Solution in php:

< ? php function exponentMod ( $ A , $ B , $ C ) { if ( $ A == 0 ) return 0 ; if ( $ B == 0 ) return 1 ; if ( $ B % 2 == 0 ) { $ y = exponentMod ( $ A , $ B / 2 , $ C ) ; $ y = ( $ y * $ y ) % $ C ; } else { $ y = $ A % $ C ; $ y = ( $ y * exponentMod ( $ A , $ B - 1 , $ C ) % $ C ) % $ C ; } return ( ( $ y + $ C ) % $ C ) ; } $ A = 2 ; $ B = 5 ; $ C = 13 ; echo " Power ▁ is ▁ " . exponentMod ( $ A , $ B , $ C ) ; ? >


"Modular multiplicative inverse | A naive method to find modulor multiplicative inverse of ' a ' under modulo ' m ' ; Driver Code ; Function call"
Solution in php:

< ? php function modInverse ( $ a , $ m ) { for ( $ x = 1 ; $ x < $ m ; $ x ++ ) if ( ( ( $ a % $ m ) * ( $ x % $ m ) ) % $ m == 1 ) return $ x ; } $ a = 3 ; $ m = 11 ; echo modInverse ( $ a , $ m ) ; a . . . >


"Modular multiplicative inverse | Returns modulo inverse of a with respect to m using extended Euclid Algorithm Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; Driver Code ; Function call"
Solution in php:

 < ? php function modInverse ( $ a , $ m ) { $ m0 = $ m ; $ y = 0 ; $ x = 1 ; if ( $ m == 1 ) return 0 ; while ( $ a > 1 ) { $ q = ( int ) ( $ a / $ m ) ; $ t = $ m ; $ m = $ a % $ m ; $ a = $ t ; $ t = $ y ; $ y = $ x - $ q * $ y ; $ x = $ t ; } if ( $ x < 0 ) $ x += $ m0 ; return $ x ; } $ a = 3 ; $ m = 11 ; echo " Modular ▁ multiplicative ▁ inverse ▁ is
 " , modInverse ( $ a , $ m ) ; a . . . >


"Modulo power for large numbers represented as strings | PHP program to find ( a ^ b ) % MOD where a and b may be very large and represented as strings . ; Returns modulo exponentiation for two numbers represented as long long int . It is used by powerStrings ( ) . Its complexity is log ( n ) ; Returns modulo exponentiation for two numbers represented as strings . It is used by powerStrings ( ) ; We convert strings to number ; calculating a % MOD ; calculating b % ( MOD - 1 ) ; Now a and b are long long int . We calculate a ^ b using modulo exponentiation ; As numbers are very large that is it may contains upto 10 ^ 6 digits . So , we use string ."
Solution in php:

< ? php $ MOD = 1000000007 ; function powerLL ( $ x , $ n ) { global $ MOD ; $ result = 1 ; while ( $ n ) { if ( $ n & 1 ) $ result = $ result * $ x % $ MOD ; $ n = ( int ) $ n / 2 ; $ x = $ x * $ x % $ MOD ; } return $ result ; } function powerStrings ( $ sa , $ sb ) { global $ MOD ; $ a = 0 ; $ b = 0 ; for ( $ i = 0 ; $ i < strlen ( $ sa ) ; $ i ++ ) $ a = ( $ a * 10 + ( $ sa [ $ i ] - '0' ) ) % $ MOD ; for ( $ i = 0 ; $ i < strlen ( $ sb ) ; $ i ++ ) $ b = ( $ b * 10 + ( $ sb [ $ i ] - '0' ) ) % ( $ MOD - 1 ) ; return powerLL ( $ a , $ b ) ; } $ sa = "2" ; $ sb = "3" ; echo powerStrings ( $ sa , $ sb ) ; ? >


"Morse Code Implementation | function to encode a alphabet as Morse code ; refer to the Morse table image attached in the article ; for space ; Character by character print Morse code ; Driver code"
Solution in php:

 < ? php function morseEncode ( $ x ) { switch ( $ x ) { case ' a ' : return " . - " ; case ' b ' : return " - . . . " ; case ' c ' : return " - . - . " ; case ' d ' : return " - . . " ; case ' e ' : return " . " ; case ' f ' : return " . . - . " ; case ' g ' : return " - - . " ; case ' h ' : return " . . . . " ; case ' i ' : return " . . " ; case ' j ' : return " . - - - " ; case ' k ' : return " - . - " ; case ' l ' : return " . - . . " ; case ' m ' : return " - - " ; case ' n ' : return " - . " ; case ' o ' : return " - - - " ; case ' p ' : return " . - - . " ; case ' q ' : return " - - . - " ; case ' r ' : return " . - . " ; case ' s ' : return " . . . " ; case ' t ' : return " - " ; case ' u ' : return " . . - " ; case ' v ' : return " . . . - " ; case ' w ' : return " . - - " ; case ' x ' : return " - . . - " ; case ' y ' : return " - . - - " ; case ' z ' : return " - - . . " ; case '1' : return " . - - - - " ; case '2' : return " . . - - - " ; case '3' : return " . . . - - " ; case '4' : return " . . . . - " ; case '5' : return " . . . . . " ; case '6' : return " - . . . . " ; case '7' : return " - - . . . " ; case '8' : return " - - - . . " ; case '9' : return " - - - - . " ; case '0' : return " - - - - - " ; } } function morseCode ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) echo morseEncode ( $ s [ $ i ] ) ; echo "
 " ; } $ s = " geeksforgeeks " ; morseCode ( $ s ) ; ? >


"Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code"
Solution in php:

 < ? php function gen ( $ n ) { if ( $ n == 0 ) return 0 ; else if ( $ n == 1 ) return 1 ; else if ( $ n % 2 == 0 ) return 4 * gen ( $ n / 2 ) ; else if ( $ n % 2 == 1 ) return 4 * gen ( $ n / 2 ) + 1 ; } function moserDeBruijn ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( gen ( $ i ) . " ▁ " ) ; echo ( "
 " ) ; } $ n = 15 ; echo ( " First ▁ " . $ n . " ▁ terms ▁ of ▁ " . " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁
 " ) ; echo ( moserDeBruijn ( $ n ) ) ; ? >


"Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code"
Solution in php:

 < ? php function gen ( $ n ) { $ S = array ( ) ; $ S [ 0 ] = 0 ; $ S [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ S [ $ i ] = 4 * $ S [ $ i / 2 ] ; else $ S [ $ i ] = 4 * $ S [ $ i / 2 ] + 1 ; } return $ S [ $ n ] ; } function moserDeBruijn ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo gen ( $ i ) , " ▁ " ; echo "
 " ; } $ n = 15 ; echo " First ▁ " , $ n , " ▁ terms ▁ of ▁ " , " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁
 " ; moserDeBruijn ( $ n ) ; ? >