codeparrot/xlcost-text-to-code huggingface dataset:
Largest Sum Contiguous Subarray | PHP program to print largest contiguous array sum ; Driver Code
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; $ start = 0 ; $ end = 0 ; $ s = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here += $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) { $ max_so_far = $ max_ending_here ; $ start = $ s ; $ end = $ i ; } if ( $ max_ending_here < 0 ) { $ max_ending_here = 0 ; $ s = $ i + 1 ; } } echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_so_far . " STRNEWLINE " ; echo " Starting ▁ index ▁ " . $ start . " " . STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " Ending index " ▁ . ▁ $ end ▁ . ▁ " " } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; ? >
Find minimum number of currency notes and values that sum to given amount | function to count and prcurrency notes ; count notes using Greedy approach ; Print notes ; Driver Code
< ? php function countCurrency ( $ amount ) { $ notes = array ( 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 ) ; $ noteCounter = array ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { if ( $ amount >= $ notes [ $ i ] ) { $ noteCounter [ $ i ] = intval ( $ amount / $ notes [ $ i ] ) ; $ amount = $ amount - $ noteCounter [ $ i ] * $ notes [ $ i ] ; } } echo ( " Currency ▁ Count ▁ - > " . " STRNEWLINE " ) ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { if ( $ noteCounter [ $ i ] != 0 ) { echo ( $ notes [ $ i ] . " : " ▁ . ▁ $ noteCounter [ $ i ] ▁ . ▁ " " } } } $ amount = 868 ; countCurrency ( $ amount ) ; ? >
Minimum sum by choosing minimum of pairs from array | Returns minimum possible sum in array B [ ] ; Driver Code
< ? 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 ) ; ? >
Program for Next Fit algorithm in Memory Management | Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a process ; pick each process and find suitable blocks according to its size ad assign to it ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver program
< ? php function NextFit ( $ blockSize , $ m , $ processSize , $ n ) { $ allocation = array_fill ( 0 , $ n , -1 ) ; $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ j < $ m ) { if ( $ blockSize [ $ j ] >= $ processSize [ $ i ] ) { $ allocation [ $ i ] = $ j ; $ blockSize [ $ j ] -= $ processSize [ $ i ] ; break ; } $ j = ( $ j + 1 ) % $ m ; } } echo " Process No . Process Size Block no . " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo " " . ( $ i ▁ + ▁ 1 ) . " " . $ processSize [ $ i ] . " " if ( $ allocation [ $ i ] != -1 ) echo ( $ allocation [ $ i ] + 1 ) ; else echo " Not ▁ Allocated " ; echo " STRNEWLINE " ; } } $ blockSize = array ( 5 , 10 , 20 ) ; $ processSize = array ( 10 , 20 , 5 ) ; $ m = count ( $ blockSize ) ; $ n = count ( $ processSize ) ; NextFit ( $ blockSize , $ m , $ processSize , $ n ) ; ? >
Find the subarray with least average | Prints beginning and ending indexes of subarray of size k with minimum average ; k must be smaller than or equal to n ; Initialize beginning index of result ; Compute sum of first subarray of size k ; Initialize minimum sum as current sum ; Traverse from ( k + 1 ) ' th ▁ element ▁ ▁ to ▁ n ' th element ; Add current item and remove first item of previous subarray ; Update result if needed ; Driver Code ; Subarray size
< ? php function findMinAvgSubarray ( $ arr , $ n , $ k ) { if ( $ n < $ k ) return ; $ res_index = 0 ; $ curr_sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ curr_sum += $ arr [ $ i ] ; $ min_sum = $ curr_sum ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ curr_sum += $ arr [ $ i ] - $ arr [ $ i - $ k ] ; if ( $ curr_sum < $ min_sum ) { $ min_sum = $ curr_sum ; $ res_index = ( $ i - $ k + 1 ) ; } } echo " Subarray between [ " ▁ , $ res _ index ▁ , ▁ " , " ▁ , $ res _ index ▁ + ▁ $ k ▁ - ▁ 1 , ▁ " ] has minimum average " ; } $ arr = array ( 3 , 7 , 90 , 20 , 10 , 50 , 40 ) ; $ k = 3 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findMinAvgSubarray ( $ arr , $ n , $ k ) ; return 0 ; ? >
Find element using minimum segments in Seven Segment Display | Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driver Code
< ? php $ seg = array ( 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ) ; function computeSegment ( $ x ) { global $ seg ; if ( $ x == 0 ) return $ seg [ 0 ] ; $ count = 0 ; while ( $ x ) { $ count += $ seg [ $ x % 10 ] ; $ x = ( int ) $ x / 10 ; } return $ count ; } function elementMinSegment ( $ arr , $ n ) { $ minseg = computeSegment ( $ arr [ 0 ] ) ; $ minindex = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ temp = computeSegment ( $ arr [ $ i ] ) ; if ( $ temp < $ minseg ) { $ minseg = $ temp ; $ minindex = $ i ; } } return $ arr [ $ minindex ] ; } $ arr = array ( 489 , 206 , 745 , 123 , 756 ) ; $ n = sizeof ( $ arr ) ; echo elementMinSegment ( $ arr , $ n ) , " " ; ? >
Find the Largest number with given number of digits and sum of digits | Prints the smalles possible number with digit sum ' s ' and ' m ' number of digits . ; If sum of digits is 0 , then a number is possible only if number of digits is 1. ; Sum greater than the maximum possible sum . ; Fill from most significant digit to least significant digit . ; Fill 9 first to make the number largest ; If remaining sum becomes less than 9 , then fill the remaining sum ; Driver code
< ? php function findLargest ( $ m , $ s ) { if ( $ s == 0 ) { if ( ( $ m == 1 ) == true ) echo " Largest ▁ number ▁ is ▁ " , 0 ; else echo " Not ▁ possible " ; return ; } if ( $ s > 9 * $ m ) { echo " Not ▁ possible " ; return ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ s >= 9 ) { $ res [ $ i ] = 9 ; $ s -= 9 ; } else { $ res [ $ i ] = $ s ; $ s = 0 ; } } echo " Largest ▁ number ▁ is ▁ " ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ res [ $ i ] ; } $ s = 9 ; $ m = 2 ; findLargest ( $ m , $ s ) ; ? >
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
< ? 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 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
< ? 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 ) ; ? >
Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver Code
< ? php function smallestSubWithSum ( $ arr , $ n , $ x ) { $ min_len = $ n + 1 ; for ( $ start = 0 ; $ start < $ n ; $ start ++ ) { $ curr_sum = $ arr [ $ start ] ; if ( $ curr_sum > $ x ) return 1 ; for ( $ end = $ start + 1 ; $ end < $ n ; $ end ++ ) { $ curr_sum += $ arr [ $ end ] ; if ( $ curr_sum > $ x && ( $ end - $ start + 1 ) < $ min_len ) $ min_len = ( $ end - $ start + 1 ) ; } } return $ min_len ; } $ arr1 = array ( 1 , 4 , 45 , 6 , 10 , 19 ) ; $ x = 51 ; $ n1 = sizeof ( $ arr1 ) ; $ res1 = smallestSubWithSum ( $ arr1 , $ n1 , $ x ) ; if ( ( $ res1 == $ n1 + 1 ) == true ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res1 , " STRNEWLINE " ; $ arr2 = array ( 1 , 10 , 5 , 2 , 7 ) ; $ n2 = sizeof ( $ arr2 ) ; $ x = 9 ; $ res2 = smallestSubWithSum ( $ arr2 , $ n2 , $ x ) ; if ( ( $ res2 == $ n2 + 1 ) == true ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res2 , " STRNEWLINE " ; $ arr3 = array ( 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ) ; $ n3 = sizeof ( $ arr3 ) ; $ x = 280 ; $ res3 = smallestSubWithSum ( $ arr3 , $ n3 , $ x ) ; if ( ( $ res3 == $ n3 + 1 ) == true ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res3 , " STRNEWLINE " ; ? >
Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver Code
< ? php function smallestSubWithSum ( $ arr , $ n , $ x ) { $ curr_sum = 0 ; $ min_len = $ n + 1 ; $ start = 0 ; $ end = 0 ; while ( $ end < $ n ) { while ( $ curr_sum <= $ x && $ end < $ n ) $ curr_sum += $ arr [ $ end ++ ] ; while ( $ curr_sum > $ x && $ start < $ n ) { if ( $ end - $ start < $ min_len ) $ min_len = $ end - $ start ; $ curr_sum -= $ arr [ $ start ++ ] ; } } return $ min_len ; } $ arr1 = array ( 1 , 4 , 45 , 6 , 10 , 19 ) ; $ x = 51 ; $ n1 = sizeof ( $ arr1 ) ; $ res1 = smallestSubWithSum ( $ arr1 , $ n1 , $ x ) ; if ( $ res1 == $ n1 + 1 ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res1 , " STRNEWLINE " ; $ arr2 = array ( 1 , 10 , 5 , 2 , 7 ) ; $ n2 = sizeof ( $ arr2 ) ; $ x = 9 ; $ res2 = smallestSubWithSum ( $ arr2 , $ n2 , $ x ) ; if ( $ res2 == $ n2 + 1 ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res2 , " STRNEWLINE " ; $ arr3 = array ( 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ) ; $ n3 = sizeof ( $ arr3 ) ; $ x = 280 ; $ res3 = smallestSubWithSum ( $ arr3 , $ n3 , $ x ) ; if ( $ res3 == $ n3 + 1 ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res3 , " STRNEWLINE " ; ? >
Count minimum steps to get the given desired array | Returns count of minimum operations to convert a zero array to target array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert target to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of target don 't become 0. ; To store count of zeroes in current target array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver Code
< ? php function countMinOperations ( $ target , $ n ) { $ result = 0 ; while ( 1 ) { $ zero_count = 0 ; $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ target [ $ i ] & 1 ) break ; else if ( $ target [ $ i ] == 0 ) $ zero_count ++ ; } if ( $ zero_count == $ n ) return $ result ; if ( $ i == $ n ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ target [ $ j ] = $ target [ $ j ] / 2 ; $ result ++ ; } for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( $ target [ $ j ] & 1 ) { $ target [ $ j ] -- ; $ result ++ ; } } } } $ arr = array ( 16 , 16 , 16 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to ▁ STRNEWLINE " . " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " . countMinOperations ( $ arr , $ n ) ; ? >
Find minimum number of merge operations to make an array palindrome | Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver Code
< ? php function findMinOps ( $ arr , $ n ) { $ ans = 1 ; for ( $ i = 0 , $ j = $ n - 1 ; $ i <= $ j { if ( $ arr [ $ i ] == $ arr [ $ j ] ) { $ i ++ ; $ j -- ; } else if ( $ arr [ $ i ] > $ arr [ $ j ] ) { $ j -- ; $ arr [ $ j ] += $ arr [ $ j + 1 ] ; $ ans ++ ; } else { $ i ++ ; $ arr [ $ i ] += $ arr [ $ i - 1 ] ; $ ans ++ ; } } return $ ans ; } $ arr [ ] = array ( 1 , 4 , 5 , 9 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " , findMinOps ( $ arr , $ n ) ; ? >
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver Code
< ? php function findSmallest ( $ arr , $ n ) { $ res = 1 ; for ( $ i = 0 ; $ i < $ n and $ arr [ $ i ] <= $ res ; $ i ++ ) $ res = $ res + $ arr [ $ i ] ; return $ res ; } $ arr1 = array ( 1 , 3 , 4 , 5 ) ; $ n1 = count ( $ arr1 ) ; echo findSmallest ( $ arr1 , $ n1 ) , " STRNEWLINE " ; $ arr2 = array ( 1 , 2 , 6 , 10 , 11 , 15 ) ; $ n2 = count ( $ arr2 ) ; echo findSmallest ( $ arr2 , $ n2 ) , " STRNEWLINE " ; $ arr3 = array ( 1 , 1 , 1 , 1 ) ; $ n3 = count ( $ arr3 ) ; echo findSmallest ( $ arr3 , $ n3 ) , " STRNEWLINE " ; $ arr4 = array ( 1 , 1 , 3 , 4 ) ; $ n4 = count ( $ arr4 ) ; echo findSmallest ( $ arr4 , $ n4 ) ; ? >
Find minimum difference between any two elements | Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver code
< ? php function findMinDiff ( $ arr , $ n ) { $ diff = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) < $ diff ) $ diff = abs ( $ arr [ $ i ] - $ arr [ $ j ] ) ; return $ diff ; } $ arr = array ( 1 , 5 , 3 , 19 , 18 , 25 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ difference ▁ is ▁ " , findMinDiff ( $ arr , $ n ) ; ? >
Find minimum difference between any two elements | Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver code
< ? php function findMinDiff ( $ arr , $ n ) { sort ( $ arr ) ; $ diff = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i + 1 ] - $ arr [ $ i ] < $ diff ) $ diff = $ arr [ $ i + 1 ] - $ arr [ $ i ] ; return $ diff ; } $ arr = array ( 1 , 5 , 3 , 19 , 18 , 25 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ difference ▁ is ▁ " , findMinDiff ( $ arr , $ n ) ; ? >
Space optimization using bit manipulations | Driver Code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1
< ? php $ a = 2 ; $ b = 10 ; $ size = abs ( $ b - $ a ) + 1 ; $ array = array_fill ( 0 , $ size , 0 ) ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) if ( $ i % 2 == 0 $ i % 5 == 0 ) $ array [ $ i - $ a ] = 1 ; echo " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : STRNEWLINE " ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) if ( $ array [ $ i - $ a ] == 1 ) echo $ i . " " ; ? >
Longest Span with same Sum in two Binary arrays | Returns length of the longest common subarray with same sum ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver Code
< ? php function longestCommonSum ( $ arr1 , $ arr2 , $ n ) { $ maxLen = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 = 0 ; $ sum2 = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ sum1 += $ arr1 [ $ j ] ; $ sum2 += $ arr2 [ $ j ] ; if ( $ sum1 == $ sum2 ) { $ len = $ j - $ i + 1 ; if ( $ len > $ maxLen ) $ maxLen = $ len ; } } } return $ maxLen ; } $ arr1 = array ( 0 , 1 , 0 , 1 , 1 , 1 , 1 ) ; $ arr2 = array ( 1 , 1 , 1 , 1 , 1 , 0 , 1 ) ; $ n = sizeof ( $ arr1 ) ; echo " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ " . " with ▁ same ▁ " , " sum ▁ is ▁ " , longestCommonSum ( $ arr1 , $ arr2 , $ n ) ; ? >
Merge an array of size n into another array of size m + n | Assuming - 1 is filled for the places where element is not available ; Function to move m elements at the end of array mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN
< ? php $ NA = -1 ; function moveToEnd ( & $ mPlusN , $ size ) { global $ NA ; $ j = $ size - 1 ; for ( $ i = $ size - 1 ; $ i >= 0 ; $ i -- ) if ( $ mPlusN [ $ i ] != $ NA ) { $ mPlusN [ $ j ] = $ mPlusN [ $ i ] ; $ j -- ; } } function merge ( & $ mPlusN , & $ N , $ m , $ n ) { $ i = $ n ; $ j = 0 ; $ k = 0 ; while ( $ k < ( $ m + $ n ) ) { if ( ( $ j == $ n ) || ( $ i < ( $ m + $ n ) && $ mPlusN [ $ i ] <= $ N [ $ j ] ) ) { $ mPlusN [ $ k ] = $ mPlusN [ $ i ] ; $ k ++ ; $ i ++ ; } else { $ mPlusN [ $ k ] = $ N [ $ j ] ; $ k ++ ; $ j ++ ; } } } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ mPlusN = array ( 2 , 8 , $ NA , $ NA , $ NA , 13 , $ NA , 15 , 20 ) ; $ N = array ( 5 , 7 , 9 , 25 ) ; $ n = sizeof ( $ N ) ; $ m = sizeof ( $ mPlusN ) - $ n ; moveToEnd ( $ mPlusN , $ m + $ n ) ; merge ( $ mPlusN , $ N , $ m , $ n ) ; printArray ( $ mPlusN , $ m + $ n ) ; ? >
Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check bool array B and sorts elements for continuous sequence of 1 ; Sort array A from i to j ; Check if array is sorted or not ; Driver Code
< ? php function sortedAfterSwap ( $ A , $ B , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ B [ $ i ] ) { $ j = $ i ; while ( $ B [ $ j ] ) $ j ++ ; sort ( $ A ) ; $ i = $ j ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] != $ i + 1 ) return false ; } return true ; } $ A = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ B = array ( 0 , 1 , 1 , 1 , 0 ) ; $ n = count ( $ A ) ; if ( sortedAfterSwap ( $ A , $ B , $ n ) ) echo " A ▁ can ▁ be ▁ sorted STRNEWLINE " ; else echo " A ▁ can ▁ not ▁ be ▁ sorted STRNEWLINE " ; ? >
Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check if array is sorted or not ; Driver Code
< ? php function sortedAfterSwap ( & $ A , & $ B , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ B [ $ i ] ) { if ( $ A [ $ i ] != $ i + 1 ) { $ t = $ A [ $ i ] ; $ A [ $ i ] = $ A [ $ i + 1 ] ; $ A [ $ i + 1 ] = $ t ; } } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] != $ i + 1 ) return false ; } return true ; } $ A = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ B = array ( 0 , 1 , 1 , 1 , 0 ) ; $ n = sizeof ( $ A ) ; if ( sortedAfterSwap ( $ A , $ B , $ n ) ) echo " A ▁ can ▁ be ▁ sorted STRNEWLINE " ; else echo " A ▁ can ▁ not ▁ be ▁ sorted STRNEWLINE " ; ? >
Sort an array containing two types of elements | Method for segregation 0 and 1 given input array ; Driver Code
< ? php function segregate0and1 ( $ arr , $ n ) { $ type0 = 0 ; $ type1 = $ n - 1 ; while ( $ type0 < $ type1 ) { if ( $ arr [ $ type0 ] == 1 ) { $ temp = $ arr [ $ type0 ] ; $ arr [ $ type0 ] = $ arr [ $ type1 ] ; $ arr [ $ type1 ] = $ temp ; $ type1 -- ; } else { $ type0 ++ ; } } return $ arr ; } $ arr = array ( 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ) ; $ n = count ( $ arr ) ; $ arr1 = segregate0and1 ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr1 [ $ i ] . " ▁ " ; ? >
Count Inversions in an array | Set 1 ( Using Merge Sort ) | PHP program to Count Inversions in an array ; Driver Code
< ? php function getInvCount ( & $ arr , $ n ) { $ inv_count = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] ) $ inv_count ++ ; return $ inv_count ; } $ arr = array ( 1 , 20 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo " Number ▁ of ▁ inversions ▁ are ▁ " , getInvCount ( $ arr , $ n ) ; ? >
Two elements whose sum is closest to zero | PHP program to find the Two elements whose sum is closest to zero ; Array should have at least two elements ; Initialization of values ; Driver Code
< ? php function minAbsSumPair ( $ arr , $ arr_size ) { $ inv_count = 0 ; if ( $ arr_size < 2 ) { echo " Invalid ▁ Input " ; return ; } $ min_l = 0 ; $ min_r = 1 ; $ min_sum = $ arr [ 0 ] + $ arr [ 1 ] ; for ( $ l = 0 ; $ l < $ arr_size - 1 ; $ l ++ ) { for ( $ r = $ l + 1 ; $ r < $ arr_size ; $ r ++ ) { $ sum = $ arr [ $ l ] + $ arr [ $ r ] ; if ( abs ( $ min_sum ) > abs ( $ sum ) ) { $ min_sum = $ sum ; $ min_l = $ l ; $ min_r = $ r ; } } } echo " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are ▁ " . $ arr [ $ min_l ] . " ▁ and ▁ " . $ arr [ $ min_r ] ; } $ arr = array ( 1 , 60 , -10 , 70 , -80 , 85 ) ; minAbsSumPair ( $ arr , 6 ) ; ? >