codeparrot/xlcost-text-to-code huggingface dataset:
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
< ? php function min_sum ( $ n , $ k , $ a ) { sort ( $ a ) ; if ( $ a [ 0 ] < 0 ) return -1 ; if ( $ k == 0 ) { if ( $ a [ 0 ] == $ a [ $ n - 1 ] ) return ( $ n * $ a [ 0 ] ) ; else return -1 ; } else { $ f = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ p = $ a [ $ i ] - $ a [ 0 ] ; if ( $ p % $ k == 0 ) continue ; else { $ f = 1 ; break ; } } if ( $ f ) return -1 ; else { if ( $ k == 1 ) return $ n ; else return ( $ n * ( $ a [ 0 ] % $ k ) ) ; } } } $ arr = array ( 2 , 3 , 4 , 5 ) ; $ K = 1 ; $ N = count ( $ arr ) ; echo min_sum ( $ N , $ K , $ arr ) ; ? >
Minimum Increment / decrement to make array elements equal | Function to return minimum operations need to be make each element of array equal ; Initialize cost to 0 ; Sort the array ; Middle element ; Find Cost ; If n , is even . Take minimum of the Cost obtained by considering both middle elements ; Find cost again ; Take minimum of two cost ; Return total cost ; Driver Code
< ? php function minCost ( $ A , $ n ) { $ cost = 0 ; sort ( $ A ) ; $ K = $ A [ $ n / 2 ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ cost += abs ( $ A [ $ i ] - $ K ) ; if ( $ n % 2 == 0 ) { $ tempCost = 0 ; $ K = $ A [ ( $ n / 2 ) - 1 ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ tempCost += abs ( $ A [ $ i ] - $ K ) ; $ cost = min ( $ cost , $ tempCost ) ; } return $ cost ; } $ A = array ( 1 , 6 , 7 , 10 ) ; $ n = sizeof ( $ A ) ; echo minCost ( $ A , $ n ) ; ? >
Print array elements in alternatively increasing and decreasing order | Function to print array elements in alternative increasing and decreasing order ; First sort the array in increasing order ; start with 2 elements in increasing order ; till all the elements are not printed ; printing the elements in increasing order ; else printing the elements in decreasing order ; increasing the number of elements to printed in next iteration ; Driver Code
< ? php function printArray ( $ arr , $ n ) { sort ( $ arr ) ; $ l = 0 ; $ r = $ n - 1 ; $ flag = 0 ; $ k = 2 ; while ( $ l <= $ r ) { if ( $ flag == 0 ) { for ( $ i = $ l ; $ i < $ l + $ k && $ i <= $ r ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; $ flag = 1 ; $ l = $ i ; } { for ( $ i = $ r ; $ i > $ r - $ k && $ i >= $ l ; $ i -- ) echo $ arr [ $ i ] , " ▁ " ; $ flag = 0 ; $ r = $ i ; } $ k ++ ; } } $ n = 6 ; $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 ) ; printArray ( $ arr , $ n ) ; ? >
Check if the characters of a given string are in alphabetical order | Function that checks whether the string is in alphabetical order or not ; if element at index ' i ' is less than the element at index ' i - 1' then the string is not sorted ; Driver code ; check whether the string is in alphabetical order or not
< ? php function isAlphabaticOrder ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] < $ s [ $ i - 1 ] ) return false ; } return true ; } $ s = " aabbbcc " ; if ( isAlphabaticOrder ( $ s ) ) echo " Yes " ; else echo " No " ; ? >
Check if it is possible to rearrange rectangles in a non | Function to check if it possible to form rectangles with heights as non - ascending ; set maximum ; replace the maximum with previous maximum ; replace the minimum with previous minimum ; print NO if the above two conditions fail at least once ; initialize the number of rectangles ; initialize n rectangles with length and breadth
< ? php function rotateRec ( $ n , $ L , $ B ) { $ m = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( max ( $ L [ $ i ] , $ B [ $ i ] ) <= $ m ) $ m = max ( $ L [ $ i ] , $ B [ $ i ] ) ; else if ( min ( $ L [ $ i ] , $ B [ $ i ] ) <= $ m ) $ m = min ( $ L [ $ i ] , $ B [ $ i ] ) ; else { return 0 ; } } return 1 ; } $ n = 3 ; $ L = array ( 5 , 5 , 6 ) ; $ B = array ( 6 , 7 , 8 ) ; if ( rotateRec ( $ n , $ L , $ B ) == 1 ) echo " YES " ; else echo " NO " ; ? >
Find a point such that sum of the Manhattan distances is minimized | Function to print the required points which minimizes the sum of Manhattan distances ; Sorting points in all k dimension ; Output the required k points ; Driver code ; function call to print required points
< ? php function minDistance ( $ n , $ k , & $ point ) { for ( $ i = 0 ; $ i < $ k ; ++ $ i ) sort ( $ point [ $ i ] ) ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) echo $ point [ $ i ] [ ( ceil ( ( double ) $ n / 2 ) - 1 ) ] . " ▁ " ; } $ n = 4 ; $ k = 4 ; $ point = array ( array ( 1 , 5 , 2 , 4 ) , array ( 6 , 2 , 0 , 6 ) , array ( 9 , 5 , 1 , 3 ) , array ( 6 , 7 , 5 , 9 ) ) ; minDistance ( $ n , $ k , $ point ) ; ? >
Sort first k values in ascending order and remaining n | Function to sort the array ; Store the k elements in an array ; Store the remaining n - k elements in an array ; sorting the array from 0 to k - 1 places ; sorting the array from k to n places ; storing the values in the final array arr ; printing the array ; Driver code
< ? php function printOrder ( $ arr , $ n , $ k ) { $ len1 = $ k ; $ len2 = $ n - $ k ; $ arr1 = array_fill ( 0 , $ k , 0 ) ; $ arr2 = array_fill ( 0 , ( $ n - $ k ) , 0 ) ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ arr1 [ $ i ] = $ arr [ $ i ] ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) $ arr2 [ $ i - $ k ] = $ arr [ $ i ] ; sort ( $ arr1 ) ; sort ( $ arr2 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ i < $ k ) $ arr [ $ i ] = $ arr1 [ $ i ] ; else { $ arr [ $ i ] = $ arr2 [ $ len2 - 1 ] ; $ len2 -= 1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) print ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , -1 ) ; $ k = 4 ; $ n = count ( $ arr ) ; printOrder ( $ arr , $ n , $ k ) ; ? >
Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in descending order to print the required number ; Print the number of times a digits occurs ; Driver code
< ? php function findMaxNum ( $ arr , $ n ) { $ hash = array_fill ( 0 , 10 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash [ $ arr [ $ i ] ] += 1 ; for ( $ i = 9 ; $ i >= 0 ; $ i -- ) for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 0 ) ; $ n = sizeof ( $ arr ) ; findMaxNum ( $ arr , $ n ) ; ? >
Equally divide into two sets such that one set has maximum distinct elements | PHP program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Driver code
< ? php function distribution ( $ arr , $ n ) { sort ( $ arr ) ; $ count = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] ) $ count ++ ; return min ( $ count , $ n / 2 ) ; } $ arr = array ( 1 , 1 , 2 , 1 , 3 , 4 ) ; $ n = count ( $ arr ) ; echo ( distribution ( $ arr , $ n ) ) ; ? >
Sort 3 numbers | PHP program to sort an array of size 3
< ? php $ a = array ( 10 , 12 , 5 ) ; sort ( $ a ) ; for ( $ i = 0 ; $ i < 3 ; $ i ++ ) echo $ a [ $ i ] , " ▁ " ; ? >
Print triplets with sum less than k | A Simple PHP program to count triplets with sum smaller than a given value ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code
< ? php function printTriplets ( & $ arr , $ n , $ sum ) { for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n - 1 ; $ j ++ ) { for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] < $ sum ) { echo ( $ arr [ $ i ] ) ; echo ( " , ▁ " ) ; echo ( $ arr [ $ j ] ) ; echo ( " , ▁ " ) ; echo ( $ arr [ $ k ] ) ; echo ( " STRNEWLINE " ) ; } } } } $ arr = array ( 5 , 1 , 3 , 4 , 7 ) ; $ n = sizeof ( $ arr ) ; $ sum = 12 ; printTriplets ( $ arr , $ n , $ sum ) ; ? >
Count number of triplets in an array having sum in the range [ a , b ] | Function to count triplets ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code
< ? php function countTriplets ( $ arr , $ n , $ a , $ b ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n - 1 ; $ j ++ ) { for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] >= $ a && $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] <= $ b ) $ ans ++ ; } } return $ ans ; } $ arr = array ( 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ) ; $ n = sizeof ( $ arr ) ; $ a = 8 ; $ b = 16 ; echo countTriplets ( $ arr , $ n , $ a , $ b ) . " " ; ? >
Count number of triplets in an array having sum in the range [ a , b ] | Function to find count of triplets having sum less than or equal to val . ; sort the input array . ; Initialize result ; to store sum ; Fix the first element ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept . ; If sum of current triplet is greater , then to reduce it decrease k . ; If sum is less than or equal to given value , then add possible triplets ( k - j ) to result . ; Function to return count of triplets having sum in range [ a , b ] . ; to store count of triplets . ; Find count of triplets having sum less than or equal to b and subtract count of triplets having sum less than or equal to a - 1. ; Driver Code
< ? php function countTripletsLessThan ( $ arr , $ n , $ val ) { sort ( $ arr ) ; $ ans = 0 ; $ sum ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { $ j = $ i + 1 ; $ k = $ n - 1 ; while ( $ j != $ k ) { $ sum = $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] ; if ( $ sum > $ val ) $ k -- ; else { $ ans += ( $ k - $ j ) ; $ j ++ ; } } } return $ ans ; } function countTriplets ( $ arr , $ n , $ a , $ b ) { $ res ; $ res = countTripletsLessThan ( $ arr , $ n , $ b ) - countTripletsLessThan ( $ arr , $ n , $ a - 1 ) ; return $ res ; } $ arr = array ( 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ) ; $ n = sizeof ( $ arr ) ; $ a = 8 ; $ b = 16 ; echo countTriplets ( $ arr , $ n , $ a , $ b ) , " " ; ? >
Sum of Areas of Rectangles possible for an array | Function to find area of rectangles ; sorting the array in descending order ; store the final sum of all the rectangles area possible ; temporary variable to store the length of rectangle ; Selecting the length of rectangle so that difference between any two number is 1 only . Here length is selected so flag is set ; flag is set means we have got length of rectangle ; length is set to a [ i + 1 ] so that if a [ i + 1 ] is less than a [ i ] by 1 then also we have the correct chice for length ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Selecting the width of rectangle so that difference between any two number is 1 only . Here width is selected so now flag is again unset for next rectangle ; area is calculated for rectangle ; flag is set false for another rectangle which we can get from elements in array ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Driver code
< ? php function MaxTotalRectangleArea ( $ a , $ n ) { rsort ( $ a ) ; $ sum = 0 ; $ flag = false ; $ len ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ( $ a [ $ i ] == $ a [ $ i + 1 ] or $ a [ $ i ] - $ a [ $ i + 1 ] == 1 ) and ( ! $ flag ) ) { $ flag = true ; $ len = $ a [ $ i + 1 ] ; $ i ++ ; } else if ( ( $ a [ $ i ] == $ a [ $ i + 1 ] or $ a [ $ i ] - $ a [ $ i + 1 ] == 1 ) and ( $ flag ) ) { $ sum = $ sum + $ a [ $ i + 1 ] * $ len ; $ flag = false ; $ i ++ ; } } return $ sum ; } $ a = array ( 10 , 10 , 10 , 10 , 11 , 10 , 11 , 10 , 9 , 9 , 8 , 8 ) ; $ n = count ( $ a ) ; echo MaxTotalRectangleArea ( $ a , $ n ) ; ? >
Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code
< ? php function swap ( & $ x , & $ y ) { $ x ^= $ y ^= $ x ^= $ y ; } function print1 ( $ a , $ n ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) echo ( $ a [ $ i ] . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } function sort1 ( $ a , $ n ) { for ( $ i = $ n ; $ i >= 0 ; $ i -- ) { for ( $ j = $ n ; $ j > $ n - $ i ; $ j -- ) { if ( $ a [ $ j ] > $ a [ $ j - 1 ] ) swap ( $ a [ $ j ] , $ a [ $ j - 1 ] ) ; } } print1 ( $ a , $ n ) ; } $ n = 6 ; $ a = array ( ) ; array_push ( $ a , 2 ) ; array_push ( $ a , 4 ) ; array_push ( $ a , 3 ) ; array_push ( $ a , 2 ) ; array_push ( $ a , 4 ) ; array_push ( $ a , 5 ) ; array_push ( $ a , 3 ) ; sort1 ( $ a , $ n ) ; ? >
Sum of Manhattan distances between all pairs of points | Return the sum of distance between all the pair of points . ; for each point , finding distance to rest of the point ; Driver Code
< ? php function distancesum ( $ x , $ y , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ sum += ( abs ( $ x [ $ i ] - $ x [ $ j ] ) + abs ( $ y [ $ i ] - $ y [ $ j ] ) ) ; return $ sum ; } $ x = array ( -1 , 1 , 3 , 2 ) ; $ y = array ( 5 , 6 , 5 , 3 ) ; $ n = count ( $ x ) ; echo distancesum ( $ x , $ y , $ n ) ; ? >
Sum of Manhattan distances between all pairs of points | Return the sum of distance of one axis . ; sorting the array . ; for each point , finding the distance . ; Driver Code
< ? php function distancesum ( $ arr , $ n ) { sort ( $ arr ) ; $ res = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ res += ( $ arr [ $ i ] * $ i - $ sum ) ; $ sum += $ arr [ $ i ] ; } return $ res ; } function totaldistancesum ( $ x , $ y , $ n ) { return distancesum ( $ x , $ n ) + distancesum ( $ y , $ n ) ; } $ x = array ( -1 , 1 , 3 , 2 ) ; $ y = array ( 5 , 6 , 5 , 3 ) ; $ n = sizeof ( $ x ) ; echo totaldistancesum ( $ x , $ y , $ n ) , " " ; ? >
Median after K additional integers | Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; Driver Code
< ? php function printMedian ( $ arr , $ n , $ K ) { sort ( $ arr ) ; echo $ arr [ ( $ n + $ K ) / 2 ] ; } $ arr = array ( 5 , 3 , 2 , 8 ) ; $ k = 3 ; $ n = count ( $ arr ) ; printMedian ( $ arr , $ n , $ k ) ; ? >
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | PHP program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Driver Code
< ? php function arrange ( $ arr , $ n , $ x ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ diff = abs ( $ arr [ $ i ] - $ x ) ; $ j = $ i - 1 ; if ( abs ( $ arr [ $ j ] - $ x ) > $ diff ) { $ temp = $ arr [ $ i ] ; while ( abs ( $ arr [ $ j ] - $ x ) > $ diff && $ j >= 0 ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j -- ; } $ arr [ $ j + 1 ] = $ temp ; } } return $ arr ; } function print_arr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 10 , 5 , 3 , 9 , 2 ) ; $ n = sizeof ( $ arr ) ; $ x = 7 ; $ arr1 = arrange ( $ arr , $ n , $ x ) ; print_arr ( $ arr1 , $ n ) ; ? >
Sort first half in ascending and second half in descending order | 1 | function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; Driver code
< ? php function printOrder ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; for ( $ j = $ n - 1 ; $ j >= $ n / 2 ; $ j -- ) echo $ arr [ $ j ] . " ▁ " ; } $ arr = array ( 5 , 4 , 6 , 2 , 1 , 3 , 8 , -1 ) ; $ n = sizeof ( $ arr ) ; printOrder ( $ arr , $ n ) ; ? >
Find the minimum and maximum amount to buy all N candies | Function to find the minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver Code ; Function call
< ? php function findMinimum ( $ arr , $ n , $ k ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ res += $ arr [ $ i ] ; $ n = $ n - $ k ; } return $ res ; } function findMaximum ( $ arr , $ n , $ k ) { $ res = 0 ; $ index = 0 ; for ( $ i = $ n - 1 ; $ i >= $ index ; $ i -- ) { $ res += $ arr [ $ i ] ; $ index += $ k ; } return $ res ; } $ arr = array ( 3 , 2 , 1 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; sort ( $ arr ) ; sort ( $ arr , $ n ) ; echo findMinimum ( $ arr , $ n , $ k ) , " " , findMaximum ( $ arr , $ n , $ k ) ; return 0 ; ? >
Find maximum height pyramid from the given array of objects | Returns maximum number of pyramidcal levels n boxes of given widths . ; Sort objects in increasing order of widths ; Initialize result ; Total width of previous level and total number of objects in previous level ; Number of object in current level . ; Width of current level . ; Picking the object . So increase current width and number of object . ; If current width and number of object are greater than previous . ; Update previous width , number of object on previous level . ; Reset width of current level , number of object on current level . ; Increment number of level . ; Driver Code
< ? php function maxLevel ( $ boxes , $ n ) { sort ( $ boxes ) ; $ ans = 1 ; $ prev_width = $ boxes [ 0 ] ; $ prev_count = 1 ; $ curr_count = 0 ; $ curr_width = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr_width += $ boxes [ $ i ] ; $ curr_count += 1 ; if ( $ curr_width > $ prev_width and $ curr_count > $ prev_count ) { $ prev_width = $ curr_width ; $ prev_count = $ curr_count ; $ curr_count = 0 ; $ curr_width = 0 ; $ ans ++ ; } } return $ ans ; } $ boxes = array ( 10 , 20 , 30 , 50 , 60 , 70 ) ; $ n = count ( $ boxes ) ; echo maxLevel ( $ boxes , $ n ) ; ? >
Sort all even numbers in ascending order and then sort all odd numbers in descending order | To do two way sort . First sort even numbers in ascending order , then odd numbers in descending order . ; Make all odd numbers negative ; if ( $arr [ $i ] & 1 ) Check for odd ; Sort all numbers ; Retaining original array ; Driver code
< ? php function twoWaySort ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] *= -1 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] & 1 ) $ arr [ $ i ] *= -1 ; } $ arr = array ( 1 , 3 , 2 , 7 , 5 , 4 ) ; $ n = sizeof ( $ arr ) ; twoWaySort ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Possible to form a triangle from array values | Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver Code
< ? php function isPossibleTriangle ( $ arr , $ N ) { if ( $ N < 3 ) return false ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ N - 2 ; $ i ++ ) if ( $ arr [ $ i ] + $ arr [ $ i + 1 ] > $ arr [ $ i + 2 ] ) return true ; } $ arr = array ( 5 , 4 , 3 , 1 , 2 ) ; $ N = count ( $ arr ) ; if ( isPossibleTriangle ( $ arr , $ N ) ) echo " Yes " ; else echo " No " ; ? >
K | PHP program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driver code
< ? php $ MAX = 10000 ; function ksmallest ( $ arr , $ n , $ k ) { global $ MAX ; $ b = array_fill ( 0 , $ MAX , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ b [ $ arr [ $ i ] ] = 1 ; for ( $ j = 1 ; $ j < $ MAX ; $ j ++ ) { if ( $ b [ $ j ] != 1 ) $ k -- ; if ( $ k == 0 ) return $ j ; } } $ k = 1 ; $ arr = array ( 1 ) ; $ n = count ( $ arr ) ; echo ksmallest ( $ arr , $ n , $ k ) ; ? >
Sort an array when two halves are sorted | PHP program to Merge two sorted halves of array Into Single Sorted Array ; Sort the given array using sort STL ; Driver Code ; Print sorted Array
< ? php function mergeTwoHalf ( & $ A , $ n ) { sort ( $ A , 0 ) ; } $ A = array ( 2 , 3 , 8 , -1 , 7 , 10 ) ; $ n = sizeof ( $ A ) ; mergeTwoHalf ( $ A , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ A [ $ i ] . " ▁ " ; ? >
Choose k array elements such that difference of maximum and minimum is minimized | Return minimum difference of maximum and minimum of k elements of arr [ 0. . n - 1 ] . ; Sorting the array . ; Find minimum value among all K size subarray . ; Driver Code
< ? php function minDiff ( $ arr , $ n , $ k ) { $ INT_MAX = 2147483647 ; $ result = $ INT_MAX ; sort ( $ arr , $ n ) ; sort ( $ arr ) ; for ( $ i = 0 ; $ i <= $ n - $ k ; $ i ++ ) $ result = min ( $ result , $ arr [ $ i + $ k - 1 ] - $ arr [ $ i ] ) ; return $ result ; } $ arr = array ( 10 , 100 , 300 , 200 , 1000 , 20 , 30 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; echo minDiff ( $ arr , $ n , $ k ) ; ? >
Number of swaps to sort when only adjacent swapping allowed | This function merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; this is tricky -- see above explanation / diagram for merge ( ) ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; Driver progra to test above functions
< ? php function merge ( & $ arr , & $ temp , $ left , $ mid , $ right ) { $ inv_count = 0 ; $ i = $ left ; $ j = $ mid ; $ k = $ left ; while ( ( $ i <= $ mid - 1 ) && ( $ j <= $ right ) ) { if ( $ arr [ $ i ] <= $ arr [ $ j ] ) $ temp [ $ k ++ ] = $ arr [ $ i ++ ] ; else { $ temp [ $ k ++ ] = $ arr [ $ j ++ ] ; $ inv_count = $ inv_count + ( $ mid - $ i ) ; } } while ( $ i <= $ mid - 1 ) $ temp [ $ k ++ ] = $ arr [ $ i ++ ] ; while ( $ j <= $ right ) $ temp [ $ k ++ ] = $ arr [ $ j ++ ] ; for ( $ i = $ left ; $ i <= $ right ; $ i ++ ) $ arr [ $ i ] = $ temp [ $ i ] ; return $ inv_count ; } function _mergeSort ( & $ arr , & $ temp , $ left , $ right ) { $ inv_count = 0 ; if ( $ right > $ left ) { $ mid = intval ( ( $ right + $ left ) / 2 ) ; $ inv_count = _mergeSort ( $ arr , $ temp , $ left , $ mid ) ; $ inv_count += _mergeSort ( $ arr , $ temp , $ mid + 1 , $ right ) ; $ inv_count += merge ( $ arr , $ temp , $ left , $ mid + 1 , $ right ) ; } return $ inv_count ; } function countSwaps ( & $ arr , $ n ) { $ temp = array_fill ( 0 , $ n , NULL ) ; return _mergeSort ( $ arr , $ temp , 0 , $ n - 1 ) ; } $ arr = array ( 1 , 20 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Number ▁ of ▁ swaps ▁ is ▁ " . countSwaps ( $ arr , $ n ) ; return 0 ; ? >
Check whether a given number is even or odd | Returns true if n is even , else odd ; Driver code
< ? php function isEven ( $ n ) { return ( $ n % 2 == 0 ) ; } $ n = 101 ; if ( isEven != true ) echo " Even " ; else echo " Odd " ; ? >