codeparrot/xlcost-text-to-code huggingface dataset:
Absolute distinct count in a sorted array | The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code
< ? php function distinctCount ( $ arr , $ n ) { $ count = $ n ; $ i = 0 ; $ j = $ n - 1 ; $ sum = 0 ; while ( $ i < $ j ) { while ( $ i != $ j && $ arr [ $ i ] == $ arr [ $ i + 1 ] ) { $ count -- ; $ i ++ ; } while ( $ i != $ j && $ arr [ $ j ] == $ arr [ $ j - 1 ] ) { $ count -- ; $ j -- ; } if ( $ i == $ j ) break ; $ sum = $ arr [ $ i ] + $ arr [ $ j ] ; if ( $ sum == 0 ) { $ count -- ; $ i ++ ; $ j -- ; } else if ( $ sum < 0 ) $ i ++ ; else $ j -- ; } return $ count ; } $ arr = array ( -2 , -1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " . distinctCount ( $ arr , $ n ) ; ? >
Sort an almost sorted array where only two elements are swapped | This function sorts an array that can be sorted by single swap ; Traverse the given array from rightmost side ; Check if arr [ i ] is not in order ; Find the other element to be swapped with arr [ i ] ; Swap the pair ; A utility function to print an array of size n ; Driver Code
< ? php function sortByOneSwap ( & $ arr , $ n ) { for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] ) { $ j = $ i - 1 ; while ( $ j >= 0 && $ arr [ $ i ] < $ arr [ $ j ] ) $ j -- ; $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j + 1 ] ; $ arr [ $ j + 1 ] = $ temp ; break ; } } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ arr = array ( 10 , 30 , 20 , 40 , 50 , 60 , 70 ) ; $ n = sizeof ( $ arr ) ; echo " Given ▁ array ▁ is ▁ " . " STRNEWLINE " ; printArray ( $ arr , $ n ) ; sortByOneSwap ( $ arr , $ n ) ; echo " Sorted ▁ array ▁ is ▁ " . " STRNEWLINE " ; printArray ( $ arr , $ n ) ;
Pancake sorting | Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already  at the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; A utility function to print n array of size n ; Driver code
< ? php function flip ( & $ arr , $ i ) { $ start = 0 ; while ( $ start < $ i ) { $ temp = $ arr [ $ start ] ; $ arr [ $ start ] = $ arr [ $ i ] ; $ arr [ $ i ] = $ temp ; $ start ++ ; $ i -- ; } } function findMax ( $ arr , $ n ) { $ mi = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) if ( $ arr [ $ i ] > $ arr [ $ mi ] ) $ mi = $ i ; return $ mi ; } function pancakeSort ( & $ arr , $ n ) { for ( $ curr_size = $ n ; $ curr_size > 1 ; -- $ curr_size ) { $ mi = findMax ( $ arr , $ curr_size ) ; if ( $ mi != $ curr_size - 1 ) { flip ( $ arr , $ mi ) ; flip ( $ arr , $ curr_size -1 ) ; } } } function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) print ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 23 , 10 , 20 , 11 , 12 , 6 , 7 ) ; $ n = count ( $ arr ) ; pancakeSort ( $ arr , $ n ) ; echo ( " Sorted ▁ Array ▁ STRNEWLINE " ) ; printArray ( $ arr , $ n ) ; return 0 ; ? >
Maximize the given number by replacing a segment of digits with the alternate digits given | PHP implementation of the approach Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code ; This Code is contributed is Tushill .
< ? php function get_maximum ( $ s , $ a ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] - '0' < $ a [ $ s [ $ i ] - '0' ] ) { $ j = $ i ; while ( $ j < $ n && ( $ s [ $ j ] - '0' <= $ a [ $ s [ $ j ] - '0' ] ) ) { $ s [ $ j ] = '0' + $ a [ $ s [ $ j ] - '0' ] ; $ j ++ ; } return $ s ; } } return $ s ; } $ s = "1337" ; $ a = array ( 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 ) ; echo get_maximum ( $ s , $ a ) ; ? >
Number of times the largest perfect square number can be subtracted from N | Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code
< ? php function countSteps ( $ n ) { $ steps = 0 ; while ( $ n ) { $ largest = ( int ) sqrt ( $ n ) ; $ n -= ( $ largest * $ largest ) ; $ steps ++ ; } return $ steps ; } $ n = 85 ; echo countSteps ( $ n ) ; ? >
Maximum array sum that can be obtained after exactly k changes | Utility function to return the sum of the array elements ; Function to return the maximized sum of the array after performing the given operation exactly k times ; Sort the array elements ; Change signs of the negative elements starting from the smallest ; If a single operation has to be performed then it must be performed on the smallest positive element ; To store the index of the minimum element ; Update the minimum index ; Perform remaining operation on the smallest element ; Return the sum of the updated array ; Driver code
< ? php function sumArr ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; return $ sum ; } function maxSum ( $ arr , $ n , $ k ) { sort ( $ arr ) ; $ i = 0 ; while ( $ i < $ n && $ k > 0 && $ arr [ $ i ] < 0 ) { $ arr [ $ i ] *= -1 ; $ k -- ; $ i ++ ; } if ( $ k % 2 == 1 ) { $ min = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ min ] > $ arr [ $ i ] ) $ min = $ i ; $ arr [ $ min ] *= -1 ; } return sumArr ( $ arr , $ n ) ; } $ arr = array ( -5 , 4 , 1 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; $ k = 4 ; echo maxSum ( $ arr , $ n , $ k ) , " STRNEWLINE " ; ? >
Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible | Function to find the maximum possible sum ; To store required sum ; Number of 234 's can be formed ; Sum obtained with 234 s ; Remaining 2 's ; Sum obtained with 12 s ; Return the required sum ; Driver code
< ? php function Maxsum ( $ c1 , $ c2 , $ c3 , $ c4 ) { $ sum = 0 ; $ two34 = min ( $ c2 , min ( $ c3 , $ c4 ) ) ; $ sum = $ two34 * 234 ; $ c2 -= $ two34 ; $ sum += min ( $ c2 , $ c1 ) * 12 ; return $ sum ; } $ c1 = 5 ; $ c2 = 2 ; $ c3 = 3 ; $ c4 = 4 ; echo Maxsum ( $ c1 , $ c2 , $ c3 , $ c4 ) ; ? >
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; pairs from 1 to n * ( a / n ) and 1 to n * ( b / n ) ; pairs from 1 to n * ( a / n ) and n * ( b / n ) to b ; pairs from n * ( a / n ) to a and 1 to n * ( b / n ) ; pairs from n * ( a / n ) to a and n * ( b / n ) to b ; Return answer ; Driver code
< ? php function findCountOfPairs ( $ a , $ b , $ n ) { $ ans = 0 ; $ ans += $ n * ( int ) ( $ a / $ n ) * ( int ) ( $ b / $ n ) ; $ ans += ( int ) ( $ a / $ n ) * ( $ b % $ n ) ; $ ans += ( $ a % $ n ) * ( int ) ( $ b / $ n ) ; $ ans += ( ( $ a % $ n ) + ( int ) ( $ b % $ n ) ) / $ n ; return $ ans ; } $ a = 5 ; $ b = 13 ; $ n = 3 ; echo findCountOfPairs ( $ a , $ b , $ n ) ; ? >
Generate array with minimum sum which can be deleted in P steps | Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code
< ? php function findArray ( $ N , $ P ) { $ ans = ( $ P * ( $ P + 1 ) ) / 2 + ( $ N - $ P ) ; $ arr [ $ N + 1 ] = array ( ) ; for ( $ i = 1 ; $ i <= $ P ; $ i ++ ) $ arr [ $ i ] = $ i ; for ( $ i = $ P + 1 ; $ i <= $ N ; $ i ++ ) $ arr [ $ i ] = 1 ; echo " The ▁ Minimum ▁ Possible ▁ Sum ▁ is : ▁ " , $ ans , " STRNEWLINE " ; echo " The ▁ Array ▁ Elements ▁ are : ▁ STRNEWLINE " ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo $ arr [ $ i ] , ' ▁ ' ; } $ N = 5 ; $ P = 3 ; findArray ( $ N , $ P ) ; ? >
Find Intersection of all Intervals | Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code
< ? php function findIntersection ( $ intervals , $ N ) { $ l = $ intervals [ 0 ] [ 0 ] ; $ r = $ intervals [ 0 ] [ 1 ] ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { if ( $ intervals [ $ i ] [ 0 ] > $ r $ intervals [ $ i ] [ 1 ] < $ l ) { echo - 1 ; return ; } else { $ l = max ( $ l , $ intervals [ $ i ] [ 0 ] ) ; $ r = min ( $ r , $ intervals [ $ i ] [ 1 ] ) ; } } echo " [ " ▁ . ▁ $ l ▁ . ▁ " , " ▁ . ▁ $ r ▁ . ▁ " ] " ; } $ intervals = array ( array ( 1 , 6 ) , array ( 2 , 8 ) , array ( 3 , 10 ) , array ( 5 , 8 ) ) ; $ N = sizeof ( $ intervals ) ; findIntersection ( $ intervals , $ N ) ; ? >
Maximum size of sub | Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Print the required answer
< ? php function cmp ( $ a , $ b ) { return ( $ a > $ b ) - ( $ a < $ b ) ; } function maxSubarraySize ( $ arr ) { $ N = sizeof ( $ arr ) ; $ ans = 1 ; $ anchor = 0 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ c = cmp ( $ arr [ $ i - 1 ] , $ arr [ $ i ] ) ; if ( $ c == 0 ) $ anchor = $ i ; else if ( $ i == $ N - 1 or $ c * cmp ( $ arr [ $ i ] , $ arr [ $ i + 1 ] ) != -1 ) { $ ans = max ( $ ans , $ i - $ anchor + 1 ) ; $ anchor = $ i ; } } return $ ans ; } $ arr = array ( 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ) ; echo maxSubarraySize ( $ arr ) ; ? >
Count valid pairs in the array satisfying given conditions | Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer
< ? php function ValidPairs ( $ arr , $ n ) { $ count = array_fill ( 0 , 121 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ $ arr [ $ i ] ] += 1 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] < $ arr [ $ j ] ) continue ; if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) % 2 == 1 ) continue ; $ ans += $ count [ $ arr [ $ i ] ] * $ count [ $ arr [ $ j ] ] ; if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ ans -= $ count [ $ arr [ $ i ] ] ; } return $ ans ; } $ arr = array ( 16 , 17 , 18 ) ; $ n = count ( $ arr ) ; echo ( ValidPairs ( $ arr , $ n ) ) ; ? >
Minimum number of 1 's to be replaced in a binary array | Function to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; return final answer ; Driver Code
< ? php function minChanges ( $ A , $ n ) { $ cnt = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; ++ $ i ) { if ( ( $ i - 1 >= 0 ) && $ A [ $ i - 1 ] == 1 && $ A [ $ i + 1 ] == 1 && $ A [ $ i ] == 0 ) { $ A [ $ i + 1 ] = 0 ; $ cnt ++ ; } } return $ cnt ; } $ A = array ( 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 ) ; $ n = sizeof ( $ A ) ; echo minChanges ( $ A , $ n ) ; ? >
Number of closing brackets needed to complete a regular bracket sequence | Function to find number of closing brackets and complete a regular bracket sequence ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver Code
< ? php function completeSequence ( $ s ) { $ n = strlen ( $ s ) ; $ open = 0 ; $ close = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' ( ' ) $ open ++ ; else $ close ++ ; if ( $ close > $ open ) { echo ( " IMPOSSIBLE " ) ; return ; } } echo ( $ s ) ; for ( $ i = 0 ; $ i < $ open - $ close ; $ i ++ ) echo ( " ) " ) ; } $ s = " ( ( ) ( ( ) ( " ; completeSequence ( $ s ) ; ? >
Lexicographically smallest permutation with no digits at Original Index | Function to print the smallest permutation ; when n is even ; when n is odd ; handling last 3 digit ; add EOL and print result ; Driver Code
< ? php function smallestPermute ( $ n ) { $ res = array_fill ( 0 , $ n + 1 , " " ) ; if ( $ n % 2 == 0 ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ res [ $ i ] = chr ( 48 + $ i + 2 ) ; else $ res [ $ i ] = chr ( 48 + $ i ) ; } } else { for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { if ( $ i % 2 == 0 ) $ res [ $ i ] = chr ( 48 + $ i + 2 ) ; else $ res [ $ i ] = chr ( 48 + $ i ) ; } $ res [ $ n - 1 ] = chr ( 48 + $ n - 2 ) ; $ res [ $ n - 2 ] = chr ( 48 + $ n ) ; $ res [ $ n - 3 ] = chr ( 48 + $ n - 1 ) ; } $ res [ $ n ] = ' \0' ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ res [ $ i ] ; } } $ n = 7 ; smallestPermute ( $ n ) ; ? >
Minimum array insertions required to make consecutive difference <= K | Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver Code
< ? php function minInsertions ( $ H , $ n , $ K ) { $ inser = 0 ; for ( $ i = 1 ; $ i < $ n ; ++ $ i ) { $ diff = abs ( $ H [ $ i ] - $ H [ $ i - 1 ] ) ; if ( $ diff <= $ K ) continue ; else $ inser += ceil ( $ diff / $ K ) - 1 ; } return $ inser ; } $ H = array ( 2 , 4 , 8 , 16 ) ; $ K = 3 ; $ n = sizeof ( $ H ) ; echo minInsertions ( $ H , $ n , $ K ) ; ? >
Minimum number of operations required to reduce N to 1 | Function that returns the minimum number of operations to be performed to reduce the number to 1 ; To stores the total number of operations to be performed ; if n is divisible by 3 then reduce it to n / 3 ; if n modulo 3 is 1 decrement it by 1 ; if n modulo 3 is 2 then increment it by 1 ; update the counter ; Driver code
< ? php function count_minimum_operations ( $ n ) { $ count = 0 ; while ( $ n > 1 ) { if ( $ n % 3 == 0 ) $ n /= 3 ; else if ( $ n % 3 == 1 ) $ n -- ; else { if ( $ n == 2 ) $ n -- ; else $ n ++ ; } $ count ++ ; } return $ count ; } $ n = 4 ; $ ans = count_minimum_operations ( $ n ) ; echo $ ans , " STRNEWLINE " ; ? >
Maximize the sum of array by multiplying prefix of array with | PHP implementation of the approach ; To store sum ; To store ending indices of the chosen prefix arrays ; Adding the absolute value of a [ i ] ; If i == 0 then there is no index to be flipped in ( i - 1 ) position ; print the maximised sum ; print the ending indices of the chosen prefix arrays ; Driver Code
< ? php function maxSum ( $ a , $ n ) { $ s = 0 ; $ l = array ( ) ; for ( $ i = 0 ; $ i < count ( $ a ) ; $ i ++ ) { $ s += abs ( $ a [ $ i ] ) ; if ( $ a [ $ i ] >= 0 ) continue ; if ( $ i == 0 ) array_push ( $ l , $ i + 1 ) ; else { array_push ( $ l , $ i + 1 ) ; array_push ( $ l , $ i ) ; } } echo $ s . " STRNEWLINE " ; for ( $ i = 0 ; $ i < count ( $ l ) ; $ i ++ ) echo $ l [ $ i ] . " ▁ " ; } $ n = 4 ; $ a = array ( 1 , -2 , -3 , 4 ) ; maxSum ( $ a , $ n ) ; ? >
Find the longest common prefix between two strings after performing swaps on second string | PHP program to find the longest common prefix between two strings after performing swaps on the second string ; $a = strlen ( $x ) ; length of x $b = strlen ( $y ) ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; Driver Code
< ? php function LengthLCP ( $ x , $ y ) { $ fr = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) { $ fr [ ord ( $ y [ $ i ] ) - 97 ] += 1 ; } $ c = 0 ; for ( $ i = 0 ; $ i < $ a ; $ i ++ ) { if ( $ fr [ ord ( $ x [ $ i ] ) - 97 ] > 0 ) { $ c += 1 ; $ fr [ ord ( $ x [ $ i ] ) - 97 ] -= 1 ; } else break ; } echo $ c ; } $ x = " here " ; $ y = " there " ; LengthLCP ( $ x , $ y ) ; return 0 ; ? >
All possible co | Function to count possible pairs ; total count of numbers in range ; printing count of pairs ; Driver code
< ? php function CountPair ( $ L , $ R ) { $ x = ( $ R - $ L + 1 ) ; echo $ x / 2 , " STRNEWLINE " ; } $ L = 1 ; $ R = 8 ; CountPair ( $ L , $ R ) ; ? >
Problems not solved at the end of Nth day | Function to find problems not solved at the end of Nth day ; Driver Code
< ? php function problemsLeft ( $ K , $ P , $ N ) { if ( $ K <= $ P ) return 0 ; else return ( $ K - $ P ) * $ N ; } $ K = 4 ; $ P = 1 ; $ N = 10 ; echo problemsLeft ( $ K , $ P , $ N ) ; ? >
Number of chocolates left after k iterations | Function to find the chocolates left ; Driver code
< ? php function results ( $ n , $ k ) { return round ( pow ( $ n , ( 1.0 / pow ( 2 , $ k ) ) ) ) ; } $ k = 3 ; $ n = 100000000 ; echo ( " Chocolates ▁ left ▁ after ▁ " ) ; echo ( $ k ) ; echo ( " ▁ iterations ▁ are ▁ " ) ; echo ( results ( $ n , $ k ) ) ; ? >
Make array elements equal in Minimum Steps | Returns the minimum steps required to make an array of N elements equal , where the first array element equals M ; Corner Case 1 : When N = 1 ; Corner Case 2 : When N = 2 else if ( $N == 2 ) corner case 2 ; Driver Code
< ? php function steps ( $ N , $ M ) { if ( $ N == 1 ) return 0 ; return $ M ; return 2 * $ M + ( $ N - 3 ) ; } $ N = 4 ; $ M = 4 ; echo steps ( $ N , $ M ) ; ? >
Check if it is possible to survive on Island | Function to find the minimum $days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver code
< ? php function survival ( $ S , $ N , $ M ) { if ( ( ( $ N * 6 ) < ( $ M * 7 ) && $ S > 6 ) $ M > $ N ) echo " No " ; else { $ days = ( $ M * $ S ) / $ N ; if ( ( ( $ M * $ S ) % $ N ) != 0 ) $ days ++ ; echo " Yes ▁ " , floor ( $ days ) ; } } $ S = 10 ; $ N = 16 ; $ M = 2 ; survival ( $ S , $ N , $ M ) ; ? >