codeparrot/xlcost-text-to-code huggingface dataset:
Variation in Nim Game | Function to return final grundy Number ( G ) of game ; if pile size is odd ; We XOR pile size + 1 ; We XOR pile size - 1 ; Game with 3 piles ; pile with different sizes ; Function to return result of game ; if ( $res == 0 ) if G is zero ; else if G is non zero
< ? php function solve ( $ p , $ n ) { $ G = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ p [ $ i ] & 1 ) $ G ^= ( $ p [ $ i ] + 1 ) ; $ G ^= ( $ p [ $ i ] - 1 ) ; } return $ G ; } $ n = 3 ; $ p = array ( 32 , 49 , 58 ) ; $ res = solve ( $ p , $ n ) ; echo " Player ▁ 2 ▁ wins " ; echo " Player ▁ 1 ▁ wins " ; ? >
Maximum AND value of a pair 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 ; 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 ; $ count ; for ( $ bit = 31 ; $ bit >= 0 ; $ bit -- ) { $ count = checkBit ( $ res | ( 1 << $ bit ) , $ arr , $ n ) ; if ( $ count >= 2 ) $ res |= ( 1 << $ bit ) ; } return $ res ; } $ arr = array ( 4 , 8 , 6 , 2 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ AND ▁ Value ▁ = ▁ " , maxAND ( $ arr , $ n ) ; ? >
Check if a number is power of 8 or not | function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; Driver Code
< ? php function checkPowerof8 ( $ n ) { $ i = log ( $ n ) / log ( 8 ) ; return ( $ i - floor ( $ i ) < 0.000001 ) ; } $ n = 65 ; if ( checkPowerof8 ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Check if binary representation of a given number and its complement are anagram | Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code
< ? php function bit_anagram_check ( $ a ) { $ longSize = 8 ; $ ULL_SIZE = 8 * $ longSize ; return ( BitCount ( $ a ) == ( $ ULL_SIZE >> 1 ) ) ; } function BitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; } $ a = 4294967295 ; echo ( bit_anagram_check ( $ a ) ) ; ? >
Sum of numbers with exactly 2 bits set | To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver Code
< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; ( 1 << $ i ) < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { $ num = ( 1 << $ i ) + ( 1 << $ j ) ; if ( $ num <= $ n ) $ sum += $ num ; } } return $ sum ; } $ n = 10 ; echo findSum ( $ n ) ; ? >
Position of rightmost different bit | function to find rightmost different bit in two numbers . ; Driver code
< ? php function posOfRightMostDiffBit ( $ m , $ n ) { $ t = floor ( log ( $ m ^ $ n , 2 ) ) ; return $ t ; } $ m = 52 ; $ n = 4 ; echo " Position ▁ = ▁ " , posOfRightMostDiffBit ( $ m , $ n ) ; ? >
Set the K | function to set the kth bit ; kth bit of n is being set by this operation ; Driver Code
< ? php function setKthBit ( $ n , $ k ) { return ( ( 1 << $ k ) $ n ) ; } $ n = 10 ; $ k = 2 ; echo " Kth ▁ bit ▁ set ▁ number ▁ = ▁ " , setKthBit ( $ n , $ k ) ; ? >
Reverse an array without using subtract sign â €˜ | Function to reverse array ; Trick to assign - 1 to a variable ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value ; Drivers code ; print the reversed array
< ? php function reverseArray ( & $ arr , $ n ) { $ x = ( PHP_INT_MIN / PHP_INT_MAX ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) swap ( $ arr , $ i , $ n + ( $ x * $ i ) + $ x ) ; } function swap ( & $ arr , $ i , $ j ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; return $ arr ; } $ arr = array ( 5 , 3 , 7 , 2 , 1 , 6 ) ; $ n = sizeof ( $ arr ) ; reverseArray ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ;
Reverse an array without using subtract sign â €˜ | Function to reverse array ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value Note : A - B = A + ~ B + 1 So n - i = n + ~ i + 1 then n - i - 1 = ( n + ~ i + 1 ) + 1 + 1 ; Driver code ; print the reversed array
< ? php function reverseArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { swap ( $ arr , $ i , ( $ n + ~ $ i + 1 ) + ~ 1 + 1 ) ; } } function swap ( & $ arr , $ i , $ j ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; return $ arr ; } { $ arr = array ( 5 , 3 , 7 , 2 , 1 , 6 ) ; $ n = sizeof ( $ arr ) ; reverseArray ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo ( $ arr [ $ i ] . " " ) ; } }
Maximum XOR value of a pair from a range | method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver Code
< ? php function maxXORInRange ( $ L , $ R ) { $ LXR = $ L ^ $ R ; $ msbPos = 0 ; while ( $ LXR ) { $ msbPos ++ ; $ LXR >>= 1 ; } $ maxXOR = 0 ; $ two = 1 ; while ( $ msbPos -- ) { $ maxXOR += $ two ; $ two <<= 1 ; } return $ maxXOR ; } $ L = 8 ; $ R = 20 ; echo maxXORInRange ( $ L , $ R ) , " STRNEWLINE " ; ? >
Numbers whose bitwise OR and sum with N are equal | Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to N , whose bitwise OR and SUM with N are equal . ; count number of zero bit in N ; power of 2 to count ; Driver code
< ? php function CountZeroBit ( $ n ) { $ count = 0 ; while ( $ n ) { if ( ! ( $ n & 1 ) ) $ count ++ ; $ n >>= 1 ; } return $ count ; } function CountORandSumEqual ( $ N ) { $ count = CountZeroBit ( $ N ) ; return ( 1 << $ count ) ; } $ N = 10 ; echo CountORandSumEqual ( $ N ) ; ? >
Count smaller numbers whose XOR with n produces greater value | PHP program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Initialize result ; If current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code
< ? php function countNumbers ( $ n ) { $ k = 0 ; $ count = 0 ; while ( $ n > 0 ) { if ( ( $ n & 1 ) == 0 ) $ count += pow ( 2 , $ k ) ; $ k += 1 ; $ n >>= 1 ; } return $ count ; } $ n = 11 ; echo countNumbers ( $ n ) ; ? >
Multiples of 4 ( An Interesting Method ) | Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Printing multiples of 4 using above method
< ? php function isMultipleOf4 ( $ n ) { if ( $ n == 1 ) return false ; $ XOR = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ XOR = $ XOR ^ $ i ; return ( $ XOR == $ n ) ; } for ( $ n = 0 ; $ n <= 42 ; $ n ++ ) if ( isMultipleOf4 ( $ n ) ) echo $ n , " ▁ " ; ? >
Multiples of 4 ( An Interesting Method ) | Returns true if n is a multiple of 4. ; Printing multiples of 4 using above method
< ? php function isMultipleOf4 ( $ n ) { if ( $ n == 0 ) return true ; return ( ( ( $ n >> 2 ) << 2 ) == $ n ) ; } for ( $ n = 0 ; $ n <= 42 ; $ n ++ ) if ( isMultipleOf4 ( $ n ) ) echo $ n , " ▁ " ; ? >
Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
< ? php function countSetBits ( $ x ) { $ count = 0 ; while ( $ x ) { $ x &= ( $ x - 1 ) ; $ count ++ ; } return $ count ; } function ceilLog2 ( $ x ) { $ count = 0 ; $ x -- ; while ( $ x > 0 ) { $ x = $ x >> 1 ; $ count ++ ; } return $ count ; } function isBleak ( $ n ) { for ( $ x = $ n - ceilLog2 ( $ n ) ; $ x < $ n ; $ x ++ ) if ( $ x + countSetBits ( $ x ) == $ n ) return false ; return true ; } if ( isBleak ( 3 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; if ( isBleak ( 4 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Count strings with consecutive 1 's | Returns count of n length binary strings with consecutive 1 's ; Count binary strings without consecutive 1 's.  See the approach discussed on be  ( http:goo.gl/p8A3sW ) ; Subtract a [ n - 1 ] + b [ n - 1 ] from 2 ^ n ; Driver Code
< ? php function countStrings ( $ n ) { $ a [ $ n ] = 0 ; $ b [ $ n ] = 0 ; $ a [ 0 ] = $ b [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ a [ $ i ] = $ a [ $ i - 1 ] + $ b [ $ i - 1 ] ; $ b [ $ i ] = $ a [ $ i - 1 ] ; } return ( 1 << $ n ) - $ a [ $ n - 1 ] - $ b [ $ n - 1 ] ; } echo countStrings ( 5 ) , " STRNEWLINE " ; ? >
Distance of nearest cell having 1 in a binary matrix | PHP program to find distance of nearest cell having 1 in a binary matrix . ; Print the distance of nearest cell having 1 for each cell . ; Initialize the answer matrix with INT_MAX . ; For each cell ; Traversing the whole matrix to find the minimum distance . ; If cell contain 1 , check for minimum distance . ; Printing the answer . ; Driver Code
< ? php $ N = 3 ; $ M = 4 ; function printDistance ( $ mat ) { global $ N , $ M ; $ ans = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ M ; $ j ++ ) $ ans [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ M ; $ j ++ ) { for ( $ k = 0 ; $ k < $ N ; $ k ++ ) for ( $ l = 0 ; $ l < $ M ; $ l ++ ) { if ( $ mat [ $ k ] [ $ l ] == 1 ) $ ans [ $ i ] [ $ j ] = min ( $ ans [ $ i ] [ $ j ] , abs ( $ i - $ k ) + abs ( $ j - $ l ) ) ; } } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) echo $ ans [ $ i ] [ $ j ] , " ▁ " ; echo " STRNEWLINE " ; } } $ mat = array ( array ( 0 , 0 , 0 , 1 ) , array ( 0 , 0 , 1 , 1 ) , array ( 0 , 1 , 1 , 0 ) ) ; printDistance ( $ mat ) ; ? >
Given level order traversal of a Binary Tree , check if the Tree is a Min | Returns true if given level order traversal is Min Heap . ; First non leaf node is at index ( n / 2 - 1 ) . Check whether each parent is greater than child ; Left child will be at index 2 * i + 1 Right child will be at index 2 * i + 2 ; If parent is greater than right child ; Driver code
< ? php function isMinHeap ( $ level , $ n ) { for ( $ i = ( $ n / 2 - 1 ) ; $ i >= 0 ; $ i -- ) { if ( $ level [ $ i ] > $ level [ 2 * $ i + 1 ] ) return false ; if ( 2 * $ i + 2 < $ n ) { if ( $ level [ $ i ] > $ level [ 2 * $ i + 2 ] ) return false ; } } return true ; } $ level = array ( 10 , 15 , 14 , 25 , 30 ) ; $ n = sizeof ( $ level ) ; if ( isMinHeap ( $ level , $ n ) ) echo " True " ; else echo " False " ;
Most frequent element in an array | PHP program to find the most frequent element in an array . ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver Code
< ? php function mostFrequent ( $ arr , $ n ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ max_count = 1 ; $ res = $ arr [ 0 ] ; $ curr_count = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ arr [ $ i - 1 ] ) $ curr_count ++ ; else { if ( $ curr_count > $ max_count ) { $ max_count = $ curr_count ; $ res = $ arr [ $ i - 1 ] ; } $ curr_count = 1 ; } } if ( $ curr_count > $ max_count ) { $ max_count = $ curr_count ; $ res = $ arr [ $ n - 1 ] ; } return $ res ; } { $ arr = array ( 1 , 5 , 2 , 1 , 3 , 2 , 1 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo mostFrequent ( $ arr , $ n ) ; return 0 ; } ? >
How to check if two given sets are disjoint ? | Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver Code
< ? php function areDisjoint ( $ set1 , $ set2 , $ m , $ n ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ set1 [ $ i ] == $ set2 [ $ j ] ) return false ; return true ; } $ set1 = array ( 12 , 34 , 11 , 9 , 3 ) ; $ set2 = array ( 7 , 2 , 1 , 5 ) ; $ m = sizeof ( $ set1 ) ; $ n = sizeof ( $ set2 ) ; if ( areDisjoint ( $ set1 , $ set2 , $ m , $ n ) == true ) echo " Yes " ; else echo " ▁ No " ; ? >
Find elements which are present in first array and not in second | Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver code
< ? php function findMissing ( $ a , $ b , $ n , $ m ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ a [ $ i ] == $ b [ $ j ] ) break ; if ( $ j == $ m ) echo $ a [ $ i ] , " ▁ " ; } } $ a = array ( 1 , 2 , 6 , 3 , 4 , 5 ) ; $ b = array ( 2 , 4 , 3 , 1 , 0 ) ; $ n = count ( $ a ) ; $ m = count ( $ b ) ; findMissing ( $ a , $ b , $ n , $ m ) ; ? >
Check if two arrays are equal or not | Returns true if arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] contain same elements . ; If lengths of array are not equal means array are not equal ; Sort both arrays ; Linearly compare elements ; If all elements were same . ; Driver Code
< ? php function areEqual ( $ arr1 , $ arr2 , $ n , $ m ) { if ( $ n != $ m ) return false ; sort ( $ arr1 ) ; sort ( $ arr2 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr1 [ $ i ] != $ arr2 [ $ i ] ) return false ; return true ; } $ arr1 = array ( 3 , 5 , 2 , 5 , 2 ) ; $ arr2 = array ( 2 , 3 , 5 , 5 , 2 ) ; $ n = count ( $ arr1 ) ; $ m = count ( $ arr2 ) ; if ( areEqual ( $ arr1 , $ arr2 , $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >
Pair with given product | Set 1 ( Find if any pair exists ) | Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver code
< ? php function isProduct ( $ arr , $ n , $ x ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] * $ arr [ $ j ] == $ x ) return true ; return false ; } $ arr = array ( 10 , 20 , 9 , 40 ) ; $ x = 400 ; $ n = count ( $ arr ) ; if ( isProduct ( $ arr , $ n , $ x ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ x = 190 ; if ( isProduct ( $ arr , $ n , $ x ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Find pair with greatest product in array | Function to find greatest number ; Driver code
< ? php function findGreatest ( $ arr , $ n ) { $ result = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n - 1 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ arr [ $ j ] * $ arr [ $ k ] == $ arr [ $ i ] ) $ result = max ( $ result , $ arr [ $ i ] ) ; return $ result ; } $ arr = array ( 30 , 10 , 9 , 3 , 35 ) ; $ n = count ( $ arr ) ; echo findGreatest ( $ arr , $ n ) ; ? >
Count pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Initialize result ; Consider all possible pairs and check their sums ; Driver Code
< ? php function getPairsCount ( $ arr , $ n , $ sum ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] + $ arr [ $ j ] == $ sum ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 5 , 7 , -1 , 5 ) ; $ n = sizeof ( $ arr ) ; $ sum = 6 ; echo " Count ▁ of ▁ pairs ▁ is ▁ " , getPairsCount ( $ arr , $ n , $ sum ) ; ? >
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; generating pairs from both the arrays ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver Code
< ? php function countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( ( $ arr1 [ $ i ] + $ arr2 [ $ j ] ) == $ x ) $ count ++ ; return $ count ; } $ arr1 = array ( 1 , 3 , 5 , 7 ) ; $ arr2 = array ( 2 , 3 , 5 , 8 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; $ x = 10 ; echo " Count ▁ = ▁ " , countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) ; ? >
Count pairs from two sorted arrays whose sum is equal to a given value x | function to search ' value ' in the given array ' arr [ ] ' it uses binary search technique as ' arr [ ] ' is sorted ; value found ; value not found ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; for each arr1 [ i ] ; check if the ' value ' is present in ' arr2 [ ] ' ; required count of pairs ; Driver Code
< ? php function isPresent ( $ arr , $ low , $ high , $ value ) { while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] == $ value ) return true ; else if ( $ arr [ $ mid ] > $ value ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return false ; } function countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ value = $ x - $ arr1 [ $ i ] ; if ( isPresent ( $ arr2 , 0 , $ n - 1 , $ value ) ) $ count ++ ; } return $ count ; } $ arr1 = array ( 1 , 3 , 5 , 7 ) ; $ arr2 = array ( 2 , 3 , 5 , 8 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; $ x = 10 ; echo " Count ▁ = ▁ " , countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) ; ? >
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if this sum is equal to ' x ' , then increment ' l ' , decrement ' r ' and increment ' count ' ; if this sum is less than x , then increment l ; else decrement ' r ' ; required count of pairs ; Driver Code
< ? php function countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) { $ count = 0 ; $ l = 0 ; $ r = $ n - 1 ; while ( $ l < $ m and $ r >= 0 ) { if ( ( $ arr1 [ $ l ] + $ arr2 [ $ r ] ) == $ x ) { $ l ++ ; $ r -- ; $ count ++ ; } else if ( ( $ arr1 [ $ l ] + $ arr2 [ $ r ] ) < $ x ) $ l ++ ; else $ r -- ; } return $ count ; } $ arr1 = array ( 1 , 3 , 5 , 7 ) ; $ arr2 = array ( 2 , 3 , 5 , 8 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; $ x = 10 ; echo " Count ▁ = ▁ " , countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) ; ? >
Count quadruples from four sorted arrays whose sum is equal to a given value x | find the ' value ' in the given array ' arr [ ] ' binary search technique is applied ; ' value ' found ; ' value ' not found ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all triplets from the 1 st three arrays ; calculate the sum of elements in the triplet so generated ; check if ' x - T ' is present in 4 th array or not ; increment count ; required count of quadruples ; four sorted arrays each of size ' n '
< ? php function isPresent ( $ arr , $ low , $ high , $ value ) { while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] == $ value ) return true ; else if ( $ arr [ $ mid ] > $ value ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return false ; } function countQuadruples ( $ arr1 , $ arr2 , $ arr3 , $ arr4 , $ n , $ x ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { $ T = $ arr1 [ $ i ] + $ arr2 [ $ j ] + $ arr3 [ $ k ] ; if ( isPresent ( $ arr4 , 0 , $ n , $ x - $ T ) ) $ count ++ ; } return $ count ; } $ arr1 = array ( 1 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 7 , 8 ) ; $ arr3 = array ( 1 , 4 , 6 , 10 ) ; $ arr4 = array ( 2 , 4 , 7 , 8 ) ; $ n = sizeof ( $ arr1 ) ; $ x = 30 ; echo " Count = " ? >
Count pairs whose products exist in array | Returns count of pairs whose product exists in arr [ ] ; find product in an array ; if product found increment counter ; return Count of all pair whose product exist in array ; Driver Code
< ? php function countPairs ( $ arr , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ product = $ arr [ $ i ] * $ arr [ $ j ] ; for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { if ( $ arr [ $ k ] == $ product ) { $ result ++ ; break ; } } } } return $ result ; } $ arr = array ( 6 , 2 , 4 , 12 , 5 , 3 ) ; $ n = sizeof ( $ arr ) ; echo countPairs ( $ arr , $ n ) ;
Master Theorem For Subtract and Conquer Recurrences | PHP code for the above approach ; Driver Code
< ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; } $ n = 9 ; echo fib ( $ n ) ; ? >
Tail Recursion | A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver program to test above function
< ? php function factTR ( $ n , $ a ) { if ( $ n == 0 ) return $ a ; return factTR ( $ n - 1 , $ n * $ a ) ; } function fact ( $ n ) { return factTR ( $ n , 1 ) ; } echo fact ( 5 ) ; ? >
Given two unsorted arrays , find all pairs whose sum is x | Function to print all pairs in both arrays whose sum is equal to given value x ; Driver code
< ? php function findPairs ( $ arr1 , $ arr2 , $ n , $ m , $ x ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ arr1 [ $ i ] + $ arr2 [ $ j ] == $ x ) echo $ arr1 [ $ i ] . " ▁ " . $ arr2 [ $ j ] . " STRNEWLINE " ; } $ arr1 = array ( 1 , 2 , 3 , 7 , 5 , 4 ) ; $ arr2 = array ( 0 , 7 , 4 , 3 , 2 , 1 ) ; $ n = count ( $ arr1 ) ; $ m = count ( $ arr2 ) ; $ x = 8 ; findPairs ( $ arr1 , $ arr2 , $ n , $ m , $ x ) ; ? >
Find pairs in array whose sums already exist in array | Function to find pair whose sum exists in arr [ ] ; Driver code
< ? php function findPair ( $ arr , $ n ) { $ found = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { if ( $ arr [ $ i ] + $ arr [ $ j ] == $ arr [ $ k ] ) { echo $ arr [ $ i ] , " ▁ " , $ arr [ $ j ] ; $ found = true ; } } } } if ( $ found == false ) echo " Not ▁ exist " ; } $ arr = array ( 10 , 4 , 8 , 13 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findPair ( $ arr , $ n ) ; ? >
Find all pairs ( a , b ) in an array such that a % b = k | Function to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver Code
< ? php function printPairs ( $ arr , $ n , $ k ) { $ isPairFound = true ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i != $ j && $ arr [ $ i ] % $ arr [ $ j ] == $ k ) { echo " ( " , $ arr [ $ i ] , " , ▁ " , $ arr [ $ j ] , " ) " , " ▁ " ; $ isPairFound = true ; } } } return $ isPairFound ; } $ arr = array ( 2 , 3 , 5 , 4 , 7 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; if ( printPairs ( $ arr , $ n , $ k ) == false ) echo " No ▁ such ▁ pair ▁ exists " ; ? >
Return maximum occurring character in an input string | PHP program to output the maximum occurring character in a string ; Create array to keep the count of individual characters and initialize the array as 0 ; Construct character count array from the input string . ; Initialize max count ; Traversing through the string and maintaining the count of each character ; Driver Code
< ? php $ ASCII_SIZE = 256 ; function getMaxOccuringChar ( $ str ) { global $ ASCII_SIZE ; $ count = array_fill ( 0 , $ ASCII_SIZE , NULL ) ; $ len = strlen ( $ str ) ; $ max = 0 ; for ( $ i = 0 ; $ i < ( $ len ) ; $ i ++ ) { $ count [ ord ( $ str [ $ i ] ) ] ++ ; if ( $ max < $ count [ ord ( $ str [ $ i ] ) ] ) { $ max = $ count [ ord ( $ str [ $ i ] ) ] ; $ result = $ str [ $ i ] ; } } return $ result ; } $ str = " sample ▁ string " ; echo " Max ▁ occurring ▁ character ▁ is ▁ " . getMaxOccuringChar ( $ str ) ; ? >
Non | Simple PHP program to find first non - repeating element . ; Driver code
< ? php function firstNonRepeating ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ i != $ j && $ arr [ $ i ] == $ arr [ $ j ] ) break ; if ( $ j == $ n ) return $ arr [ $ i ] ; } return -1 ; } $ arr = array ( 9 , 4 , 9 , 6 , 7 , 4 ) ; $ n = sizeof ( $ arr ) ; echo firstNonRepeating ( $ arr , $ n ) ; ? >