codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographically smallest string formed by appending a character from the first K characters of a given string | Function to find the new string thus formed by removing characters ; new string ; Remove characters until the string is empty ; Traverse to find the smallest character in the first k characters ; append the smallest character ; removing the lexicographically smallest character from the string ; Driver code
< ? php function newString ( $ s , $ k ) { $ X = " " ; while ( strlen ( $ s ) > 0 ) { $ temp = $ s [ 0 ] ; for ( $ i = 1 ; $ i < $ k && $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] < $ temp ) { $ temp = $ s [ $ i ] ; } } $ X = $ X . $ temp ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) { if ( $ s [ $ i ] == $ temp ) { $ s = substr ( $ s , 0 , $ i ) . substr ( $ s , $ i + 1 , strlen ( $ s ) ) ; break ; } } } return $ X ; } $ s = " gaurang " ; $ k = 3 ; echo ( newString ( $ s , $ k ) ) ; ? >
Convert String into Binary Sequence | utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Driver code
< ? php function strToBinary ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ val = ord ( $ s [ $ i ] ) ; $ bin = " " ; while ( $ val > 0 ) { ( $ val % 2 ) ? $ bin = $ bin . '1' : $ bin = $ bin . '0' ; $ val = floor ( $ val / 2 ) ; } for ( $ x = strlen ( $ bin ) - 1 ; $ x >= 0 ; $ x -- ) echo $ bin [ $ x ] ; echo " ▁ " ; } } $ s = " geeks " ; strToBinary ( $ s ) ; ? >
Convert a sentence into its equivalent mobile numeric keypad sequence | Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; storing the sequence in array
< ? php function printSequence ( & $ arr , $ input ) { $ output = " " ; $ n = strlen ( $ input ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ input [ $ i ] == ' ▁ ' ) $ output = $ output + "0" ; else { $ position = ord ( $ input [ $ i ] ) - ord ( ' A ' ) ; $ output = $ output . $ arr [ $ position ] ; } } return $ output ; } $ str = array ( "2" , "22" , "222" , "3" , "33" , "333" , "4" , "44" , "444" , "5" , "55" , "555" , "6" , "66" , "666" , "7" , "77" , "777" , "7777" , "8" , "88" , "888" , "9" , "99" , "999" , "9999" ) ; $ input = " GEEKSFORGEEKS " ; echo printSequence ( $ str , $ input ) ; ? >
Check whether given floating point number is even or odd | Function to check even or odd . ; Loop to traverse number from LSB ; We ignore trailing 0 s after dot ; If it is ' . ' we will check next digit and it means decimal part is traversed . ; If digit is divisible by 2 means even number . ; Driver Code
< ? php function isEven ( $ s ) { $ l = strlen ( $ s ) ; $ dotSeen = false ; for ( $ i = $ l - 1 ; $ i >= 0 ; $ i -- ) { if ( $ s [ $ i ] == '0' && $ dotSeen == false ) continue ; if ( $ s [ $ i ] == ' . ' ) { $ dotSeen = true ; continue ; } if ( ( $ s [ $ i ] - '0' ) % 2 == 0 ) return true ; return false ; } } $ s = "100.70" ; if ( isEven ( $ s ) ) echo " Even " ; else echo " Odd " ; ? >
Length of the smallest sub | PHP program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's count with 0 ; Increase the count in array if a character is found ; size of given string ; Find maximum distinct characters in any string ; result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Input String
< ? php $ NO_OF_CHARS = 256 ; function max_distinct_char ( $ str , $ n ) { global $ NO_OF_CHARS ; $ count = array_fill ( 0 , $ NO_OF_CHARS , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) ] ++ ; $ max_distinct = 0 ; for ( $ i = 0 ; $ i < $ NO_OF_CHARS ; $ i ++ ) if ( $ count [ $ i ] != 0 ) $ max_distinct ++ ; return $ max_distinct ; } function smallesteSubstr_maxDistictChar ( $ str ) { $ n = strlen ( $ str ) ; $ max_distinct = max_distinct_char ( $ str , $ n ) ; $ minl = $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ subs = substr ( $ str , $ i , $ j ) ; $ subs_lenght = strlen ( $ subs ) ; $ sub_distinct_char = max_distinct_char ( $ subs , $ subs_lenght ) ; if ( $ subs_lenght < $ minl && $ max_distinct == $ sub_distinct_char ) { $ minl = $ subs_lenght ; } } } return $ minl ; } $ str = " AABBBCBB " ; $ len = smallesteSubstr_maxDistictChar ( $ str ) ; echo " ▁ The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " . " ▁ consisting ▁ of ▁ maximum ▁ distinct ▁ characters ▁ : ▁ " . $ len ; ? >
Evaluate a boolean expression represented as string | Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code
< ? php function evaluateBoolExpr ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i += 2 ) { if ( ( $ i + 1 ) < $ n && $ s [ $ i + 1 ] == ' A ' ) { if ( $ s [ $ i + 2 ] == '0' $ s [ $ i ] == '0' ) $ s [ $ i + 2 ] = '0' ; else $ s [ $ i + 2 ] = '1' ; } else if ( ( $ i + 1 ) < $ n && $ s [ $ i + 1 ] == ' B ' ) { if ( $ s [ $ i + 2 ] == '1' $ s [ $ i ] == '1' ) $ s [ $ i + 2 ] = '1' ; else $ s [ $ i + 2 ] = '0' ; } else { if ( ( $ i + 2 ) < $ n && $ s [ $ i + 2 ] == $ s [ $ i ] ) $ s [ $ i + 2 ] = '0' ; else $ s [ $ i + 2 ] = '1' ; } } return $ s [ $ n - 1 ] - '0' ; } $ s = "1C1B1B0A0" ; echo evaluateBoolExpr ( $ s ) ;
Efficiently find first repeated character in a string without using any additional data structure in one traversal | Returns - 1 if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
< ? php function FirstRepeated ( $ str ) { $ checker = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; ++ $ i ) { $ val = ( ord ( $ str [ $ i ] ) - ord ( ' a ' ) ) ; if ( ( $ checker & ( 1 << $ val ) ) > 0 ) return $ i ; $ checker |= ( 1 << $ val ) ; } return -1 ; } $ s = " abcfdeacf " ; $ i = FirstRepeated ( $ s ) ; if ( $ i != -1 ) echo " Char ▁ = ▁ " . $ s [ $ i ] . " ▁ and ▁ Index ▁ = ▁ " . $ i ; else echo " No ▁ repeated ▁ Char " ; ? >
Number of even substrings in a string of digits | Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver Code
< ? php function evenNumSubstring ( $ str ) { $ len = strlen ( $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ temp = $ str [ $ i ] - '0' ; if ( $ temp % 2 == 0 ) $ count += ( $ i + 1 ) ; } return $ count ; } $ str = "1234" ; echo evenNumSubstring ( $ str ) , " STRNEWLINE " ; ? >
Find largest word in dictionary by deleting some characters of given string | Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1 ; If all characters of str1 were found in str2 ; Returns the longest string in dictionary which is a subsequence of str . ; Traverse through all words of dictionary ; If current word is subsequence of str and is largest such word so far . ; Return longest string ; Driver code
< ? php function isSubSequence ( $ str1 , $ str2 ) { $ m = strlen ( $ str1 ) ; $ n = strlen ( $ str2 ) ; for ( $ i = 0 ; $ i < $ n && $ j < $ m ; $ i ++ ) if ( $ str1 [ $ j ] == $ str2 [ $ i ] ) $ j ++ ; return ( $ j == $ m ) ; } function findLongestString ( $ dict , $ str ) { $ result = " " ; $ length = 0 ; foreach ( $ dict as $ word ) { if ( $ length < strlen ( $ word ) && isSubSequence ( $ word , $ str ) ) { $ result = $ word ; $ length = strlen ( $ word ) ; } } return $ result ; } $ dict = array ( " ale " , " apple " , " monkey " , " plea " ) ; $ str = " abpcplea " ; echo findLongestString ( $ dict , $ str ) ; ? >
Check if edit distance between two strings is one | Returns true if edit distance between s1 and s2 is one , else false ; Find lengths of given strings ; If difference between lengths is more than 1 , then strings can 't  be at one distance ; Count of edits ; If current characters don 't match ; If length of one string is more , then only possible edit is to remove a character ; If lengths of both strings is same ; Increment count of edits ; If current characters match ; If last character is extra in any string ; Driver Code
< ? php function isEditDistanceOne ( $ s1 , $ s2 ) { $ m = strlen ( $ s1 ) ; $ n = strlen ( $ s2 ) ; if ( abs ( $ m - $ n ) > 1 ) return false ; $ count = 0 ; $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ s1 [ $ i ] != $ s2 [ $ j ] ) { if ( $ count == 1 ) return false ; if ( $ m > $ n ) $ i ++ ; else if ( $ m < $ n ) $ j ++ ; else { $ i ++ ; $ j ++ ; } $ count ++ ; } else { $ i ++ ; $ j ++ ; } } if ( $ i < $ m $ j < $ n ) $ count ++ ; return $ count == 1 ; } $ s1 = " gfg " ; $ s2 = " gf " ; if ( isEditDistanceOne ( $ s1 , $ s2 ) ) echo " Yes " ; else echo " No " ; ? >
Given a binary string , count number of substrings that start and end with 1. | A simple PHP program to count number of substrings starting and ending with 1 ; Initialize result ; Pick a starting point ; Search for all possible ending point ; Driver Code
< ? php function countSubStr ( $ str ) { $ res = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == '1' ) { for ( $ j = $ i + 1 ; $ j < strlen ( $ str ) ; $ j ++ ) if ( $ str [ $ j ] == '1' ) $ res ++ ; } } return $ res ; } $ str = "00100101" ; echo countSubStr ( $ str ) ; ? >
Modular exponentiation ( Recursive ) | Recursive PHP program to compute modular power ; Base cases ; If B is even ; If B is odd ; Driver Code
< ? php function exponentMod ( $ A , $ B , $ C ) { if ( $ A == 0 ) return 0 ; if ( $ B == 0 ) return 1 ; if ( $ B % 2 == 0 ) { $ y = exponentMod ( $ A , $ B / 2 , $ C ) ; $ y = ( $ y * $ y ) % $ C ; } else { $ y = $ A % $ C ; $ y = ( $ y * exponentMod ( $ A , $ B - 1 , $ C ) % $ C ) % $ C ; } return ( ( $ y + $ C ) % $ C ) ; } $ A = 2 ; $ B = 5 ; $ C = 13 ; echo " Power ▁ is ▁ " . exponentMod ( $ A , $ B , $ C ) ; ? >
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Driver Code
< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; if ( $ x == 0 ) return 0 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; y = $ y / 2 $ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } $ x = 2 ; $ y = 5 ; $ p = 13 ; echo " Power ▁ is ▁ " , power ( $ x , $ y , $ p ) ; ? >
Area of the Largest Triangle inscribed in a Hexagon | Function to find the area of the triangle ; side cannot be negative ; area of the triangle ; Driver code
< ? php function trianglearea ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = ( 3 * sqrt ( 3 ) * pow ( $ a , 2 ) ) / 4 ; return $ area ; } $ a = 6 ; echo trianglearea ( $ a ) ; ? >
Equation of ellipse from its focus , directrix , and eccentricity | Function to find equation of ellipse . ; Driver Code
< ? php function equation_ellipse ( $ x1 , $ y1 , $ a , $ b , $ c , $ e ) { $ t = ( $ a * $ a ) + ( $ b * $ b ) ; $ a1 = $ t - $ e * ( $ a * $ a ) ; $ b1 = $ t - $ e * ( $ b * $ b ) ; $ c1 = ( -2 * $ t * $ x1 ) - ( 2 * $ e * $ c * $ a ) ; $ d1 = ( -2 * $ t * $ y1 ) - ( 2 * $ e * $ c * $ b ) ; $ e1 = -2 * $ e * $ a * $ b ; $ f1 = ( - $ e * $ c * $ c ) + ( $ t * $ x1 * $ x1 ) + ( $ t * $ y1 * $ y1 ) ; $ fixed ; echo " Equation ▁ of ▁ ellipse ▁ is ▁ STRNEWLINE " , $ a1 , " ▁ x ^ 2 ▁ + ▁ " , $ b1 , " ▁ y ^ 2 ▁ + ▁ " , $ c1 , " ▁ x ▁ + ▁ " , $ d1 , " ▁ y ▁ + ▁ " , $ e1 , " ▁ xy ▁ + ▁ " , $ f1 , " ▁ = ▁ 0" ; } $ x1 = 1 ; $ y1 = 1 ; $ a = 1 ; $ b = -1 ; $ c = 3 ; $ e = 0.5 * 0.5 ; equation_ellipse ( $ x1 , $ y1 , $ a , $ b , $ c , $ e ) ; ? >
Area of circle which is inscribed in equilateral triangle | Function return the area of circle inscribed in equilateral triangle ; Driver code
< ? php function circle_inscribed ( $ a ) { return 3.14 * ( $ a * $ a ) / 12 ; } $ a = 4 ; echo circle_inscribed ( $ a ) ;
Program to find the Volume of an irregular tetrahedron | Function to find the volume ; Steps to calculate volume of a Tetrahedron using formula ; edge lengths
< ? php function findVolume ( $ u , $ v , $ w , $ U , $ V , $ W , $ b ) { $ uPow = pow ( $ u , 2 ) ; $ vPow = pow ( $ v , 2 ) ; $ wPow = pow ( $ w , 2 ) ; $ UPow = pow ( $ U , 2 ) ; $ VPow = pow ( $ V , 2 ) ; $ WPow = pow ( $ W , 2 ) ; $ a = 4 * ( $ uPow * $ vPow * $ wPow ) - $ uPow * pow ( ( $ vPow + $ wPow - $ UPow ) , 2 ) - $ vPow * pow ( ( $ wPow + $ uPow - $ VPow ) , 2 ) - $ wPow * pow ( ( $ uPow + $ vPow - $ WPow ) , 2 ) + ( $ vPow + $ wPow - $ UPow ) * ( $ wPow + $ uPow - $ VPow ) * ( $ uPow + $ vPow - $ WPow ) ; $ vol = sqrt ( $ a ) ; $ vol /= $ b ; echo $ vol ; } $ u = 1000 ; $ v = 1000 ; $ w = 1000 ; $ U = 3 ; $ V = 4 ; $ W = 5 ; $ b = 12 ; findVolume ( $ u , $ v , $ w , $ U , $ V , $ W , $ b ) ; ? >
Check if it is possible to create a polygon with a given angle | Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer
< ? php function makePolygon ( $ a ) { $ n = 360 / ( 180 - $ a ) ; if ( $ n == ( int ) $ n ) echo " YES " ; else echo " NO " ; } $ a = 90 ; makePolygon ( $ a ) ; ? >
Finding Quadrant of a Coordinate with respect to a Circle | Thus function returns the quadrant number ; Coincides with center ; Outside circle ; 1 st quadrant ; 2 nd quadrant ; 3 rd quadrant ; 4 th quadrant ; Coordinates of centre ; Radius of circle ; Coordinates of the given po$
< ? php function getQuadrant ( $ X , $ Y , $ R , $ PX , $ PY ) { if ( $ PX == $ X and $ PY == $ Y ) return 0 ; $ val = pow ( ( $ PX - $ X ) , 2 ) + pow ( ( $ PY - $ Y ) , 2 ) ; if ( $ val > pow ( $ R , 2 ) ) return -1 ; if ( $ PX > $ X and $ PY >= $ Y ) return 1 ; if ( $ PX <= $ X and $ PY > $ Y ) return 2 ; if ( $ PX < $ X and $ PY <= $ Y ) return 3 ; if ( $ PX >= $ X and $ PY < $ Y ) return 4 ; } $ X = 0 ; $ Y = 3 ; $ R = 2 ; $ PX = 1 ; $ PY = 4 ; $ ans = getQuadrant ( $ X , $ Y , $ R , $ PX , $ PY ) ; if ( $ ans == -1 ) echo " Lies ▁ Outside ▁ the ▁ circle " ; else if ( $ ans == 0 ) echo " Coincides ▁ with ▁ centre " ; else echo $ ans , " ▁ Quadrant " ; ? >
Hexadecagonal number | Function to calculate hexadecagonal number ; Driver Code
< ? php function hexadecagonalNum ( $ n ) { return ( ( 14 * $ n * $ n ) - 12 * $ n ) / 2 ; } $ n = 5 ; echo $ n , " th ▁ Hexadecagonal ▁ number ▁ : ▁ " ; echo hexadecagonalNum ( $ n ) ; echo " STRNEWLINE " ; $ n = 9 ; echo $ n , " th ▁ Hexadecagonal ▁ number ▁ : ▁ " ; echo hexadecagonalNum ( $ n ) ;
Find the perimeter of a cylinder | Function to calculate perimeter ; Driver Code
< ? php function perimeter ( $ diameter , $ height ) { return 2 * ( $ diameter + $ height ) ; } $ diameter = 5 ; $ height = 10 ; echo ( " Perimeter ▁ = ▁ " ) ; echo ( perimeter ( $ diameter , $ height ) ) ; echo ( " ▁ units " ) ; ? >
Find all possible coordinates of parallelogram | coordinates of A ; coordinates of B ; coordinates of C
< ? php $ ax = 5 ; $ ay = 0 ; $ bx = 1 ; $ by = 1 ; $ cx = 2 ; $ cy = 5 ; echo $ ax + $ bx - $ cx , " , ▁ " , $ ay + $ by - $ cy , " STRNEWLINE " ; echo $ ax + $ cx - $ bx , " , ▁ " , $ ay + $ cy - $ by , " STRNEWLINE " ; echo $ cx + $ bx - $ ax , " , ▁ " , $ cy + $ by - $ ax ; ? >
Check whether a given point lies inside a rectangle or not | A utility function to calculate area of triangle formed by ( x1 , y1 ) , ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the rectangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) , C ( x3 , y3 ) and D ( x4 , y4 ) ; Calculate area of rectangle ABCD ; Calculate area of triangle PAB ; Calculate area of triangle PBC ; Calculate area of triangle PCD ; Calculate area of triangle PAD ; Check if sum of A1 , A2 , A3 and A4 is same as A ; Let us check whether the point P ( 10 , 15 ) lies inside the rectangle formed by A ( 0 , 10 ) , B ( 10 , 0 ) C ( 0 , - 10 ) D ( - 10 , 0 )
< ? php function area ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 ) { return abs ( ( $ x1 * ( $ y2 - $ y3 ) + $ x2 * ( $ y3 - $ y1 ) + $ x3 * ( $ y1 - $ y2 ) ) / 2.0 ) ; } function check ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 , $ x , $ y ) { $ A = area ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 ) + area ( $ x1 , $ y1 , $ x4 , $ y4 , $ x3 , $ y3 ) ; $ A1 = area ( $ x , $ y , $ x1 , $ y1 , $ x2 , $ y2 ) ; $ A2 = area ( $ x , $ y , $ x2 , $ y2 , $ x3 , $ y3 ) ; $ A3 = area ( $ x , $ y , $ x3 , $ y3 , $ x4 , $ y4 ) ; $ A4 = area ( $ x , $ y , $ x1 , $ y1 , $ x4 , $ y4 ) ; return ( $ A == $ A1 + $ A2 + $ A3 + $ A4 ) ; } if ( check ( 0 , 10 , 10 , 0 , 0 , -10 , -10 , 0 , 10 , 15 ) ) echo " yes " ; else echo " no " ; ? >
Pizza cut problem ( Or Circle Division by Lines ) | Function for finding maximum pieces with n cuts . ; Driver code
< ? php function findMaximumPieces ( $ n ) { return 1 + $ n * ( $ n + 1 ) / 2 ; } echo findMaximumPieces ( 3 ) ; ? >
A Space Optimized DP solution for 0 | val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 ] [ W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; one by one traverse each element ; traverse all weights j <= W ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; check for each value ; include element ; exclude element ; if i is even that mean till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 ] [ W ] if n is odd , else mat [ 1 ] [ W ] ; Driver Code
< ? php function KnapSack ( & $ val , & $ wt , $ n , $ W ) { $ mat = array_fill ( 0 , 2 , array_fill ( 0 , $ W + 1 , NULL ) ) ; $ i = 0 ; while ( $ i < $ n ) { $ j = 0 ; if ( $ i % 2 != 0 ) { { while ( ++ $ j <= $ W ) if ( $ wt [ $ i ] <= $ j ) $ mat [ 1 ] [ $ j ] = max ( $ val [ $ i ] + $ mat [ 0 ] [ $ j - $ wt [ $ i ] ] , $ mat [ 0 ] [ $ j ] ) ; else $ mat [ 1 ] [ $ j ] = $ mat [ 0 ] [ $ j ] ; } } else { while ( ++ $ j <= $ W ) { if ( $ wt [ $ i ] <= $ j ) $ mat [ 0 ] [ $ j ] = max ( $ val [ $ i ] + $ mat [ 1 ] [ $ j - $ wt [ $ i ] ] , $ mat [ 1 ] [ $ j ] ) ; else $ mat [ 0 ] [ $ j ] = $ mat [ 1 ] [ $ j ] ; } } $ i ++ ; } if ( $ n % 2 != 0 ) return $ mat [ 0 ] [ $ W ] ; else return $ mat [ 1 ] [ $ W ] ; } $ val = array ( 7 , 8 , 4 ) ; $ wt = array ( 3 , 8 , 6 ) ; $ W = 10 ; $ n = 3 ; echo KnapSack ( $ val , $ wt , $ n , $ W ) . " STRNEWLINE " ; ? >
Find original numbers from gcd ( ) every pair | PHP implementation of the approach ; Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ; Driver code
< ? php function gcd ( $ a , $ b ) { return ( $ a % $ b ) ? gcd ( $ b , $ a % $ b ) : $ b ; } function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } function findNumbers ( $ arr , $ n ) { rsort ( $ arr ) ; $ freq = array_fill ( 0 , $ arr [ 0 ] + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ $ arr [ $ i ] ] ++ ; $ size = floor ( sqrt ( $ n ) ) ; $ brr = array_fill ( 0 , $ size , 0 ) ; $ l = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ freq [ $ arr [ $ i ] ] > 0 ) { $ brr [ $ l ] = $ arr [ $ i ] ; $ freq [ $ brr [ $ l ] ] -- ; $ l ++ ; for ( $ j = 0 ; $ j < $ l ; $ j ++ ) { if ( $ i != $ j ) { $ x = gcd ( $ arr [ $ i ] , $ brr [ $ j ] ) ; $ freq [ $ x ] -= 2 ; } } } } printArr ( $ brr , $ size ) ; } $ arr = array ( 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 5 , 5 , 5 , 7 , 10 , 12 , 2 , 2 ) ; $ n = count ( $ arr ) ; findNumbers ( $ arr , $ n ) ; ? >
Rearrange Odd and Even values in Alternate Fashion in Ascending Order | PHP implementation of the above approach ; Sort the array ; $v1 = array ( ) ; to insert even values $v2 = array ( ) ; to insert odd values ; Set flag to true if first element is even ; Start rearranging array ; If first element is even ; Else , first element is Odd ; Print the rearranged array ; Driver code
< ? php function AlternateRearrange ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 == 0 ) array_push ( $ v1 , $ arr [ $ i ] ) ; else array_push ( $ v2 , $ arr [ $ i ] ) ; $ index = 0 ; $ i = 0 ; $ j = 0 ; $ flag = false ; if ( $ arr [ 0 ] % 2 == 0 ) $ flag = true ; while ( $ index < $ n ) { if ( $ flag == true ) { $ arr [ $ index ++ ] = $ v1 [ $ i ++ ] ; $ flag = ! $ flag ; } else { $ arr [ $ index ++ ] = $ v2 [ $ j ++ ] ; $ flag = ! $ flag ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 9 , 8 , 13 , 2 , 19 , 14 ) ; $ n = sizeof ( $ arr ) ; AlternateRearrange ( $ arr , $ n ) ; ? >
Convert given array to Arithmetic Progression by adding an element | Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible  to add another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code
< ? php function getNumToAdd ( $ arr , $ n ) { sort ( $ arr ) ; $ d = $ arr [ 1 ] - $ arr [ 0 ] ; $ numToAdd = -1 ; $ numAdded = false ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ diff = $ arr [ $ i ] - $ arr [ $ i - 1 ] ; if ( $ diff != $ d ) { if ( $ numAdded ) return -1 ; if ( $ diff == 2 * $ d ) { $ numToAdd = $ arr [ $ i ] - $ d ; $ numAdded = true ; } else return -1 ; } } if ( $ numToAdd == -1 ) return ( $ arr [ $ n - 1 ] + $ d ) ; return $ numToAdd ; } $ arr = array ( 1 , 3 , 5 , 7 , 11 , 13 , 15 ) ; $ n = sizeof ( $ arr ) ; echo getNumToAdd ( $ arr , $ n ) ; ? >
Get maximum items when other items of total cost of an item are free | Function to count the total number of items ; Sort the prices ; Choose the last element ; Initial count of item ; Sum to keep track of the total price of free items ; If total is less than or equal to z then we will add 1 to the answer ; Driver code
< ? php function items ( $ n , $ a ) { sort ( $ a ) ; $ z = $ a [ $ n - 1 ] ; $ x = 1 ; $ s = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ s += $ a [ $ i ] ; if ( $ s <= $ z ) $ x += 1 ; else break ; } return $ x ; } $ n = 5 ; $ a = array ( 5 , 3 , 1 , 5 , 6 ) ; echo items ( $ n , $ a ) ; ? >
Minimum boxes required to carry all gifts | Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver program
< ? php function numBoxes ( $ A , $ n , $ K ) { sort ( $ A ) ; $ i = 0 ; $ j = $ n - 1 ; $ ans = 0 ; while ( $ i <= $ j ) { $ ans ++ ; if ( $ A [ $ i ] + $ A [ $ j ] <= $ K ) $ i ++ ; $ j -- ; } return $ ans ; } $ A = array ( 3 , 2 , 2 , 1 ) ; $ K = 3 ; $ n = sizeof ( $ A ) / sizeof ( $ A [ 0 ] ) ; echo numBoxes ( $ A , $ n , $ K ) ; ? >