codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum of elements from each row in the matrix | PHP Program to find row - wise maximum element sum considering elements in increasing order . ; Function to perform given task ; Getting the maximum element from last row ; Comparing it with the elements of above rows ; Maximum of current row . ; If we could not an element smaller than prev_max . ; Driver code
< ? php $ N = 3 ; function getGreatestSum ( $ a ) { global $ N ; $ prev_max = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ prev_max < $ a [ $ N - 1 ] [ $ j ] ) $ prev_max = $ a [ $ N - 1 ] [ $ j ] ; $ sum = $ prev_max ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { $ curr_max = PHP_INT_MIN ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ prev_max > $ a [ $ i ] [ $ j ] and $ a [ $ i ] [ $ j ] > $ curr_max ) $ curr_max = $ a [ $ i ] [ $ j ] ; if ( $ curr_max == PHP_INT_MIN ) return -1 ; $ prev_max = $ curr_max ; $ sum += $ prev_max ; } return $ sum ; } $ a = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; echo getGreatestSum ( $ a ) , " STRNEWLINE " ; $ b = array ( array ( 4 , 5 , 6 ) , array ( 4 , 5 , 6 ) , array ( 4 , 5 , 6 ) ) ; echo getGreatestSum ( $ b ) , " STRNEWLINE " ; ? >
Value of k | PHP program to fin k - th element after append and insert middle operations ; Middle element of the sequence ; length of the resulting sequence . ; Updating the middle element of next sequence ; Moving to the left side of the middle element . ; Moving to the right side of the middle element . ; Driver code
< ? php function findElement ( $ n , $ k ) { $ ans = $ n ; $ left = 1 ; $ right = pow ( 2 , $ n ) - 1 ; while ( 1 ) { $ mid = ( $ left + $ right ) / 2 ; if ( $ k == $ mid ) { echo $ ans , " STRNEWLINE " ; break ; } $ ans -- ; if ( $ k < $ id ) $ right = $ mid - 1 ; else $ left = $ mid + 1 ; } } $ n = 4 ; $ k = 8 ; findElement ( $ n , $ k ) ; ? >
Print pair with maximum AND value in an array | Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Find the elements ; print the pair of elements ; inc count value after printing element ; return the result value ; Driver code
< ? php function checkBit ( $ pattern , $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ( $ pattern & $ arr [ $ i ] ) == $ pattern ) $ count ++ ; return $ count ; } function maxAND ( $ arr , $ n ) { $ res = 0 ; for ( $ bit = 31 ; $ bit >= 0 ; $ bit -- ) { $ count = checkBit ( $ res | ( 1 << $ bit ) , $ arr , $ n ) ; if ( $ count >= 2 ) $ res |= ( 1 << $ bit ) ; } if ( $ res == 0 ) echo " Not ▁ Possible STRNEWLINE " ; else { echo " Pair = " $ count = 0 ; for ( $ i = 0 ; $ i < $ n && $ count < 2 ; $ i ++ ) { if ( ( $ arr [ $ i ] & $ res ) == $ res ) { $ count ++ ; echo $ arr [ $ i ] . " " ; } } } return $ res ; } $ arr = array ( 4 , 8 , 6 , 2 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Maximum AND Value = " ? >
Probability of choosing a random pair with maximum sum in an array | Function to get max first and second ; If current element is smaller than first , then update both first and second ; If arr [ i ] is in between first and second then update second ; frequency of first maximum ; frequency of second maximum ; Returns probability of choosing a pair with maximum sum . ; Driver Code
< ? php function countMaxSumPairs ( $ a , $ n ) { $ first = PHP_INT_MIN ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ first ) { $ second = $ first ; $ first = $ a [ $ i ] ; } else if ( $ a [ $ i ] > $ second && $ a [ $ i ] != $ first ) $ second = $ a [ $ i ] ; } $ cnt1 = 0 ; $ cnt2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ first ) $ cnt1 ++ ; if ( $ a [ $ i ] == $ second ) $ cnt2 ++ ; } if ( $ cnt1 == 1 ) return $ cnt2 ; if ( $ cnt1 > 1 ) return $ cnt1 * ( $ cnt1 - 1 ) / 2 ; } function findMaxSumProbability ( $ a , $ n ) { $ total = $ n * ( $ n - 1 ) / 2 ; $ max_sum_pairs = countMaxSumPairs ( $ a , $ n ) ; return ( float ) $ max_sum_pairs / ( float ) $ total ; } $ a = array ( 1 , 2 , 2 , 3 ) ; $ n = sizeof ( $ a ) ; echo findMaxSumProbability ( $ a , $ n ) ; ? >
Find if given number is sum of first n natural numbers | Function to find no . of elements to be added from 1 to get n ; Start adding numbers from 1 ; If sum becomes equal to s return n ; Drivers code
< ? php function findS ( $ s ) { $ sum = 0 ; for ( $ n = 1 ; $ sum < $ s ; $ n ++ ) { $ sum += $ n ; if ( $ sum == $ s ) return $ n ; } return -1 ; } $ s = 15 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo " - 1" ; else echo $ n ; ? >
Save from Bishop in chessboard | function to calculate total safe position ; i , j denotes row and column of position of bishop ; calc distance in four direction ; calc total sum of distance + 1 for unsafe positions ; return total safe positions ; Driver Code
< ? php function calcSafe ( $ pos ) { $ j = $ pos % 10 ; $ i = $ pos / 10 ; $ dis_11 = min ( abs ( 1 - $ i ) , abs ( 1 - $ j ) ) ; $ dis_18 = min ( abs ( 1 - $ i ) , abs ( 8 - $ j ) ) ; $ dis_81 = min ( abs ( 8 - $ i ) , abs ( 1 - $ j ) ) ; $ dis_88 = min ( abs ( 8 - $ i ) , abs ( 8 - $ j ) ) ; $ sum = $ dis_11 + $ dis_18 + $ dis_81 + $ dis_88 + 1 ; return ceil ( 64 - $ sum ) ; } $ pos = 34 ; echo " Safe ▁ Positions ▁ = ▁ " , calcSafe ( $ pos ) ; ? >
Count number of elements between two given elements in array | Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements betweenthe two elements . ; Driver Code
< ? php function getCount ( $ arr , $ n , $ num1 , $ num2 ) { $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ num1 ) break ; if ( $ i >= $ n - 1 ) return 0 ; $ j ; for ( $ j = $ n - 1 ; $ j >= $ i + 1 ; $ j -- ) if ( $ arr [ $ j ] == $ num2 ) break ; if ( $ j == $ i ) return 0 ; return ( $ j - $ i - 1 ) ; } $ arr = array ( 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 ) ; $ n = sizeof ( $ arr ) ; $ num1 = 5 ; $ num2 = 4 ; echo ( getCount ( $ arr , $ n , $ num1 , $ num2 ) ) ; ? >
Count numbers with difference between number and its digit sum greater than specific value | method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit is greater than or equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ ▁ found ' and ' N ' as result ; Driver Code
< ? php function sumOfDigit ( $ K ) { $ sod = 0 ; while ( $ K ) { $ sod += $ K % 10 ; $ K /= 10 ; } return $ sod ; } function totalNumbersWithSpecificDifference ( $ N , $ diff ) { $ low = 1 ; $ high = $ N ; while ( $ low <= $ high ) { $ mid = floor ( ( $ low + $ high ) / 2 ) ; if ( $ mid - sumOfDigit ( $ mid ) < $ diff ) $ low = $ mid + 1 ; else $ high = $ mid - 1 ; } return ( $ N - $ high ) ; } $ N = 13 ; $ diff = 2 ; echo totalNumbersWithSpecificDifference ( $ N , $ diff ) ; ? >
Number of buildings facing the sun | Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver code
< ? php function countBuildings ( $ arr , $ n ) { $ count = 1 ; $ curr_max = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ curr_max $ arr [ $ i ] == $ curr_max ) { $ count ++ ; $ curr_max = $ arr [ $ i ] ; } } return $ count ; } $ arr = array ( 7 , 4 , 8 , 2 , 9 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo countBuildings ( $ arr , $ n ) ; ? >
Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays
< ? php function findExtra ( $ arr1 , $ arr2 , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr1 [ $ i ] != $ arr2 [ $ i ] ) return $ i ; return $ n ; } $ arr1 = array ( 2 , 4 , 6 , 8 , 10 , 12 , 13 ) ; $ arr2 = array ( 2 , 4 , 6 , 8 , 10 , 12 ) ; $ n = sizeof ( $ arr2 ) ; echo findExtra ( $ arr1 , $ arr2 , $ n ) ; ? >
Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left , our search is complete . ; Driver code ; Solve is passed both arrays
< ? php function findExtra ( $ arr1 , $ arr2 , $ n ) { $ index = $ n ; $ left = 0 ; $ right = $ n - 1 ; while ( $ left <= $ right ) { $ mid = ( $ left + $ right ) / 2 ; if ( $ arr2 [ $ mid ] == $ arr1 [ $ mid ] ) $ left = $ mid + 1 ; else { $ index = $ mid ; $ right = $ mid - 1 ; } } return $ index ; } { $ arr1 = array ( 2 , 4 , 6 , 8 , 10 , 12 , 13 ) ; $ arr2 = array ( 2 , 4 , 6 , 8 , 10 , 12 ) ; $ n = sizeof ( $ arr2 ) / sizeof ( $ arr2 [ 0 ] ) ; echo findExtra ( $ arr1 , $ arr2 , $ n ) ; return 0 ; } ? >
Make all array elements equal with minimum cost | Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver Code
< ? php function computeCost ( $ arr , $ N , $ X ) { $ cost = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ cost += abs ( $ arr [ $ i ] - $ X ) ; return $ cost ; } function minCostToMakeElementEqual ( $ arr , $ N ) { $ low ; $ high ; $ low = $ high = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ low > $ arr [ $ i ] ) $ low = $ arr [ $ i ] ; if ( $ high < $ arr [ $ i ] ) $ high = $ arr [ $ i ] ; } while ( ( $ high - $ low ) > 2 ) { $ mid1 = $ low + ( floor ( $ high - $ low ) / 3 ) ; $ mid2 = $ high - ( $ high - $ low ) / 3 ; $ cost1 = computeCost ( $ arr , $ N , $ mid1 ) ; $ cost2 = computeCost ( $ arr , $ N , $ mid2 ) ; if ( $ cost1 < $ cost2 ) $ high = $ mid2 ; else $ low = $ mid1 ; } return computeCost ( $ arr , $ N , ( $ low + $ high ) / 2 ) ; } $ arr = array ( 1 , 100 , 101 ) ; $ N = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo minCostToMakeElementEqual ( $ arr , $ N ) ; ? >
Smallest element greater than X not present in the array | Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code
< ? php function Next_greater ( $ a , $ n , $ x ) { sort ( $ a ) ; $ low = 0 ; $ high = $ n - 1 ; $ ans = $ x + 1 ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ a [ $ mid ] <= $ ans ) { if ( $ a [ $ mid ] == $ ans ) { $ ans ++ ; $ high = $ n - 1 ; } $ low = $ mid + 1 ; } else $ high = $ mid - 1 ; } return $ ans ; } $ a = array ( 1 , 5 , 10 , 4 , 7 ) ; $ x = 4 ; $ n = count ( $ a ) ; echo Next_greater ( $ a , $ n , $ x ) ; ? >
Count number of subsets whose median is also present in the same subset | PHP implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nCr ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
< ? php $ mod = 1000000007 ; function fact ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function nCr ( $ n , $ r ) { return fact ( $ n ) / ( fact ( $ r ) * fact ( $ n - $ r ) ) ; } function powmod ( $ a , $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; $ pt = powmod ( $ a , $ n / 2 ) ; $ pt = ( $ pt * $ pt ) % $ mod ; if ( $ n % 2 == 1 ) return ( $ pt * $ a ) % $ mod ; else return $ pt ; } function CountSubset ( $ arr , $ n ) { global $ mod ; $ ans = powmod ( 2 , $ n - 1 ) ; sort ( $ arr , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ j = $ i + 1 ; while ( $ j < $ n && $ arr [ $ j ] == $ arr [ $ i ] ) { $ r = $ n - 1 - $ j ; $ l = $ i ; $ ans = ( $ ans + nCr ( $ l + $ r , $ l ) ) % $ mod ; $ j ++ ; } } return $ ans ; } { $ arr = array ( 2 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; echo ( CountSubset ( $ arr , $ n ) ) ; }
Reorder the position of the words in alphabetical order | Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code
< ? php function reArrange ( $ words , $ n ) { $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ freq [ $ words [ $ i ] ] = ( $ i + 1 ) ; } sort ( $ words ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ freq [ $ words [ $ i ] ] , " ▁ " ; } $ words = array ( " live " , " place " , " travel " , " word " , " sky " ) ; $ n = count ( $ words ) ; reArrange ( $ words , $ n ) ; ? >
Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | PHP implementation of the approach ; Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code
< ? php $ MAX = 10000 ; function findSumofEle ( $ arr1 , $ m , $ arr2 , $ n ) { $ hash = array_fill ( 0 , $ GLOBALS [ ' MAX ' ] , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash [ $ arr2 [ $ i ] ] ++ ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' MAX ' ] ; $ i ++ ) $ hash [ $ i ] = $ hash [ $ i ] + $ hash [ $ i - 1 ] ; $ maximumFreq = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ maximumFreq = max ( $ maximumFreq , $ hash [ $ arr1 [ $ i ] ] ) ; $ sumOfElements = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ sumOfElements += ( $ maximumFreq == $ hash [ $ arr1 [ $ i ] ] ) ? $ arr1 [ $ i ] : 0 ; return $ sumOfElements ; } $ arr1 = array ( 2 , 5 , 6 , 8 ) ; $ arr2 = array ( 4 , 10 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; echo findSumofEle ( $ arr1 , $ m , $ arr2 , $ n ) ; ? >
Find A and B from list of divisors | Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code
< ? php function printNumbers ( $ arr , $ n ) { sort ( $ arr ) ; $ A = $ arr [ $ n - 1 ] ; $ B = -1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ A % $ arr [ $ i ] != 0 ) { $ B = $ arr [ $ i ] ; break ; } if ( $ i - 1 >= 0 && $ arr [ $ i ] == $ arr [ $ i - 1 ] ) { $ B = $ arr [ $ i ] ; break ; } } echo ( " A ▁ = ▁ " . $ A . " , B = " } $ arr = array ( 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; printNumbers ( $ arr , $ n ) ;
Find the modified array after performing k operations of given type | Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code
< ? php function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function removeMin ( & $ arr , $ n ) { $ minVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minVal = min ( $ minVal , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i ] - $ minVal ; } function removeFromMax ( & $ arr , $ n ) { $ maxVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ maxVal = max ( $ maxVal , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ maxVal - $ arr [ $ i ] ; } function modifyArray ( $ arr , $ n , $ k ) { if ( $ k % 2 == 0 ) removeMin ( $ arr , $ n ) ; else removeFromMax ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; } $ arr = array ( 4 , 8 , 12 , 16 ) ; $ n = count ( $ arr ) ; $ k = 2 ; modifyArray ( $ arr , $ n , $ k ) ; ? >
Minimize the sum of the squares of the sum of elements of each group the array is divided into | Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code
< ? php function findAnswer ( $ n , $ arr ) { sort ( $ arr ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n / 2 ; ++ $ i ) { $ sum += ( $ arr [ $ i ] + $ arr [ $ n - $ i - 1 ] ) * ( $ arr [ $ i ] + $ arr [ $ n - $ i - 1 ] ) ; } return $ sum ; } $ arr = array ( 53 , 28 , 143 , 5 ) ; $ n = count ( $ arr ) ; echo findAnswer ( $ n , $ arr ) ; ? >
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | PHP program to merge K sorted arrays ; Function to perform merge operation ; to store the starting point of left and right array ; to store the size of left and right array ; array to temporarily store left and right array ; storing data in left array ; storing data in right array ; to store the current index of temporary left and right array ; to store the current index for output array ; two pointer merge for two sorted arrays ; Code to drive merge - sort and create recursion tree ; base step to initialize the output array before performing merge operation ; to sort left half ; to sort right half ; merge the left and right half ; input 2D - array ; Number of arrays ; Output array ; Print merged array
< ? php $ n = 4 ; function merge ( $ l , $ r , & $ output ) { global $ n ; $ l_in = $ l * $ n ; $ r_in = ( ( int ) ( ( $ l + $ r ) / 2 ) + 1 ) * $ n ; $ l_c = ( int ) ( ( ( ( $ l + $ r ) / 2 ) - $ l + 1 ) * $ n ) ; $ r_c = ( $ r - ( int ) ( ( $ l + $ r ) / 2 ) ) * $ n ; $ l_arr = array_fill ( 0 , $ l_c , 0 ) ; $ r_arr = array_fill ( 0 , $ r_c , 0 ) ; for ( $ i = 0 ; $ i < $ l_c ; $ i ++ ) $ l_arr [ $ i ] = $ output [ $ l_in + $ i ] ; for ( $ i = 0 ; $ i < $ r_c ; $ i ++ ) $ r_arr [ $ i ] = $ output [ $ r_in + $ i ] ; $ l_curr = 0 ; $ r_curr = 0 ; $ in = $ l_in ; while ( $ l_curr + $ r_curr < $ l_c + $ r_c ) { if ( $ r_curr == $ r_c || ( $ l_curr != $ l_c && $ l_arr [ $ l_curr ] < $ r_arr [ $ r_curr ] ) ) { $ output [ $ in ] = $ l_arr [ $ l_curr ] ; $ l_curr ++ ; $ in ++ ; } else { $ output [ $ in ] = $ r_arr [ $ r_curr ] ; $ r_curr ++ ; $ in ++ ; } } } function divide ( $ l , $ r , & $ output , $ arr ) { global $ n ; if ( $ l == $ r ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ output [ $ l * $ n + $ i ] = $ arr [ $ l ] [ $ i ] ; return ; } divide ( $ l , ( int ) ( ( $ l + $ r ) / 2 ) , $ output , $ arr ) ; divide ( ( int ) ( ( $ l + $ r ) / 2 ) + 1 , $ r , $ output , $ arr ) ; merge ( $ l , $ r , $ output ) ; } $ arr = array ( array ( 5 , 7 , 15 , 18 ) , array ( 1 , 8 , 9 , 17 ) , array ( 1 , 4 , 7 , 7 ) ) ; $ k = count ( $ arr ) ; $ output = array_fill ( 0 , $ n * $ k , 0 ) ; divide ( 0 , $ k - 1 , $ output , $ arr ) ; for ( $ i = 0 ; $ i < $ n * $ k ; $ i ++ ) print ( $ output [ $ i ] . " ▁ " ) ; ? >
Count distinct elements in an array | PHP program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver Code
< ? php function countDistinct ( & $ arr , $ n ) { $ res = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) break ; if ( $ i == $ j ) $ res ++ ; } return $ res ; } $ arr = array ( 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ) ; $ n = count ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >
Count distinct elements in an array | PHP program to count all distinct elements in a given array ; First sort the array so that all occurrences become consecutive ; Traverse the sorted array ; Move the index ahead while there are duplicates ; Driver Code
< ? php function countDistinct ( $ arr , $ n ) { sort ( $ arr , 0 ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ i < $ n - 1 && $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ i ++ ; $ res ++ ; } return $ res ; } $ arr = array ( 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ) ; $ n = sizeof ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >
Choose n elements such that their mean is maximum | Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code
< ? php function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function printMaxMean ( $ arr , $ n ) { $ newArr [ $ n ] = array ( ) ; sort ( $ arr , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ newArr [ $ i ] = $ arr [ $ i + $ n ] ; printArray ( $ newArr , $ n ) ; } $ arr = array ( 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ) ; $ n = sizeof ( $ arr ) ; printMaxMean ( $ arr , $ n / 2 ) ;
Average of remaining elements after removing K largest and K smallest elements from array | Function to find average ; base case if 2 * k >= n means all element get removed ; first sort all elements ; sum of req number ; find average ; Driver code
< ? php function average ( $ arr , $ n , $ k ) { $ total = 0 ; if ( 2 * $ k >= $ n ) return 0 ; sort ( $ arr ) ; $ start = $ k ; $ end = $ n - $ k - 1 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) $ total += $ arr [ $ i ] ; return ( $ total / ( $ n - 2 * $ k ) ) ; } $ arr = array ( 1 , 2 , 4 , 4 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; echo average ( $ arr , $ n , $ k ) ; ? >