codeparrot/xlcost-text-to-code huggingface dataset:
Average of a stream of numbers | Returns the new average after including x ; Prints average of a stream of numbers ; Driver Code
< ? php function getAvg ( $ prev_avg , $ x , $ n ) { return ( $ prev_avg * $ n + $ x ) / ( $ n + 1 ) ; } function streamAvg ( $ arr , $ n ) { $ avg = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ avg = getAvg ( $ avg , $ arr [ $ i ] , $ i ) ; echo " Average ▁ of ▁ " , $ i + 1 , " numbers ▁ is ▁ " , $ avg , " STRNEWLINE " ; } return ; } $ arr = array ( 10 , 20 , 30 , 40 , 50 , 60 ) ; $ n = sizeof ( $ arr ) ; streamAvg ( $ arr , $ n ) ; ? >
Average of a stream of numbers | Returns the new average after including x ; Prints average of a stream of numbers ; Driver Code
< ? php function getAvg ( $ x ) { static $ sum ; static $ n ; $ sum += $ x ; return ( ( ( float ) $ sum ) / ++ $ n ) ; } function streamAvg ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ avg = getAvg ( $ arr [ $ i ] ) ; echo " Average ▁ of ▁ " . ( $ i + 1 ) . " ▁ numbers ▁ is ▁ " . $ avg . " ▁ STRNEWLINE " ; } return ; } $ arr = array ( 10 , 20 , 30 , 40 , 50 , 60 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; streamAvg ( $ arr , $ n ) ; ? >
Space and time efficient Binomial Coefficient | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } $ n = 8 ; $ k = 2 ; echo " ▁ Value ▁ of ▁ C ▁ ( $ n , ▁ $ k ) ▁ is ▁ " , binomialCoeff ( $ n , $ k ) ; ? >
Efficient program to print all prime factors of a given number | function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
< ? php function primeFactors ( $ n ) { while ( $ n % 2 == 0 ) { echo 2 , " ▁ " ; $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { while ( $ n % $ i == 0 ) { echo $ i , " " ; $ n = $ n / $ i ; } } if ( $ n > 2 ) echo $ n , " ▁ " ; } $ n = 315 ; primeFactors ( $ n ) ; ? >
Print all possible combinations of r elements in a given array of size n | The main function that prints all combinations of size r in arr [ ] of size n . This function mainly uses combinationUtil ( ) ; A temporary array to store all combination one by one ; Print all combination using temprary array ' data [ ] ' ; arr [ ] -- -> Input Array data [ ] -- -> Temporary array to store current combination start & end -- -> Staring and Ending indexes in arr [ ] index -- -> Current index in data [ ] r -- -> Size of a combination to be printed ; Current combination is ready to be printed , print it ; replace index with all possible elements . The condition " end - i + 1 ▁ > = ▁ ▁ r - index " makes sure that including one element at index will make a combination with remaining elements at remaining positions ; Driver Code
< ? php function printCombination ( $ arr , $ n , $ r ) { $ data = array ( ) ; combinationUtil ( $ arr , $ data , 0 , $ n - 1 , 0 , $ r ) ; } function combinationUtil ( $ arr , $ data , $ start , $ end , $ index , $ r ) { if ( $ index == $ r ) { for ( $ j = 0 ; $ j < $ r ; $ j ++ ) echo $ data [ $ j ] ; echo " STRNEWLINE " ; return ; } for ( $ i = $ start ; $ i <= $ end && $ end - $ i + 1 >= $ r - $ index ; $ i ++ ) { $ data [ $ index ] = $ arr [ $ i ] ; combinationUtil ( $ arr , $ data , $ i + 1 , $ end , $ index + 1 , $ r ) ; } } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ r = 3 ; $ n = sizeof ( $ arr ) ; printCombination ( $ arr , $ n , $ r ) ; ? >
Print all possible combinations of r elements in a given array of size n | The main function that prints all combinations of size r in arr [ ] of size n . This function mainly uses combinationUtil ( ) ; A temporary array to store all combination one by one ; Print all combination using temprary array ' data [ ] ' ; arr [ ] -- -> Input Array n -- -> Size of input array r -- -> Size of a combination to be printed index -- -> Current index in data [ ] data [ ] -- -> Temporary array to store current combination i -- -> index of current element in arr [ ] ; Current cobination is ready , print it ; When no more elements are there to put in data [ ] ; current is included , put next at next location ; current is excluded , replace it with next ( Note that i + 1 is passed , but index is not changed ) ; Driver Code
< ? php function printCombination ( $ arr , $ n , $ r ) { $ data = Array ( ) ; combinationUtil ( $ arr , $ n , $ r , 0 , $ data , 0 ) ; } function combinationUtil ( $ arr , $ n , $ r , $ index , $ data , $ i ) { if ( $ index == $ r ) { for ( $ j = 0 ; $ j < $ r ; $ j ++ ) echo $ data [ $ j ] , " ▁ " ; echo " STRNEWLINE " ; return ; } if ( $ i >= $ n ) return ; $ data [ $ index ] = $ arr [ $ i ] ; combinationUtil ( $ arr , $ n , $ r , $ index + 1 , $ data , $ i + 1 ) ; combinationUtil ( $ arr , $ n , $ r , $ index , $ data , $ i + 1 ) ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ r = 3 ; $ n = sizeof ( $ arr ) ; printCombination ( $ arr , $ n , $ r ) ; ? >
Count all possible groups of size 2 or 3 that have sum as multiple of 3 | Returns count of all possible groups that can be formed from elements of a [ ] . ; Create an array C [ 3 ] to store counts of elements with remainder 0 , 1 and 2. c [ i ] would store count of elements with remainder i ; To store the result ; Count elements with remainder 0 , 1 and 2 ; Case 3. a : Count groups of size 2 from 0 remainder elements ; Case 3. b : Count groups of size 2 with one element with 1 remainder and other with 2 remainder ; Case 4. a : Count groups of size 3 with all 0 remainder elements ; Case 4. b : Count groups of size 3 with all 1 remainder elements ; Case 4. c : Count groups of size 3 with all 2 remainder elements ; Case 4. c : Count groups of size 3 with different remainders ; Return total count stored in res ; Driver Code
< ? php function findgroups ( $ arr , $ n ) { $ c = array ( 0 , 0 , 0 ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ c [ $ arr [ $ i ] % 3 ] += 1 ; $ res += ( ( $ c [ 0 ] * ( $ c [ 0 ] - 1 ) ) >> 1 ) ; $ res += $ c [ 1 ] * $ c [ 2 ] ; $ res += ( $ c [ 0 ] * ( $ c [ 0 ] - 1 ) * ( $ c [ 0 ] - 2 ) ) / 6 ; $ res += ( $ c [ 1 ] * ( $ c [ 1 ] - 1 ) * ( $ c [ 1 ] - 2 ) ) / 6 ; $ res += ( ( $ c [ 2 ] * ( $ c [ 2 ] - 1 ) * ( $ c [ 2 ] - 2 ) ) / 6 ) ; $ res += $ c [ 0 ] * $ c [ 1 ] * $ c [ 2 ] ; return $ res ; } $ arr = array ( 3 , 6 , 7 , 2 , 9 ) ; $ n = count ( $ arr ) ; echo " Required ▁ number ▁ of ▁ groups ▁ are ▁ " . ( int ) ( findgroups ( $ arr , $ n ) ) ; ? >
Smallest power of 2 greater than or equal to n | Program to find smallest power of 2 greater than or equal to n ; Driver Code
< ? php function nextPowerOf2 ( $ n ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ; while ( $ n != 0 ) { $ n >>= 1 ; $ count += 1 ; } return 1 << $ count ; } $ n = 5 ; echo ( nextPowerOf2 ( $ n ) ) ; ? >
Smallest power of 2 greater than or equal to n | Finds next power of two for n . If n itself is a power of two then returns n ; Driver Code
< ? php function nextPowerOf2 ( $ n ) { $ n -- ; $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; $ n ++ ; return $ n ; } $ n = 5 ; echo nextPowerOf2 ( $ n ) ; ? >
Segregate 0 s and 1 s in an array | Function to segregate 0 s and 1 s ; Counts the no of zeros in arr ; Loop fills the arr with 0 until count ; Loop fills remaining arr space with 1 ; Function to print segregated array ; Driver Code
< ? php function segregate0and1 ( & $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 0 ) $ count ++ ; } for ( $ i = 0 ; $ i < $ count ; $ i ++ ) $ arr [ $ i ] = 0 ; for ( $ i = $ count ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = 1 ; } function toprint ( & $ arr , $ n ) { echo ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ n ) ; toprint ( $ arr , $ n ) ; ? >
Segregate 0 s and 1 s in an array | Function to put all 0 s on left and all 1 s on right ; Initialize left and right indexes ; Increment left index while we see 0 at left ; Decrement right index while we see 1 at right ; If left is smaller than right then there is a 1 at left and a 0 at right . Exchange arr [ left ] and arr [ right ] ; Driver code
< ? php function segregate0and1 ( & $ arr , $ size ) { $ left = 0 ; $ right = $ size - 1 ; while ( $ left < $ right ) { while ( $ arr [ $ left ] == 0 && $ left < $ right ) $ left ++ ; while ( $ arr [ $ right ] == 1 && $ left < $ right ) $ right -- ; if ( $ left < $ right ) { $ arr [ $ left ] = 0 ; $ arr [ $ right ] = 1 ; $ left ++ ; $ right -- ; } } } $ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ arr_size ) ; printf ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( $ i = 0 ; $ i < 6 ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; ? >
Segregate 0 s and 1 s in an array | Function to put all 0 s on left and all 1 s on right ; Driver Code
< ? php function segregate0and1 ( & $ arr , $ size ) { $ type0 = 0 ; $ type1 = $ size - 1 ; while ( $ type0 < $ type1 ) { if ( $ arr [ $ type0 ] == 1 ) { $ temp = $ arr [ $ type0 ] ; $ arr [ $ type0 ] = $ arr [ $ type1 ] ; $ arr [ $ type1 ] = $ temp ; $ type1 -- ; } else $ type0 ++ ; } } $ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ arr_size ) ; echo ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; ? >
Given an array arr [ ] , find the maximum j | For a given array arr [ ] , returns the maximum j a i such that arr [ j ] > arr [ i ] ; Driver Code
< ? php function maxIndexDiff ( $ arr , $ n ) { $ maxDiff = -1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { for ( $ j = $ n - 1 ; $ j > $ i ; -- $ j ) { if ( $ arr [ $ j ] > $ arr [ $ i ] && $ maxDiff < ( $ j - $ i ) ) $ maxDiff = $ j - $ i ; } } return $ maxDiff ; } $ arr = array ( 9 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 18 , 0 ) ; $ n = count ( $ arr ) ; $ maxDiff = maxIndexDiff ( $ arr , $ n ) ; echo $ maxDiff ; ? >
k | Function to find k - th missing element ; interating over the array ; check if i - th and ( i + 1 ) - th element are not consecutive ; save their difference ; check for difference and given k ; if found ; Input array ; k - th missing element to be found in the array ; calling function to find missing element
< ? php function missingK ( & $ a , $ k , $ n ) { $ difference = 0 ; $ ans = 0 ; $ count = $ k ; $ flag = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ difference = 0 ; if ( ( $ a [ $ i ] + 1 ) != $ a [ $ i + 1 ] ) { $ difference += ( $ a [ $ i + 1 ] - $ a [ $ i ] ) - 1 ; if ( $ difference >= $ count ) { $ ans = $ a [ $ i ] + $ count ; $ flag = 1 ; break ; } else $ count -= $ difference ; } } if ( $ flag ) return $ ans ; else return -1 ; } $ a = array ( 1 , 5 , 11 , 19 ) ; $ k = 11 ; $ n = count ( $ a ) ; $ missing = missingK ( $ a , $ k , $ n ) ; echo $ missing ; ? >
Minimum rotations required to get the same string | Returns count of rotations to get the same string back . ; tmp is the concatenated string . ; substring from i index of original string size . ; if substring matches with original string then we will come out of the loop . ; Driver code
< ? php function findRotations ( $ str ) { $ tmp = ( $ str + $ str ) ; $ n = strlen ( $ str ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ substring = $ tmp . substr ( $ i , strlen ( $ str ) ) ; if ( $ str == $ substring ) return $ i ; } return $ n ; } $ str = " abc " ; echo findRotations ( $ str ) , " STRNEWLINE " ; ? >
k | Function to find the sum of minimum of all subarrays ; Insert all the elements in a set ; Find the maximum and minimum element ; Traverse from the minimum to maximum element ; Check if " i " is missing ; Check if it is kth missing ; If no kth element is missing ; Driver code
< ? php function findKth ( $ arr , $ n , $ k ) { $ missing = array ( ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) array_push ( $ missing , $ arr [ $ i ] ) ; $ missing = array_unique ( $ missing ) ; $ maxm = max ( $ arr ) ; $ minm = min ( $ arr ) ; for ( $ i = $ minm + 1 ; $ i < $ maxm ; $ i ++ ) { if ( ! in_array ( $ i , $ missing , false ) ) $ count += 1 ; if ( $ count == $ k ) return $ i ; } return -1 ; } $ arr = array ( 2 , 10 , 9 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 5 ; echo findKth ( $ arr , $ n , $ k ) ; ? >
Number of Binary Strings of length N with K adjacent Set Bits | Function to find the number of Bit Strings of length N with K adjacent set bits ; Base Case when we form bit string of length n ; if f ( bit string ) = k , count this way ; Check if the last bit was set , if it was set then call for next index by incrementing the adjacent bit count else just call the next index with same value of adjacent bit count and either set the bit at current index or let it remain unset ; set the bit at currentIndex ; unset the bit at currentIndex ; Driver Code ; total ways = ( ways by placing 1 st bit as 1 + ways by placing 1 st bit as 0 )
< ? php function waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex , $ adjacentSetBits , $ lastBit ) { if ( $ currentIndex == $ n ) { if ( $ adjacentSetBits == $ k ) return 1 ; return 0 ; } $ noOfWays = 0 ; if ( $ lastBit == 1 ) { $ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits + 1 , 1 ) ; $ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits , 0 ) ; } else if ( ! $ lastBit ) { $ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits , 1 ) ; $ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits , 0 ) ; } return $ noOfWays ; } $ n = 5 ; $ k = 2 ; $ totalWays = waysToKAdjacentSetBits ( $ n , $ k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( $ n , $ k , 1 , 0 , 0 ) ; echo " Number ▁ of ▁ ways ▁ = ▁ " , $ totalWays , " STRNEWLINE " ; ? >
Count ways to reach the nth stair using step 1 , 2 or 3 | Returns count of ways to reach n - th stair using 1 or 2 or 3 steps . ; Driver code
< ? php function findStep ( $ n ) { if ( $ n == 1 $ n == 0 ) return 1 ; else if ( $ n == 2 ) return 2 ; else return findStep ( $ n - 3 ) + findStep ( $ n - 2 ) + findStep ( $ n - 1 ) ; } $ n = 4 ; echo findStep ( $ n ) ; ? >
Partition problem | DP | A utility function that returns true if there is a subset of arr [ ] with sun equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of the elements in array ; If sum is odd , there cannot be two subsets with equal sum ; Find if there is subset with sum equal to half of total sum ; Driver Code ; Function call
< ? php function isSubsetSum ( $ arr , $ n , $ sum ) { if ( $ sum == 0 ) return true ; if ( $ n == 0 && $ sum != 0 ) return false ; if ( $ arr [ $ n - 1 ] > $ sum ) return isSubsetSum ( $ arr , $ n - 1 , $ sum ) ; return isSubsetSum ( $ arr , $ n - 1 , $ sum ) || isSubsetSum ( $ arr , $ n - 1 , $ sum - $ arr [ $ n - 1 ] ) ; } function findPartiion ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ sum % 2 != 0 ) return false ; return isSubsetSum ( $ arr , $ n , $ sum / 2 ) ; } $ arr = array ( 3 , 1 , 5 , 9 , 12 ) ; $ n = count ( $ arr ) ; if ( findPartiion ( $ arr , $ n ) == true ) echo " Can ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ; else echo " Can ▁ not ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ; ? >
Find repeated character present first in a string | PHP program to find the first character that is repeated ; this is O ( N ^ 2 ) method ; Driver code
< ? php function findRepeatFirstN2 ( $ s ) { $ p = -1 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { for ( $ j = ( $ i + 1 ) ; $ j < strlen ( $ s ) ; $ j ++ ) { if ( $ s [ $ i ] == $ s [ $ j ] ) { $ p = $ i ; break ; } } if ( $ p != -1 ) break ; } return $ p ; } $ str = " geeksforgeeks " ; $ pos = findRepeatFirstN2 ( $ str ) ; if ( $ pos == -1 ) echo ( " Not ▁ found " ) ; else echo ( $ str [ $ pos ] ) ; ? >
Count number of strings ( made of R , G and B ) using given combination | Function to calculate number of strings ; Store factorial of numbers up to n for further computation ; Find the remaining values to be added ; Make all possible combinations of R , B and G for the remaining value ; Compute permutation of each combination one by one and add them . ; Return total no . of strings / permutation ; Driver Code
< ? php function possibleStrings ( $ n , $ r , $ b , $ g ) { $ fact [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ fact [ $ i ] = $ fact [ $ i - 1 ] * $ i ; $ left = $ n - ( $ r + $ g + $ b ) ; $ sum = 0 ; for ( $ i = 0 ; $ i <= $ left ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ left - $ i ; $ j ++ ) { $ k = $ left - ( $ i + $ j ) ; $ sum = $ sum + $ fact [ $ n ] / ( $ fact [ $ i + $ r ] * $ fact [ $ j + $ b ] * $ fact [ $ k + $ g ] ) ; } } return $ sum ; } $ n = 4 ; $ r = 2 ; $ b = 0 ; $ g = 1 ; echo possibleStrings ( $ n , $ r , $ b , $ g ) ; ? >
Remove minimum number of characters so that two strings become anagram | function to calculate minimum numbers of characters to be removed to make two strings anagram ; make hash array for both string and calculate frequency of each character ; count frequency of each character in first string ; count frequency of each character in second string ; traverse count arrays to find number of characters to be removed ; Driver Code
< ? php function remAnagram ( $ str1 , $ str2 ) { $ count1 = array ( 26 ) ; $ count2 = array ( 26 ) ; for ( $ i = 0 ; $ i < strlen ( $ str1 ) ; $ i ++ ) $ count1 [ $ str1 [ $ i ] - ' a ' ] ++ ; for ( $ i = 0 ; $ i < strlen ( $ str2 ) ; $ i ++ ) $ count2 [ $ str2 [ $ i ] - ' a ' ] ++ ; $ result = 0 ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ result += abs ( $ count1 [ $ i ] - $ count2 [ $ i ] ) ; return $ result ; } { $ str1 = " bcadeh " ; $ str2 = " hea " ; echo ( remAnagram ( $ str1 , $ str2 ) ) ; }
Print path from root to all nodes in a Complete Binary Tree | Function to print path of all the nodes nth node represent as given node kth node represents as left and right node ; base condition if kth node value is greater then nth node then its means kth node is not valid so we not store it into the res simply we just return ; Storing node into res ; Print the path from root to node ; store left path of a tree So for left we will go node ( kThNode * 2 ) ; right path of a tree and for right we will go node ( kThNode * 2 + 1 ) ; Function to print path from root to all of the nodes ; res is for store the path from root to particulate node ; Print path from root to all node . third argument 1 because of we have to consider root node is 1 ; Given Node ; Print path from root to all node .
< ? php function printPath ( $ res , $ nThNode , $ kThNode ) { if ( $ kThNode > $ nThNode ) return ; array_push ( $ res , $ kThNode ) ; for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo $ res [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; printPath ( $ res , $ nThNode , $ kThNode * 2 ) ; printPath ( $ res , $ nThNode , $ kThNode * 2 + 1 ) ; } function printPathToCoverAllNodeUtil ( $ nThNode ) { $ res = array ( ) ; printPath ( $ res , $ nThNode , 1 ) ; } $ nThNode = 7 ; printPathToCoverAllNodeUtil ( $ nThNode ) ; ? >
Minimum length of the shortest path of a triangle | function to get the minimum length of the shorter side of the triangle ; traversing through each points on the plane ; if sum of a points is greater than the previous one , the maximum gets replaced ; print the length ; initialize the number of points ; points on the plane
< ? php function shortestLength ( $ n , & $ x , & $ y ) { $ answer = 0 ; $ i = 0 ; while ( $ n -- ) { if ( $ x [ $ i ] + $ y [ $ i ] > $ answer ) $ answer = $ x [ $ i ] + $ y [ $ i ] ; $ i ++ ; } echo " Length ▁ - > ▁ " . $ answer . " STRNEWLINE " ; echo " Path ▁ - > ▁ " . " ( 1 , " ▁ . $ answer ▁ . " ) " . STRNEWLINE TABSYMBOL TABSYMBOL " and ( " ▁ . $ answer ▁ . ▁ " , 1 ) " ; } $ n = 4 ; $ x = array ( 1 , 4 , 2 , 1 ) ; $ y = array ( 4 , 1 , 1 , 2 ) ; shortestLength ( $ n , $ x , $ y ) ; ? >
Intersecting rectangle when bottom | function to find intersection rectangle of given two rectangles . ; gives bottom - left point of intersection rectangle ; gives top - right point of intersection rectangle ; no intersection ; gives top - left point of intersection rectangle ; gives bottom - right point of intersection rectangle ; bottom - left and top - right corners of first rectangle ; bottom - left and top - right corners of first rectangle ; function call
< ? php function FindPoints ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 ) { $ x5 = max ( $ x1 , $ x3 ) ; $ y5 = max ( $ y1 , $ y3 ) ; $ x6 = min ( $ x2 , $ x4 ) ; $ y6 = min ( $ y2 , $ y4 ) ; if ( $ x5 > $ x6 $ y5 > $ y6 ) { echo " No ▁ intersection " ; return ; } echo " ( " . $ x5 . " , " ▁ . ▁ $ y5 ▁ . ▁ " ) " ; echo " ( " . $ x6 . " , " ▁ . ▁ $ y6 ▁ . ▁ " ) " ; $ x7 = $ x5 ; $ y7 = $ y6 ; echo " ( " . $ x7 . " , " ▁ . ▁ $ y7 ▁ . ▁ " ) " ; $ x8 = $ x6 ; $ y8 = $ y5 ; echo " ( " . $ x8 . " , " ▁ . ▁ $ y8 ▁ . ▁ " ) " ; } $ x1 = 0 ; $ y1 = 0 ; $ x2 = 10 ; $ y2 = 8 ; $ x3 = 2 ; $ y3 = 3 ; $ x4 = 7 ; $ y4 = 9 ; FindPoints ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 ) ; ? >
Area of triangle formed by the axes of co | Function to find area ; Driver code
< ? php function area ( $ a , $ b , $ c ) { $ d = abs ( ( $ c * $ c ) / ( 2 * $ a * $ b ) ) ; return $ d ; } $ a = -2 ; $ b = 4 ; $ c = 3 ; echo area ( $ a , $ b , $ c ) ; ? >
Sum of two numbers where one number is represented as array of digits | Function to return the vector containing the answer ; Vector v is to store each digits sum and vector ans is to store the answer ; No carry in the beginning ; Start loop from the end and take element one by one ; Array index and last digit of number ; Maintain carry of summation ; Push the digit value into the array ; K value is greater then 0 ; Push digits of K one by one in the array ; Also maintain carry with summation ; Reverse the elements of vector v and store it in vector ans ; Driver code ; Print the answer
< ? php function addToArrayForm ( $ A , $ K ) { $ v = array ( ) ; $ ans = array ( ) ; $ rem = 0 ; $ i = 0 ; for ( $ i = count ( $ A ) - 1 ; $ i >= 0 ; $ i -- ) { $ my = $ A [ $ i ] + $ K % 10 + $ rem ; if ( $ my > 9 ) { $ rem = 1 ; array_push ( $ v , $ my % 10 ) ; } else { array_push ( $ v , $ my ) ; $ rem = 0 ; } $ K = floor ( $ K / 10 ) ; } while ( $ K > 0 ) { $ my = $ K % 10 + $ rem ; array_push ( $ v , $ my % 10 ) ; if ( $ my / 10 > 0 ) $ rem = 1 ; else $ rem = 0 ; $ K = floor ( $ K / 10 ) ; } if ( $ rem > 0 ) array_push ( $ v , $ rem ) ; for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) array_push ( $ ans , $ v [ $ i ] ) ; return $ ans ; } $ A = array ( 2 , 7 , 4 ) ; $ K = 181 ; $ ans = addToArrayForm ( $ A , $ K ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) echo $ ans [ $ i ] ; ? >
Third last digit in 5 ^ N for given N | Function to find the element ; if n < 3 ; If n is even return 6 If n is odd return 1 ; Driver code
< ? php function findThirdDigit ( $ n ) { if ( $ n < 3 ) return 0 ; return $ n & 1 ? 1 : 6 ; } $ n = 7 ; echo findThirdDigit ( $ n ) ; ? >
Probability of A winning the match when individual probabilities of hitting the target given | Function to return the probability of A winning ; p and q store the values of fractions a / b and c / d ; To store the winning probability of A ; Driver code
< ? php function getProbability ( $ a , $ b , $ c , $ d ) { $ p = $ a / $ b ; $ q = $ c / $ d ; $ ans = $ p * ( 1 / ( 1 - ( 1 - $ q ) * ( 1 - $ p ) ) ) ; return round ( $ ans , 6 ) ; } $ a = 1 ; $ b = 2 ; $ c = 10 ; $ d = 11 ; echo getProbability ( $ a , $ b , $ c , $ d ) ; ? >