codeparrot/xlcost-text-to-code huggingface dataset:
Find the Side of the smallest Square that can contain given 4 Big Squares | Function to find the maximum of two values ; Function to find the smallest side of the suitable suitcase ; sort array to find the smallest and largest side of suitcases ; side of the suitcase will be smallest if they arranged in 2 x 2 way so find all possible sides of that arrangement ; since suitcase should be square so find maximum of all four side ; now find greatest side and that will be the smallest square ; return the result ; Driver program ; Get the side of the 4 small squares ; Find the smallest side ; Get the side of the 4 small squares ; Find the smallest side
< ? php function max1 ( $ a , $ b ) { if ( $ a > $ b ) return $ a ; else return $ b ; } function smallestSide ( $ a ) { sort ( $ a , 0 ) ; $ side1 = $ a [ 0 ] + $ a [ 3 ] ; $ side2 = $ a [ 1 ] + $ a [ 2 ] ; $ side3 = $ a [ 0 ] + $ a [ 1 ] ; $ side4 = $ a [ 2 ] + $ a [ 3 ] ; $ side11 = max1 ( $ side1 , $ side2 ) ; $ side12 = max1 ( $ side3 , $ side4 ) ; $ sideOfSquare = max1 ( $ side11 , $ side12 ) ; return $ sideOfSquare ; } $ side = array ( ) ; echo " Test ▁ Case ▁ 1 STRNEWLINE " ; $ side [ 0 ] = 2 ; $ side [ 1 ] = 2 ; $ side [ 2 ] = 2 ; $ side [ 3 ] = 2 ; echo smallestSide ( $ side ) . " STRNEWLINE " ; echo " Test Case 2 " ; $ side [ 0 ] = 100000000000000 ; $ side [ 1 ] = 123450000000000 ; $ side [ 2 ] = 987650000000000 ; $ side [ 3 ] = 987654321000000 ; echo smallestSide ( $ side ) . " STRNEWLINE " ; ? >
Rectangle with minimum possible difference between the length and the width | Function to print the length ( l ) and breadth ( b ) of the rectangle having area = N and | l - b | as minimum as possible ; i is a factor ; l >= sqrt ( area ) >= i ; so here l is + ve always ; Here l and b are length and breadth of the rectangle ; Driver code
< ? php function find_rectangle ( $ area ) { $ M = floor ( sqrt ( $ area ) ) ; for ( $ i = $ M ; $ i >= 1 ; $ i -- ) { if ( $ area % $ i == 0 ) { $ l = floor ( $ area / $ i ) ; $ b = $ i ; break ; } } echo " l = " , ▁ $ l , ▁ " , b = " , ▁ $ b , ▁ " " } $ area = 99 ; find_rectangle ( $ area ) ; ? >
Largest sub | Function to return the size of the required sub - set ; Sort the array ; Set to store the contents of the required sub - set ; Insert the elements satisfying the conditions ; Return the size of the set ; Driver code
< ? php function sizeSubSet ( $ a , $ k , $ n ) { sort ( $ a ) ; $ s = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % $ k != 0 or ! in_array ( floor ( $ a [ $ i ] / $ k ) , $ s ) ) array_push ( $ s , $ a [ $ i ] ) ; } return sizeof ( $ s ) ; } $ a = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = sizeof ( $ a ) ; $ k = 2 ; echo sizeSubSet ( $ a , $ k , $ n ) ; ? >
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
< ? php function minimumSets ( $ s , $ y ) { $ cnt = 0 ; $ num = 0 ; $ l = strlen ( $ s ) ; $ f = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { $ num = $ num * 10 + ( $ s [ $ i ] - '0' ) ; if ( $ num <= $ y ) $ f = 1 ; { if ( $ f ) $ cnt += 1 ; $ num = $ s [ $ i ] - '0' ; $ f = 0 ; if ( $ num <= $ y ) $ f = 1 ; else $ num = 0 ; } } if ( $ f ) $ cnt += 1 ; return $ cnt ; } $ s = "1234" ; $ y = 30 ; echo ( minimumSets ( $ s , $ y ) ) ; ? >
Find the non decreasing order array from given array | Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code
< ? php function printArr ( $ b , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ b [ $ i ] . " ▁ " ; } function ModifiedArray ( $ a , $ n ) { $ l = 0 ; $ r = PHP_INT_MAX ; $ b = array ( 0 ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { $ b [ $ i ] = max ( $ l , $ a [ $ i ] - $ r ) ; $ b [ $ n - $ i - 1 ] = $ a [ $ i ] - $ b [ $ i ] ; $ l = $ b [ $ i ] ; $ r = $ b [ $ n - $ i - 1 ] ; } printArr ( $ b , $ n ) ; } $ a = array ( 5 , 6 ) ; $ n = sizeof ( $ a ) ; ModifiedArray ( $ a , 2 * $ n ) ; ? >
Maximum sum of all elements of array after performing given operations | Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } function maxSum ( $ a , $ n ) { $ S = 0 ; $ S1 = maxSubArraySum ( $ a , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ S += $ a [ $ i ] ; return ( 2 * $ S1 - $ S ) ; } $ a = array ( -35 , 32 , -24 , 0 , 27 , -10 , 0 , -19 ) ; $ n = sizeof ( $ a ) ; echo ( maxSum ( $ a , $ n ) ) ;
Minimize the difference between minimum and maximum elements | Function to minimize the difference between minimum and maximum elements ; Find max and min elements of the array ; Check whether the difference between the max and min element is less than or equal to k or not ; Calculate average of max and min ; If the array element is greater than the average then decrease it by k ; If the array element is smaller than the average then increase it by k ; Find max and min of the modified array ; return the new difference ; Driver code
< ? php function minimizeDiff ( & $ arr , $ n , $ k ) { $ max = max ( $ arr ) ; $ min = min ( $ arr ) ; if ( ( $ max - $ min ) <= $ k ) { return ( $ max - $ min ) ; } $ avg = ( $ max + $ min ) / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ avg ) $ arr [ $ i ] -= $ k ; else $ arr [ $ i ] += $ k ; } $ max = max ( $ arr ) ; $ min = min ( $ arr ) ; return ( $ max - $ min ) ; } $ arr = array ( 3 , 16 , 12 , 9 , 20 ) ; $ n = 5 ; $ k = 3 ; echo " Max ▁ height ▁ difference ▁ = ▁ " . minimizeDiff ( $ arr , $ n , $ k ) . " STRNEWLINE " ; ? >
Maximum litres of water that can be bought with N Rupees | PHP implementation of the above approach ; if buying glass bottles is profitable ; Glass bottles that can be bought ; Change budget according the bought bottles ; Plastic bottles that can be bought ; if only plastic bottles need to be bought ; Driver Code
< ? php function maxLitres ( $ budget , $ plastic , $ glass , $ refund ) { if ( $ glass - $ refund < $ plastic ) { $ ans = max ( ( int ) ( $ budget - $ refund ) / ( $ glass - $ refund ) , 0 ) ; $ budget -= $ ans * ( $ glass - $ refund ) ; $ ans += ( int ) ( $ budget / $ plastic ) ; echo $ ans . " STRNEWLINE " ; } else echo ( int ) ( $ budget / $ plastic ) . " STRNEWLINE " ; } $ budget = 10 ; $ plastic = 11 ; $ glass = 9 ; $ refund = 8 ; maxLitres ( $ budget , $ plastic , $ glass , $ refund ) ; ? >
Find number from given list for which value of the function is closest to A | Function to find number from given list for which value of the function is closest to A ; Stores the final index ; Declaring a variable to store the minimum absolute difference ; Finding F ( n ) ; Updating the index of the answer if new absolute difference is less than tmp ; Driver Code
< ? php function leastValue ( $ P , $ A , $ N , $ a ) { $ ans = -1 ; $ tmp = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ t = $ P - $ a [ $ i ] * 0.006 ; if ( abs ( $ t - $ A ) < $ tmp ) { $ tmp = abs ( $ t - $ A ) ; $ ans = $ i ; } } return $ a [ $ ans ] ; } $ N = 2 ; $ P = 12 ; $ A = 5 ; $ a = array ( 1000 , 2000 ) ; print ( leastValue ( $ P , $ A , $ N , $ a ) ) ; ? >
Find permutation of n which is divisible by 3 but not divisible by 6 | Function to find the permutation ; length of integer ; if integer is even ; return odd integer ; rotate integer ; return - 1 in case no required permutation exists ; Driver Code
< ? php function findPermutation ( $ n ) { $ len = ceil ( log10 ( $ n ) ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ n % 2 != 0 ) { return ( int ) $ n ; } else { $ n = ( $ n / 10 ) + ( $ n % 10 ) * pow ( 10 , $ len - $ i - 1 ) ; continue ; } } return -1 ; } $ n = 132 ; echo findPermutation ( $ n ) ; ? >
Check if it is possible to serve customer queue with different notes | Function that returns true is selling of the tickets is possible ; Nothing to return to the customer ; Check if 25 can be returned to customer . ; Try returning one 50 and one 25 ; Try returning three 25 ; If the loop did not break , all the tickets were sold ; Driver Code
< ? php function isSellingPossible ( $ n , $ a ) { $ c25 = 0 ; $ c50 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 25 ) $ c25 ++ ; else if ( $ a [ $ i ] == 50 ) { $ c50 ++ ; if ( $ c25 == 0 ) break ; $ c25 -- ; } else { if ( $ c50 > 0 && $ c25 > 0 ) { $ c50 -- ; $ c25 -- ; } else if ( $ c25 >= 3 ) $ c25 -= 3 ; else break ; } } if ( $ i == $ n ) return true ; else return false ; } $ a = array ( 25 , 25 , 50 , 100 ) ; $ n = sizeof ( $ a ) ; if ( isSellingPossible ( $ n , $ a ) ) { echo " YES " ; } else { echo " NO " ; } ? >
Check if a cell can be visited more than once in a String | Function to check if any cell can be visited more than once ; Array to mark cells ; Traverse the string ; Increase the visit count of the left and right cells within the array which can be visited ; If any cell can be visited more than once , Return True ; Driver code
< ? php function checkIfOverlap ( $ str ) { $ len = strlen ( $ str ) ; $ visited = array_fill ( 0 , $ len + 1 , NULL ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == ' . ' ) continue ; for ( $ j = max ( 0 , $ i - $ str [ $ i ] ) ; $ j <= min ( $ len , $ i + $ str [ $ i ] ) ; $ j ++ ) $ visited [ $ j ] ++ ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ visited [ $ i ] > 1 ) { return true ; } } return false ; } $ str = " . 2 . . 2 . " ; if ( checkIfOverlap ( $ str ) ) echo " YES " ; else echo " NO " ; ? >
Check if a number has digits in the given Order | Check if the digits follow the correct order ; to store the previous digit ; pointer to tell what type of sequence are we dealing with ; check if we have same digit as the previous digit ; checking the peak point of the number ; check if we have same digit as the previous digit ; check if the digit is greater than the previous one If true , then break from the loop as we are in descending order part ; Driver code
< ? php function isCorrectOrder ( $ n ) { $ flag = true ; $ prev = -1 ; $ type = -1 ; while ( $ n != 0 ) { if ( $ type == -1 ) { if ( $ prev == -1 ) { $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; continue ; } if ( $ prev == $ n % 10 ) { $ flag = false ; break ; } if ( $ prev > $ n % 10 ) { $ type = 1 ; $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; continue ; } $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; } else { if ( $ prev == $ n % 10 ) { $ flag = false ; break ; } if ( $ prev < $ n % 10 ) { $ flag = false ; break ; } $ prev = $ n % 10 ; $ n = ( int ) $ n / 10 ; } } return $ flag ; } $ n = 123454321 ; if ( isCorrectOrder ( $ n ) ) echo " YES " ; else echo " NO " ; ? >
Coin game of two corners ( Greedy Approach ) | Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Find sum of odd positioned coins ; Find sum of even positioned coins ; Print even or odd coins depending upon which sum is greater . ; Driver Code
< ? php function printCoins ( & $ arr , $ n ) { $ oddSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 2 ) $ oddSum += $ arr [ $ i ] ; $ evenSum = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i += 2 ) $ evenSum += $ arr [ $ i ] ; $ start = ( ( $ oddSum > $ evenSum ) ? 0 : 1 ) ; for ( $ i = $ start ; $ i < $ n ; $ i += 2 ) echo $ arr [ $ i ] . " ▁ " ; } $ arr1 = array ( 8 , 15 , 3 , 7 ) ; $ n = sizeof ( $ arr1 ) ; printCoins ( $ arr1 , $ n ) ; echo " STRNEWLINE " ; $ arr2 = array ( 2 , 2 , 2 , 2 ) ; $ n = sizeof ( $ arr2 ) ; printCoins ( $ arr2 , $ n ) ; echo " STRNEWLINE " ; $ arr3 = array ( 20 , 30 , 2 , 2 , 2 , 10 ) ; $ n = sizeof ( $ arr3 ) ; printCoins ( $ arr3 , $ n ) ; ? >
Final cell position in the matrix | function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code
< ? php function finalPos ( $ command , $ n , $ x , $ y ) { $ cup ; $ cdown ; $ cleft ; $ cright ; $ final_x ; $ final_y ; $ cup = $ cdown = $ cleft = $ cright = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ command [ $ i ] == ' U ' ) $ cup ++ ; else if ( $ command [ $ i ] == ' D ' ) $ cdown ++ ; else if ( $ command [ $ i ] == ' L ' ) $ cleft ++ ; else if ( $ command [ $ i ] == ' R ' ) $ cright ++ ; } $ final_x = $ x + ( $ cright - $ cleft ) ; $ final_y = $ y + ( $ cdown - $ cup ) ; echo " Final ▁ Position : ▁ " . " ( " . $ final_x . " , ▁ " . $ final_y . " ) " ; } $ command = " DDLRULL " ; $ n = strlen ( $ command ) ; $ x = 3 ; $ y = 4 ; finalPos ( $ command , $ n , $ x , $ y ) ;
Smallest sum contiguous subarray | Set | Function to find the smallest sum contiguous subarray ; First invert the sign of the elements ; Apply the normal Kadane algorithm but on the elements of the array having inverted sign ; Invert the answer to get minimum val ; Driver Code
< ? php function smallestSumSubarr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = - $ arr [ $ i ] ; $ sum_here = $ arr [ 0 ] ; $ max_sum = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum_here = max ( $ sum_here + $ arr [ $ i ] , $ arr [ $ i ] ) ; $ max_sum = max ( $ max_sum , $ sum_here ) ; } return ( -1 ) * $ max_sum ; } $ arr = array ( 3 , -4 , 2 , -3 , -1 , 7 , -5 ) ; $ n = sizeof ( $ arr ) ; echo " Smallest ▁ sum : ▁ " , smallestSumSubarr ( $ arr , $ n ) ; ? >
Maximum number by concatenating every element in a rotation of an array | Function to print the largest number ; store the index of largest left most digit of elements ; Iterate for all numbers ; check for the the last digit ; check for the largest left most digit ; print the rotation of array ; print the rotation of array ; Driver Code
< ? php function printLargest ( $ a , $ n ) { $ max = -1 ; $ ind = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ num = $ a [ $ i ] ; while ( $ num ) { $ r = $ num % 10 ; $ num = ( int ) $ num / 10 ; if ( $ num == 0 ) { if ( $ max < $ r ) { $ max = $ r ; $ ind = $ i ; } } } } for ( $ i = $ ind ; $ i < $ n ; $ i ++ ) echo $ a [ $ i ] ; for ( $ i = 0 ; $ i < $ ind ; $ i ++ ) echo $ a [ $ i ] ; } $ a = array ( 54 , 546 , 548 , 60 ) ; $ n = sizeof ( $ a ) ; printLargest ( $ a , $ n ) ; ? >
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
< ? 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 ) ; ? >
Maximum product subset of an array | PHP program to find maximum product of a subset . ; Find count of negative numbers , count of zeros , negative number with least absolute value and product of non - zero numbers ; If number is 0 , we don 't  multiply it with product. ; Count negatives and keep track of negative number with least absolute value . ; If there are all zeros ; If there are odd number of negative numbers ; Exceptional case : There is only negative and all other are zeros ; Otherwise result is product of all non - zeros divided by negative number with least absolute value . ; Driver Code
< ? php function maxProductSubset ( $ a , $ n ) { if ( $ n == 1 ) return $ a [ 0 ] ; $ max_neg = PHP_INT_MIN ; $ count_neg = 0 ; $ count_zero = 0 ; $ prod = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 0 ) { $ count_zero ++ ; continue ; } if ( $ a [ $ i ] < 0 ) { $ count_neg ++ ; $ max_neg = max ( $ max_neg , $ a [ $ i ] ) ; } $ prod = $ prod * $ a [ $ i ] ; } if ( $ count_zero == $ n ) return 0 ; if ( $ count_neg & 1 ) { if ( $ count_neg == 1 && $ count_zero > 0 && $ count_zero + $ count_neg == $ n ) return 0 ; $ prod = $ prod / $ max_neg ; } return $ prod ; } $ a = array ( -1 , -1 , -2 , 4 , 3 ) ; $ n = sizeof ( $ a ) ; echo maxProductSubset ( $ a , $ n ) ; ? >
Smallest number with sum of digits as N and divisible by 10 ^ N | PHP program to find smallest number to find smallest number with N as sum of digits and divisible by 10 ^ N . ; If N = 0 the string will be 0 ; If n is not perfectly divisible by 9 output the remainder ; Print 9 N / 9 times ; Append N zero 's to the number so  as to make it divisible by 10^N ; Driver Code
< ? php function digitsNum ( $ N ) { if ( $ N == 0 ) echo "0 STRNEWLINE " ; if ( $ N % 9 != 0 ) echo ( $ N % 9 ) ; for ( $ i = 1 ; $ i <= ( $ N / 9 ) ; ++ $ i ) echo "9" ; for ( $ i = 1 ; $ i <= $ N ; ++ $ i ) echo "0" ; echo " STRNEWLINE " ; } $ N = 5 ; echo " The ▁ number ▁ is ▁ : ▁ " ; digitsNum ( $ N ) ; ? >
Divide 1 to n into two groups with minimum sum difference | To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group 1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver Code
< ? php function printVector ( $ v ) { echo count ( $ v ) . " STRNEWLINE " ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) echo $ v [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } function findTwoGroup ( $ n ) { $ sum = $ n * ( $ n + 1 ) / 2 ; $ group1Sum = ( int ) ( $ sum / 2 ) ; $ group1 ; $ group2 ; $ x = 0 ; $ y = 0 ; for ( $ i = $ n ; $ i > 0 ; $ i -- ) { if ( $ group1Sum - $ i >= 0 ) { $ group1 [ $ x ++ ] = $ i ; $ group1Sum -= $ i ; } else { $ group2 [ $ y ++ ] = $ i ; } } printVector ( $ group1 ) ; printVector ( $ group2 ) ; } $ n = 5 ; findTwoGroup ( $ n ) ; ? >
Partition into two subarrays of lengths k and ( N | Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver Code
< ? php function maxDifference ( $ arr , $ N , $ k ) { $ M ; $ S = 0 ; $ S1 = 0 ; $ max_difference = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ S += $ arr [ $ i ] ; rsort ( $ arr ) ; $ M = max ( $ k , $ N - $ k ) ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) $ S1 += $ arr [ $ i ] ; $ max_difference = $ S1 - ( $ S - $ S1 ) ; return $ max_difference ; } $ arr = array ( 8 , 4 , 5 , 2 , 10 ) ; $ N = count ( $ arr ) ; $ k = 2 ; echo maxDifference ( $ arr , $ N , $ k ) ; ? >
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
< ? php function minproduct ( $ a , $ b , $ n , $ k ) { $ diff = 0 ; $ res = 0 ; $ temp ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ pro = $ a [ $ i ] * $ b [ $ i ] ; $ res = $ res + $ pro ; if ( $ pro < 0 and $ b [ $ i ] < 0 ) $ temp = ( $ a [ $ i ] + 2 * $ k ) * $ b [ $ i ] ; else if ( $ pro < 0 and $ a [ $ i ] < 0 ) $ temp = ( $ a [ $ i ] - 2 * $ k ) * $ b [ $ i ] ; else if ( $ pro > 0 and $ a [ $ i ] < 0 ) $ temp = ( $ a [ $ i ] + 2 * $ k ) * $ b [ $ i ] ; else if ( $ pro > 0 and $ a [ $ i ] > 0 ) $ temp = ( $ a [ $ i ] - 2 * $ k ) * $ b [ $ i ] ; $ d = abs ( $ pro - $ temp ) ; if ( $ d > $ diff ) $ diff = $ d ; } return $ res - $ diff ; } $ a = array ( 2 , 3 , 4 , 5 , 4 , 0 ) ; $ b = array ( 3 , 4 , 2 , 3 , 2 ) ; $ n = 5 ; $ k = 3 ; echo minproduct ( $ a , $ b , $ n , $ k ) ; ? >
Split n into maximum composite numbers | function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; driver program to test the above function
< ? php function c_ount ( $ n ) { if ( $ n < 4 ) return -1 ; $ rem = $ n % 4 ; if ( $ rem == 0 ) return $ n / 4 ; if ( $ rem == 1 ) { if ( $ n < 9 ) return -1 ; return ( $ n - 9 ) / 4 + 1 ; } if ( $ rem == 2 ) return ( $ n - 6 ) / 4 + 1 ; if ( $ rem == 3 ) { if ( $ n < 15 ) return -1 ; return ( $ n - 15 ) / 4 + 2 ; } } $ n = 90 ; echo c_ount ( $ n ) , " STRNEWLINE " ; $ n = 143 ; echo c_ount ( $ n ) ; ? >
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
< ? 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 cost for acquiring all coins with k extra coins allowed with every coin | Converts coin [ ] to prefix sum array ; sort the coins value ; Maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver Code
< ? php function preprocess ( & $ coin , $ n ) { sort ( $ coin ) ; for ( $ i = 1 ; $ i <= $ n - 1 ; $ i ++ ) $ coin [ $ i ] += $ coin [ $ i - 1 ] ; } function minCost ( & $ coin , $ n , $ k ) { $ coins_needed = ceil ( 1.0 * $ n / ( $ k + 1 ) ) ; return $ coin [ $ coins_needed - 1 ] ; } $ coin = array ( 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ) ; $ n = sizeof ( $ coin ) ; preprocess ( $ coin , $ n ) ; $ k = 3 ; echo minCost ( $ coin , $ n , $ k ) . " STRNEWLINE " ; $ k = 7 ; echo minCost ( $ coin , $ n , $ k ) . " STRNEWLINE " ; ? >