codeparrot/xlcost-text-to-code huggingface dataset:
Shortest Un | bool function for checking an array elements are in increasing . ; bool function for checking an array elements are in decreasing . ; increasing and decreasing are two functions . if function return true value then print 0 otherwise 3. ; Driver code
< ? php function increasing ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ a [ $ i ] >= $ a [ $ i + 1 ] ) return false ; return true ; } function decreasing ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ a [ $ i ] < $ a [ $ i + 1 ] ) return false ; return true ; } function shortestUnsorted ( $ a , $ n ) { if ( increasing ( $ a , $ n ) == true || decreasing ( $ a , $ n ) == true ) return 0 ; else return 3 ; } $ ar = array ( 7 , 9 , 10 , 8 , 11 ) ; $ n = sizeof ( $ ar ) ; echo shortestUnsorted ( $ ar , $ n ) ; ? >
Union and Intersection of two sorted arrays | Function prints union of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Print remaining elements of the larger array ; Driver Code
< ? php function printUnion ( $ arr1 , $ arr2 , $ m , $ n ) { $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ arr1 [ $ i ] < $ arr2 [ $ j ] ) echo ( $ arr1 [ $ i ++ ] . " ▁ " ) ; else if ( $ arr2 [ $ j ] < $ arr1 [ $ i ] ) echo ( $ arr2 [ $ j ++ ] . " ▁ " ) ; else { echo ( $ arr2 [ $ j ++ ] . " " ) ; $ i ++ ; } } while ( $ i < $ m ) echo ( $ arr1 [ $ i ++ ] . " ▁ " ) ; while ( $ j < $ n ) echo ( $ arr2 [ $ j ++ ] . " ▁ " ) ; } $ arr1 = array ( 1 , 2 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 5 , 7 ) ; $ m = sizeof ( $ arr1 ) ; $ n = sizeof ( $ arr2 ) ; printUnion ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >
Union and Intersection of two sorted arrays | Function prints Intersection of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Driver Code ; Function calling
< ? php function printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) { $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ arr1 [ $ i ] < $ arr2 [ $ j ] ) $ i ++ ; else if ( $ arr2 [ $ j ] < $ arr1 [ $ i ] ) $ j ++ ; else { echo $ arr2 [ $ j ] , " " ; $ i ++ ; $ j ++ ; } } } $ arr1 = array ( 1 , 2 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 5 , 7 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >
Find Union and Intersection of two unsorted arrays | Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be presen in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver program to test above function ; Function call
< ? php function printUnion ( $ arr1 , $ arr2 , $ m , $ n ) { if ( $ m > $ n ) { $ tempp = $ arr1 ; $ arr1 = $ arr2 ; $ arr2 = $ tempp ; $ temp = $ m ; $ m = $ n ; $ n = $ temp ; } sort ( $ arr1 ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ arr1 [ $ i ] . " ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( binarySearch ( $ arr1 , 0 , $ m - 1 , $ arr2 [ $ i ] ) == -1 ) echo $ arr2 [ $ i ] . " ▁ " ; } function printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) { if ( $ m > $ n ) { $ tempp = $ arr1 ; $ arr1 = $ arr2 ; $ arr2 = $ tempp ; $ temp = $ m ; $ m = $ n ; $ n = $ temp ; } sort ( $ arr1 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( binarySearch ( $ arr1 , 0 , $ m - 1 , $ arr2 [ $ i ] ) != -1 ) echo $ arr2 [ $ i ] . " ▁ " ; } function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = ( int ) ( $ l + ( $ r - $ l ) / 2 ) ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } $ arr1 = array ( 7 , 1 , 5 , 2 , 3 , 6 ) ; $ arr2 = array ( 3 , 8 , 6 , 20 , 7 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; echo " Union ▁ of ▁ two ▁ arrays ▁ is ▁ STRNEWLINE " ; printUnion ( $ arr1 , $ arr2 , $ m , $ n ) ; echo " Intersection of two arrays is " ; printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >
Find Union and Intersection of two unsorted arrays | Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call
< ? php function intersection ( $ a , $ b , $ n , $ m ) { $ i = 0 ; $ j = 0 ; while ( $ i < $ n && $ j < $ m ) { if ( $ a [ $ i ] > $ b [ $ j ] ) { $ j ++ ; } else if ( $ b [ $ j ] > $ a [ $ i ] ) { $ i ++ ; } else { echo ( $ a [ $ i ] . " " ) ; $ i ++ ; $ j ++ ; } } } $ a = array ( 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 ) ; $ b = array ( 3 , 3 , 5 ) ; $ n = sizeof ( $ a ) ; $ m = sizeof ( $ b ) ; sort ( $ a ) ; sort ( $ b ) ; intersection ( $ a , $ b , $ n , $ m ) ; ? >
Sort an array of 0 s , 1 s and 2 s | UTILITY FUNCTIONS ; Sort the input array , the array is assumed to have values in { 0 , 1 , 2 } ; Utility function to print array arr [ ] ; Driver Code
< ? php function swap ( & $ a , & $ b ) { $ temp = $ a ; $ a = $ b ; $ b = $ temp ; } function sort012 ( & $ a , $ arr_size ) { $ lo = 0 ; $ hi = $ arr_size - 1 ; $ mid = 0 ; while ( $ mid <= $ hi ) { switch ( $ a [ $ mid ] ) { case 0 : swap ( $ a [ $ lo ++ ] , $ a [ $ mid ++ ] ) ; break ; case 1 : $ mid ++ ; break ; case 2 : swap ( $ a [ $ mid ] , $ a [ $ hi -- ] ) ; break ; } } } function printArray ( & $ arr , $ arr_size ) { for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ arr = array ( 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; sort012 ( $ arr , $ arr_size ) ; echo " array ▁ after ▁ segregation ▁ " ; printArray ( $ arr , $ arr_size ) ; ? >
Find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted | PHP program to find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted ; step 1 ( a ) of above algo ; step 1 ( b ) of above algo ; step 2 ( a ) of above algo ; step 2 ( b ) of above algo ; step 2 ( c ) of above algo ; step 3 of above algo
< ? php function printUnsorted ( & $ arr , $ n ) { $ s = 0 ; $ e = $ n - 1 ; for ( $ s = 0 ; $ s < $ n - 1 ; $ s ++ ) { if ( $ arr [ $ s ] > $ arr [ $ s + 1 ] ) break ; } if ( $ s == $ n - 1 ) { echo " The ▁ complete ▁ array ▁ is ▁ sorted " ; return ; } for ( $ e = $ n - 1 ; $ e > 0 ; $ e -- ) { if ( $ arr [ $ e ] < $ arr [ $ e - 1 ] ) break ; } $ max = $ arr [ $ s ] ; $ min = $ arr [ $ s ] ; for ( $ i = $ s + 1 ; $ i <= $ e ; $ i ++ ) { if ( $ arr [ $ i ] > $ max ) $ max = $ arr [ $ i ] ; if ( $ arr [ $ i ] < $ min ) $ min = $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ s ; $ i ++ ) { if ( $ arr [ $ i ] > $ min ) { $ s = $ i ; break ; } } for ( $ i = $ n - 1 ; $ i >= $ e + 1 ; $ i -- ) { if ( $ arr [ $ i ] < $ max ) { $ e = $ i ; break ; } } echo " ▁ The ▁ unsorted ▁ subarray ▁ which ▁ makes ▁ " . " the ▁ given ▁ array ▁ " . " STRNEWLINE " . " ▁ sorted ▁ lies ▁ between ▁ the ▁ indees ▁ " . $ s . " ▁ and ▁ " . $ e ; return ; } $ arr = array ( 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 ) ; $ arr_size = sizeof ( $ arr ) ; printUnsorted ( $ arr , $ arr_size ) ; ? >
Count the number of possible triangles | Function to count all possible triangles with arr [ ] element ; Sort the array elements in non - decreasing order ; Initialize count of triangles ; Fix the first element . We need to run till n - 3 as the other two elements are selected from arr [ i + 1. . . n - 1 ] ; Initialize index of the rightmost third element ; Fix the second element ; Find the rightmost element which is smaller than the sum of two fixed elements . The important thing to note here is , we use the previous value of k . If value of arr [ i ] + arr [ j - 1 ] was greater than arr [ k ] , then arr [ i ] + arr [ j ] must be greater than k , because the array is sorted . ; Total number of possible triangles that can be formed with the two fixed elements is k - j - 1. The two fixed elements are arr [ i ] and arr [ j ] . All elements between arr [ j + 1 ] to arr [ k - 1 ] can form a triangle with arr [ i ] and arr [ j ] . One is subtracted from k because k is incremented one extra in above while loop . k will always be greater than j . If j becomes equal to k , then above loop will increment k , because arr [ k ] + arr [ i ] is always / greater than arr [ k ] ; Driver code
< ? php function findNumberOfTriangles ( $ arr ) { $ n = count ( $ arr ) ; sort ( $ arr ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; ++ $ i ) { $ k = $ i + 2 ; for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { while ( $ k < $ n && $ arr [ $ i ] + $ arr [ $ j ] > $ arr [ $ k ] ) ++ $ k ; if ( $ k > $ j ) $ count += $ k - $ j - 1 ; } } return $ count ; } $ arr = array ( 10 , 21 , 22 , 100 , 101 , 200 , 300 ) ; echo " Total ▁ number ▁ of ▁ triangles ▁ is ▁ " , findNumberOfTriangles ( $ arr ) ; ? >
Count all distinct pairs with difference equal to k | A simple PHP program to count pairs with difference k ; Pick all elements one by one ; See if there is a pair of this picked element ; Driver Code
< ? php function countPairsWithDiffK ( $ arr , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] - $ arr [ $ j ] == $ k or $ arr [ $ j ] - $ arr [ $ i ] == $ k ) $ count ++ ; } return $ count ; } $ arr = array ( 1 , 5 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( $ arr , $ n , $ k ) ; ? >
Count all distinct pairs with difference equal to k | Standard binary search function ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; Pick a first element point ; Driver Code
< ? php function binarySearch ( $ arr , $ low , $ high , $ x ) { if ( $ high >= $ low ) { $ mid = $ low + ( $ high - $ low ) / 2 ; if ( $ x == $ arr [ $ mid ] ) return $ mid ; if ( $ x > $ arr [ $ mid ] ) return binarySearch ( $ arr , ( $ mid + 1 ) , $ high , $ x ) ; else return binarySearch ( $ arr , $ low , ( $ mid - 1 ) , $ x ) ; } return -1 ; } function countPairsWithDiffK ( $ arr , $ n , $ k ) { $ count = 0 ; $ i ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( binarySearch ( $ arr , $ i + 1 , $ n - 1 , $ arr [ $ i ] + $ k ) != -1 ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 5 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( $ arr , $ n , $ k ) ; ? >
Count all distinct pairs with difference equal to k | Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; arr [ r ] - arr [ l ] < k ; Driver Code
< ? php function countPairsWithDiffK ( $ arr , $ n , $ k ) { $ count = 0 ; sort ( $ arr ) ; $ l = 0 ; $ r = 0 ; while ( $ r < $ n ) { if ( $ arr [ $ r ] - $ arr [ $ l ] == $ k ) { $ count ++ ; $ l ++ ; $ r ++ ; } else if ( $ arr [ $ r ] - $ arr [ $ l ] > $ k ) $ l ++ ; else $ r ++ ; } return $ count ; } $ arr = array ( 1 , 5 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( $ arr , $ n , $ k ) ; ? >
Construct an array from its pair | Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver Code
< ? php function constructArr ( $ pair ) { $ arr = array ( ) ; $ n = 5 ; $ arr [ 0 ] = intval ( ( $ pair [ 0 ] + $ pair [ 1 ] - $ pair [ $ n - 1 ] ) / 2 ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ pair [ $ i - 1 ] - $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ pair = array ( 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 ) ; constructArr ( $ pair ) ; ? >
Merge two sorted arrays with O ( 1 ) extra space | Merge ar1 [ ] and ar2 [ ] with O ( 1 ) extra space ; Iterate through all elements of ar2 [ ] starting from the last element ; Find the smallest element greater than ar2 [ i ] . Move all elements one position ahead till the smallest greater element is not found ; If there was a greater element ; Driver program
< ? php function merge ( & $ ar1 , & $ ar2 , $ m , $ n ) { for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ last = $ ar1 [ $ m - 1 ] ; for ( $ j = $ m - 2 ; $ j >= 0 && $ ar1 [ $ j ] > $ ar2 [ $ i ] ; $ j -- ) $ ar1 [ $ j + 1 ] = $ ar1 [ $ j ] ; if ( $ j != $ m - 2 $ last > $ ar2 [ $ i ] ) { $ ar1 [ $ j + 1 ] = $ ar2 [ $ i ] ; $ ar2 [ $ i ] = $ last ; } } } $ ar1 = array ( 1 , 5 , 9 , 10 , 15 , 20 ) ; $ ar2 = array ( 2 , 3 , 8 , 13 ) ; $ m = sizeof ( $ ar1 ) / sizeof ( $ ar1 [ 0 ] ) ; $ n = sizeof ( $ ar2 ) / sizeof ( $ ar2 [ 0 ] ) ; merge ( $ ar1 , $ ar2 , $ m , $ n ) ; echo " First Array : " for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ ar1 [ $ i ] . " ▁ " ; echo " Second Array : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ ar2 [ $ i ] . " ▁ " ; return 0 ; ? >
Gould 's Sequence | Function to generate gould 's Sequence ; loop to generate each row of pascal 's Triangle up to nth row ; Loop to generate each element of ith row ; if c is odd increment count ; print count of odd elements ; Get n ; Function call
< ? php function gouldSequence ( $ n ) { for ( $ row_num = 1 ; $ row_num <= $ n ; $ row_num ++ ) { $ count = 1 ; $ c = 1 ; for ( $ i = 1 ; $ i <= $ row_num ; $ i ++ ) { $ c = $ c * ( $ row_num - $ i ) / $ i ; if ( $ c % 2 == 1 ) $ count ++ ; } echo $ count , " " ; } } $ n = 16 ; gouldSequence ( $ n ) ; ? >
Product of maximum in first array and minimum in second | Function to calculate the product ; Sort the arrays to find the maximum and minimum elements in given arrays ; Return product of maximum and minimum . ; Driver code
< ? php function minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) { sort ( $ arr1 ) ; sort ( $ arr2 ) ; return $ arr1 [ $ n1 - 1 ] * $ arr2 [ 0 ] ; } $ arr1 = array ( 10 , 2 , 3 , 6 , 4 , 1 ) ; $ arr2 = array ( 5 , 1 , 4 , 2 , 6 , 9 ) ; $ n1 = count ( $ arr1 ) ; $ n2 = count ( $ arr2 ) ; echo minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) ; ? >
Minimum odd cost path in a matrix | PHP program to find Minimum odd cost path in a matrix ; Function to find the minimum cost ; leftmost element ; rightmost element ; Any element except leftmost and rightmost element of a row is reachable from direct upper or left upper or right upper row 's block ; Counting the minimum cost ; Find the minimum cost ; Driver code
< ? php $ M = 100 ; $ N = 100 ; function find_min_odd_cost ( $ given , $ m , $ n ) { global $ M , $ N ; $ floor1 [ $ M ] [ $ N ] = array ( array ( 0 ) , array ( 0 ) ) ; $ min_odd_cost = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ floor1 [ 0 ] [ $ j ] = $ given [ 0 ] [ $ j ] ; for ( $ i = 1 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ j == 0 ) { $ floor1 [ $ i ] [ $ j ] = $ given [ $ i ] [ $ j ] ; $ floor1 [ $ i ] [ $ j ] += min ( $ floor1 [ $ i - 1 ] [ $ j ] , $ floor1 [ $ i - 1 ] [ $ j + 1 ] ) ; } else if ( $ j == $ n - 1 ) { $ floor1 [ $ i ] [ $ j ] = $ given [ $ i ] [ $ j ] ; $ floor1 [ $ i ] [ $ j ] += min ( $ floor1 [ $ i - 1 ] [ $ j ] , $ floor1 [ $ i - 1 ] [ $ j - 1 ] ) ; } else { $ temp = min ( $ floor1 [ $ i - 1 ] [ $ j ] , $ floor1 [ $ i - 1 ] [ $ j - 1 ] ) ; $ temp = min ( $ temp , $ floor1 [ $ i - 1 ] [ $ j + 1 ] ) ; $ floor1 [ $ i ] [ $ j ] = $ given [ $ i ] [ $ j ] + $ temp ; } } $ min_odd_cost = PHP_INT_MAX ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ floor1 [ $ n - 1 ] [ $ j ] % 2 == 1 ) { if ( $ min_odd_cost > $ floor1 [ $ n - 1 ] [ $ j ] ) $ min_odd_cost = $ floor1 [ $ n - 1 ] [ $ j ] ; } } if ( $ min_odd_cost == PHP_INT_MIN ) return -1 ; return $ min_odd_cost ; } $ m = 5 ; $ n = 5 ; $ given = array ( array ( 1 , 2 , 3 , 4 , 6 ) , array ( 1 , 2 , 3 , 4 , 5 ) , array ( 1 , 2 , 3 , 4 , 5 ) , array ( 1 , 2 , 3 , 4 , 5 ) , array ( 100 , 2 , 3 , 4 , 5 ) ) ; echo " Minimum ▁ odd ▁ cost ▁ is ▁ " . find_min_odd_cost ( $ given , $ m , $ n ) ; ? >
Product of maximum in first array and minimum in second | Function to calculate the product ; Initialize max of first array ; initialize min of second array ; To find the maximum element in first array ; To find the minimum element in second array ; Process remaining elements ; Driven code
< ? php function minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) { $ max = $ arr1 [ 0 ] ; $ min = $ arr2 [ 0 ] ; $ i ; for ( $ i = 1 ; $ i < $ n1 && $ i < $ n2 ; ++ $ i ) { if ( $ arr1 [ $ i ] > $ max ) $ max = $ arr1 [ $ i ] ; if ( $ arr2 [ $ i ] < $ min ) $ min = $ arr2 [ $ i ] ; } while ( $ i < $ n1 ) { if ( $ arr1 [ $ i ] > $ max ) $ max = $ arr1 [ $ i ] ; $ i ++ ; } while ( $ i < $ n2 ) { if ( $ arr2 [ $ i ] < $ min ) $ min = $ arr2 [ $ i ] ; $ i ++ ; } return $ max * $ min ; } $ arr1 = array ( 10 , 2 , 3 , 6 , 4 , 1 ) ; $ arr2 = array ( 5 , 1 , 4 , 2 , 6 , 9 ) ; $ n1 = count ( $ arr1 ) ; $ n2 = count ( $ arr2 ) ; echo minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) ; ? >
Search , insert and delete in an unsorted array | Function to implement search operation ; Driver Code ; Using a last element as search element
< ? php function findElement ( $ arr , $ n , $ key ) { $ i ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ key ) return $ i ; return -1 ; } $ arr = array ( 12 , 34 , 10 , 6 , 40 ) ; $ n = sizeof ( $ arr ) ; $ key = 40 ; $ position = findElement ( $ arr , $ n , $ key ) ; if ( $ position == - 1 ) echo ( " Element ▁ not ▁ found " ) ; else echo ( " Element ▁ Found ▁ at ▁ Position : ▁ " . ( $ position + 1 ) ) ; ? >
Search , insert and delete in an unsorted array | Inserts a key in arr [ ] of given capacity . n is current size of arr [ ] . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver Code ; Inserting key
< ? php function insertSorted ( & $ arr , $ n , $ key , $ capacity ) { if ( $ n >= $ capacity ) return $ n ; array_push ( $ arr , $ key ) ; return ( $ n + 1 ) ; } $ arr = array ( 12 , 16 , 20 , 40 , 50 , 70 ) ; $ capacity = 20 ; $ n = 6 ; $ key = 26 ; echo " Before ▁ Insertion : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; $ n = insertSorted ( $ arr , $ n , $ key , $ capacity ) ; echo " After Insertion : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Search , insert and delete in an unsorted array | To search a key to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver code
< ? php function findElement ( & $ arr , $ n , $ key ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ key ) return $ i ; return -1 ; } function deleteElement ( & $ arr , $ n , $ key ) { $ pos = findElement ( $ arr , $ n , $ key ) ; if ( $ pos == -1 ) { echo " Element ▁ not ▁ found " ; return $ n ; } for ( $ i = $ pos ; $ i < $ n - 1 ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i + 1 ] ; return $ n - 1 ; } $ arr = array ( 10 , 50 , 30 , 40 , 20 ) ; $ n = count ( $ arr ) ; $ key = 30 ; echo " Array ▁ before ▁ deletion STRNEWLINE " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; $ n = deleteElement ( $ arr , $ n , $ key ) ; echo " Array after deletion " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Search , insert and delete in a sorted array | function to implement binary search ; low + ( high - low ) / 2 ; Driver Code Let us search 3 in below array
< ? php function binarySearch ( $ arr , $ low , $ high , $ key ) { if ( $ high < $ low ) return -1 ; $ mid = ( $ low + $ high ) / 2 ; if ( $ key == $ arr [ ( int ) $ mid ] ) return $ mid ; if ( $ key > $ arr [ ( int ) $ mid ] ) return binarySearch ( $ arr , ( $ mid + 1 ) , $ high , $ key ) ; return ( binarySearch ( $ arr , $ low , ( $ mid -1 ) , $ key ) ) ; } $ arr = array ( 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = count ( $ arr ) ; $ key = 10 ; echo " Index : ▁ " , ( int ) binarySearch ( $ arr , 0 , $ n -1 , $ key ) ; ? >
Find n | function to find nth stern ' diatomic series ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver Code
< ? php function findSDSFunc ( $ n ) { $ DP [ 0 ] = 0 ; $ DP [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ DP [ $ i ] = $ DP [ $ i / 2 ] ; else $ DP [ $ i ] = $ DP [ ( $ i - 1 ) / 2 ] + $ DP [ ( $ i + 1 ) / 2 ] ; } return $ DP [ $ n ] ; } $ n = 15 ; echo ( findSDSFunc ( $ n ) ) ; ? >
Find common elements in three sorted arrays | This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver program to test above function
< ? php function findCommon ( $ ar1 , $ ar2 , $ ar3 , $ n1 , $ n2 , $ n3 ) { $ i = 0 ; $ j = 0 ; $ k = 0 ; while ( $ i < $ n1 && $ j < $ n2 && $ k < $ n3 ) { if ( $ ar1 [ $ i ] == $ ar2 [ $ j ] && $ ar2 [ $ j ] == $ ar3 [ $ k ] ) { echo $ ar1 [ $ i ] , " " ; $ i ++ ; $ j ++ ; $ k ++ ; } else if ( $ ar1 [ $ i ] < $ ar2 [ $ j ] ) $ i ++ ; else if ( $ ar2 [ $ j ] < $ ar3 [ $ k ] ) $ j ++ ; else $ k ++ ; } } $ ar1 = array ( 1 , 5 , 10 , 20 , 40 , 80 ) ; $ ar2 = array ( 6 , 7 , 20 , 80 , 100 ) ; $ ar3 = array ( 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 ) ; $ n1 = count ( $ ar1 ) ; $ n2 = count ( $ ar2 ) ; $ n3 = count ( $ ar3 ) ; echo " Common ▁ Elements ▁ are ▁ " ; findCommon ( $ ar1 , $ ar2 , $ ar3 , $ n1 , $ n2 , $ n3 ) ; ? >
Jacobsthal and Jacobsthal | Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driver Code
< ? php function Jacobsthal ( $ n ) { $ dp [ $ n + 1 ] ; $ dp [ 0 ] = 0 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + 2 * $ dp [ $ i - 2 ] ; return $ dp [ $ n ] ; } function Jacobsthal_Lucas ( $ n ) { $ dp [ $ n + 1 ] ; $ dp [ 0 ] = 2 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + 2 * $ dp [ $ i - 2 ] ; return $ dp [ $ n ] ; } $ n = 5 ; echo " Jacobsthal ▁ number : ▁ " , Jacobsthal ( $ n ) , " STRNEWLINE " ; echo " Jacobsthal - Lucas ▁ number : ▁ " , Jacobsthal_Lucas ( $ n ) , " STRNEWLINE " ; ? >
Find position of an element in a sorted array of infinite numbers | Simple binary search algorithm ; function takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can assume size to be infinite in this function. NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , Thus use binary search between them ; Driver Code
< ? php function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = $ l + ( $ r - $ l ) / 2 ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } function findPos ( $ arr , $ key ) { $ l = 0 ; $ h = 1 ; $ val = $ arr [ 0 ] ; while ( $ val < $ key ) { $ l = $ h ; $ h = 2 * $ h ; $ val = $ arr [ $ h ] ; } return binarySearch ( $ arr , $ l , $ h , $ key ) ; } $ arr = array ( 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 ) ; $ ans = findPos ( $ arr , 10 ) ; if ( $ ans == -1 ) echo " Element ▁ not ▁ found " ; else echo " Element ▁ found ▁ at ▁ index ▁ " , $ ans ; ? >