codeparrot/xlcost-text-to-code huggingface dataset:
Find the Product of first N Prime Numbers | PHP implementation of above solution ; Create a boolean array " $ prime [ 0 . . $ n ] " and initialize all entries it as true . A value in $prime [ i ] will finally be false if i is Not a $prime , else true . ; If $prime [ $p ] is not changed , then it is a $prime ; Set all multiples of $$p to non - $prime ; find the product of 1 st N $prime numbers ; $count of $prime numbers ; product of $prime numbers ; if the number is $prime add it ; increase the $count ; get to next number ; create the sieve ; find the value of 1 st $n $prime numbers
< ? php $ MAX = 10000 ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX ; global $ prime ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function solve ( $ n ) { global $ prime ; $ count = 0 ; $ num = 1 ; $ prod = 1 ; while ( $ count < $ n ) { if ( $ prime [ $ num ] == true ) { $ prod *= $ num ; $ count ++ ; } $ num ++ ; } return $ prod ; } SieveOfEratosthenes ( ) ; $ n = 5 ; echo solve ( $ n ) ; ? >
Check if there is any pair in a given range with GCD is divisible by k | Returns count of numbers in [ l r ] that are divisible by k . ; Add 1 explicitly as l is divisible by k ; l is not divisible by k ; Driver Code
< ? php function Check_is_possible ( $ l , $ r , $ k ) { $ div_count = ( int ) ( $ r / $ k ) - ( int ) ( $ l / $ k ) ; if ( $ l % $ k == 0 ) $ div_count ++ ; return ( $ div_count > 1 ) ; } $ l = 30 ; $ r = 70 ; $ k = 10 ; if ( Check_is_possible ( $ l , $ r , $ k ) ) echo " YES STRNEWLINE " ; else echo " NO STRNEWLINE " ; ? >
Find sum of N | calculate sum of Nth group ; Driver code
< ? php function nth_group ( $ n ) { return $ n * ( 2 * pow ( $ n , 2 ) + 1 ) ; } $ N = 5 ; echo nth_group ( $ N ) ; ? >
Find if a molecule can be formed from 3 atoms using their valence numbers | Function to check if it is possible ; Driver code
< ? php function printPossible ( $ a , $ b , $ c ) { if ( ( $ a + $ b + $ c ) % 2 != 0 $ a + $ b < $ c ) echo ( " NO " ) ; else echo ( " YES " ) ; } $ a = 2 ; $ b = 4 ; $ c = 2 ; printPossible ( $ a , $ b , $ c ) ; ? >
Find the sum of first N terms of the series 2 Ã — 3 + 4 Ã — 4 + 6 Ã — 5 + 8 Ã — 6 + ... | calculate sum upto N term of series ; Driver code
< ? php function Sum_upto_nth_Term ( $ n ) { $ r = $ n * ( $ n + 1 ) * ( 2 * $ n + 7 ) / 3 ; echo $ r ; } $ N = 5 ; Sum_upto_nth_Term ( $ N ) ; ? >
Absolute Difference between the Sum of Non | Function to find the difference between the sum of non - primes and the sum of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the sum of primes in S1 and the sum of non primes in S2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Get the array ; Find the absolute difference
< ? php function CalculateDifference ( $ arr , $ n ) { $ max_val = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ max_val ) $ max_val = $ arr [ $ i ] ; } $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ S1 = 0 ; $ S2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) { $ S1 += $ arr [ $ i ] ; } else if ( $ arr [ $ i ] != 1 ) { $ S2 += $ arr [ $ i ] ; } } return abs ( $ S2 - $ S1 ) ; } $ arr = array ( 1 , 3 , 5 , 10 , 15 , 7 ) ; $ n = sizeof ( $ arr ) ; echo CalculateDifference ( $ arr , $ n ) ; ? >
Count number of integers less than or equal to N which has exactly 9 divisors | Function to count factors in O ( N ) ; iterate and check if factor or not ; Function to count numbers having exactly 9 divisors ; check for all numbers <= N ; check if exactly 9 factors or not ; Driver Code
< ? php Function numberOfDivisors ( $ num ) { $ c = 0 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { if ( $ num % $ i == 0 ) { $ c += 1 ; } } return $ c ; } Function countNumbers ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( numberOfDivisors ( $ i ) == 9 ) $ c += 1 ; } return $ c ; } $ n = 1000 ; echo countNumbers ( $ n ) ; ? >
Number of distinct integers obtained by lcm ( X , N ) / X | Function to count the number of distinct integers ontained by lcm ( x , num ) / x ; iterate to count the number of factors ; Driver Code
< ? php function numberOfDistinct ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ ans ++ ; if ( ( $ n / $ i ) != $ i ) $ ans ++ ; } } return $ ans ; } $ n = 3 ; echo numberOfDistinct ( $ n ) ; ? >
Find the number of rectangles of size 2 * 1 which can be placed inside a rectangle of size n * m | function to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; if n is even ; if m is even ; if both are odd ; Driver code ; function call
< ? php function NumberOfRectangles ( $ n , $ m ) { if ( $ n % 2 == 0 ) return ( $ n / 2 ) * $ m ; else if ( $ m % 2 == 0 ) return ( $ m / 2 ) * $ n ; return ( $ n * $ m - 1 ) / 2 ; } $ n = 3 ; $ m = 3 ; echo NumberOfRectangles ( $ n , $ m ) ; ? >
Next greater Number than N with the same quantity of digits A and B | Recursive function to find the required number ; If the resulting number is >= n and count of a = count of b , return the number ; select minimum of two and call the function again ; Function to find the number next greater Number than N with the same quantity of digits A and B ; Driver code
< ? php function findNumUtil ( $ res , $ a , $ aCount , $ b , $ bCount , $ n ) { if ( $ res > 100000000000 ) return 10000000000 ; if ( $ aCount == $ bCount && $ res >= $ n ) return $ res ; return min ( findNumUtil ( $ res * 10 + $ a , $ a , $ aCount + 1 , $ b , $ bCount , $ n ) , findNumUtil ( $ res * 10 + $ b , $ a , $ aCount , $ b , $ bCount + 1 , $ n ) ) ; } function findNum ( $ n , $ a , $ b ) { $ result = 0 ; $ aCount = 0 ; $ bCount = 0 ; return findNumUtil ( $ result , $ a , $ aCount , $ b , $ bCount , $ n ) ; } $ N = 4500 ; $ A = 4 ; $ B = 7 ; echo findNum ( $ N , $ A , $ B ) ; ? >
Minimum and maximum number of N chocolates after distribution among K students | Driver code
< ? php $ n = 7 ; $ k = 3 ; if ( $ n % $ k == 0 ) echo $ n / $ k . " ▁ " . $ n / $ k ; else echo ( ( $ n - ( $ n % $ k ) ) / $ k ) . " ▁ " . ( ( ( $ n - ( $ n % $ k ) ) / $ k ) + 1 ) ; ? >
Total money to be paid after traveling the given number of hours | PHP implementation of the above approach ; calculating hours travelled
< ? php $ m = 50 ; $ n = 5 ; $ x = 67 ; $ h = 2927 ; $ z = ( int ) ( ceil ( $ h / 60 ) ) ; if ( $ z <= $ n ) print ( $ z * $ m ) ; else print ( $ n * $ m + ( $ z - $ n ) * $ x ) ; ? >
Absolute difference between sum and product of roots of a quartic equation | Function taking coefficient of each term of equation as input ; Finding sum of roots ; Finding product of roots ; Absolute difference ; Driver code
< ? php function sumProductDifference ( $ a , $ b , $ c , $ d , $ e ) { $ rootSum = ( double ) ( -1 * $ b ) / $ a ; $ rootProduct = ( double ) $ e / $ a ; return abs ( $ rootSum - $ rootProduct ) ; } echo sumProductDifference ( 8 , 4 , 6 , 4 , 1 ) ; ? >
Number of solutions of n = x + n âŠ • x | Function to find the number of solutions of n = n xor x ; Counter to store the number of solutions found ; Driver code
< ? php function numberOfSolutions ( $ n ) { $ c = 0 ; for ( $ x = 0 ; $ x <= $ n ; ++ $ x ) if ( $ n == $ x + $ n ^ $ x ) ++ $ c ; return $ c ; } $ n = 3 ; echo numberOfSolutions ( $ n ) ;
Program to find minimum number of lectures to attend to maintain 75 % | Function to compute minimum lecture ; Formula to compute ; Driver Code
< ? php function minimumLectures ( $ m , $ n ) { $ ans = 0 ; if ( $ n < ceil ( 0.75 * $ m ) ) $ ans = ( int ) ceil ( ( ( 0.75 * $ m ) - $ n ) / 0.25 ) ; else $ ans = 0 ; return $ ans ; } $ M = 9 ; $ N = 1 ; echo minimumLectures ( $ M , $ N ) ; ? >
Count Numbers with N digits which consists of odd number of 0 's | Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code
< ? php function countNumbers ( $ N ) { return ( pow ( 10 , $ N ) - pow ( 8 , $ N ) ) / 2 ; } $ n = 5 ; echo countNumbers ( $ n ) ; ? >
Sum of the first N terms of the series 5 , 12 , 23 , 38. ... | Function to calculate the sum ; number of terms to be included in sum ; find the Sn
< ? php function calculateSum ( $ n ) { return 2 * ( $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) / 6 ) + $ n * ( $ n + 1 ) / 2 + 2 * ( $ n ) ; } $ n = 3 ; echo " Sum = " ? >
Program to find number of solutions in Quadratic Equation | Method to check for solutions of equations ; If the expression is greater than 0 , then 2 solutions ; If the expression is equal 0 , then 2 solutions ; Else no solutions ; Driver Code
< ? php function checkSolution ( $ a , $ b , $ c ) { if ( ( ( $ b * $ b ) - ( 4 * $ a * $ c ) ) > 0 ) echo "2 ▁ solutions " ; else if ( ( ( $ b * $ b ) - ( 4 * $ a * $ c ) ) == 0 ) echo "1 ▁ solution " ; else echo " No ▁ solutions " ; } $ a = 2 ; $ b = 5 ; $ c = 2 ; checkSolution ( $ a , $ b , $ c ) ; ? >
Program to convert KiloBytes to Bytes and Bits | Function to calculates the bits ; calculates Bits 1 kilobytes ( s ) = 8192 bits ; Function to calculates the bytes ; calculates Bytes 1 KB = 1024 bytes ; Driver code
< ? php function Bits ( $ kilobytes ) { $ Bits = 0 ; $ Bits = $ kilobytes * 8192 ; return $ Bits ; } function Bytes ( $ kilobytes ) { $ Bytes = 0 ; $ Bytes = $ kilobytes * 1024 ; return $ Bytes ; } $ kilobytes = 1 ; echo $ kilobytes ; echo ( " ▁ Kilobytes ▁ = ▁ " ) ; echo Bytes ( $ kilobytes ) ; echo ( " ▁ Bytes ▁ and ▁ " ) ; echo Bits ( $ kilobytes ) ; echo ( " ▁ Bits . " ) ; ? >
Program to find the Hidden Number | Getting the size of array ; Getting the array of size n ; Solution ; Finding sum of the array elements ; Dividing sum by size n ; Print x , if found
< ? php $ n = 3 ; $ a = array ( 1 , 2 , 3 ) ; $ i = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ a [ $ i ] ; } $ x = $ sum / $ n ; if ( $ x * $ n == $ sum ) echo ( $ x ) ; else echo ( " - 1" ) ; ? >
Find sum of the series ? 3 + ? 12 + ... ... ... upto N terms | Function to find the sum ; Apply AP formula ; number of terms
< ? php function findSum ( $ n ) { return sqrt ( 3 ) * ( $ n * ( $ n + 1 ) / 2 ) ; } $ n = 10 ; echo findSum ( $ n ) ; ? >
Find the sum of the series x ( x + y ) + x ^ 2 ( x ^ 2 + y ^ 2 ) + x ^ 3 ( x ^ 3 + y ^ 3 ) + ... + x ^ n ( x ^ n + y ^ n ) | Function to return required sum ; sum of first series ; sum of second series ; Driver code ; function call to print sum
< ? php function sum ( $ x , $ y , $ n ) { $ sum1 = ( pow ( $ x , 2 ) * ( pow ( $ x , 2 * $ n ) - 1 ) ) / ( pow ( $ x , 2 ) - 1 ) ; $ sum2 = ( $ x * $ y * ( pow ( $ x , $ n ) * pow ( $ y , $ n ) - 1 ) ) / ( $ x * $ y - 1 ) ; return $ sum1 + $ sum2 ; } $ x = 2 ; $ y = 2 ; $ n = 2 ; echo sum ( $ x , $ y , $ n ) ; ? >
Find any pair with given GCD and LCM | Function to print the pairs ; Driver Code
< ? php function printPair ( $ g , $ l ) { echo $ g ; echo ( " ▁ " ) ; echo $ l ; } $ g = 3 ; $ l = 12 ; printPair ( $ g , $ l ) ; ? >
Sum of first n terms of a given series 3 , 6 , 11 , ... . . | Function to calculate the sum ; starting number ; Common Ratio ; Common difference ; Nth term to be find ; find the Sn
< ? php function calculateSum ( $ n ) { $ a1 = 1 ; $ a2 = 2 ; $ r = 2 ; $ d = 1 ; return ( $ n ) * ( 2 * $ a1 + ( $ n - 1 ) * $ d ) / 2 + $ a2 * ( pow ( $ r , $ n ) - 1 ) / ( $ r - 1 ) ; } $ n = 5 ; echo " Sum = " ? >
Minimum number of sub | Function that returns true if n is a power of 5 ; Function to return the decimal value of binary equivalent ; Function to return the minimum cuts required ; Allocating memory for dp [ ] array ; From length 1 to n ; If previous character is '0' then ignore to avoid number with leading 0 s . ; Ignore s [ j ] = '0' starting numbers ; Number formed from s [ j ... . i ] ; Check for power of 5 ; Assigning min value to get min cut possible ; ( n + 1 ) to check if all the strings are traversed and no divisible by 5 is obtained like 000000 ; Driver code
< ? php function ispower ( $ n ) { if ( $ n < 125 ) return ( $ n == 1 $ n == 5 $ n == 25 ) ; if ( $ n % 125 != 0 ) return false ; else return ispower ( $ n / 125 ) ; } function number ( $ s , $ i , $ j ) { $ ans = 0 ; for ( $ x = $ i ; $ x < $ j ; $ x ++ ) { $ ans = $ ans * 2 + ( ord ( $ s [ $ x ] ) - ord ( '0' ) ) ; } return $ ans ; } function minCuts ( $ s , $ n ) { $ dp = array_fill ( 0 , $ n + 1 , $ n + 1 ) ; $ dp [ 0 ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ s [ $ i - 1 ] == '0' ) continue ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( $ s [ $ j ] == '0' ) continue ; $ num = number ( $ s , $ j , $ i ) ; if ( ! ispower ( $ num ) ) continue ; $ dp [ $ i ] = min ( $ dp [ $ i ] , $ dp [ $ j ] + 1 ) ; } } return ( ( $ dp [ $ n ] < $ n + 1 ) ? $ dp [ $ n ] : -1 ) ; } $ s = "101101101" ; $ n = strlen ( $ s ) ; echo minCuts ( $ s , $ n ) ; ? >
Minimum number of cubes whose sum equals to given number N | Function to return the minimum number of cubes whose sum is k ; If k is less than the 2 ^ 3 ; Initialize with the maximum number of cubes required ; Driver code
< ? php function MinOfCubed ( $ k ) { if ( $ k < 8 ) return $ k ; $ res = $ k ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { if ( ( $ i * $ i * $ i ) > $ k ) return $ res ; $ res = min ( $ res , MinOfCubed ( $ k - ( $ i * $ i * $ i ) ) + 1 ) ; } return $ res ; } $ num = 15 ; echo MinOfCubed ( $ num ) ; ? >
Minimum number of cubes whose sum equals to given number N | Function to return the minimum number of cubes whose sum is k ; While current perfect cube is less than current element ; If i is a perfect cube ; i = ( i - 1 ) + 1 ^ 3 ; Next perfect cube ; Re - initialization for next element ; Driver code
< ? php function MinOfCubedDP ( $ k ) { $ DP = array ( $ k + 1 ) ; $ j = 1 ; $ t = 1 ; $ DP [ 0 ] = 0 ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { $ DP [ $ i ] = PHP_INT_MAX ; while ( $ j <= $ i ) { if ( $ j == $ i ) $ DP [ $ i ] = 1 ; else if ( $ DP [ $ i ] > $ DP [ $ i - $ j ] ) $ DP [ $ i ] = $ DP [ $ i - $ j ] + 1 ; $ t ++ ; $ j = $ t * $ t * $ t ; } $ t = $ j = 1 ; } return $ DP [ $ k ] ; } $ num = 15 ; echo ( MinOfCubedDP ( $ num ) ) ; ? >
Maximum Subarray Sum after inverting at most two elements | Function to return the maximum required sub - array sum ; Creating one based indexing ; 2d array to contain solution for each step ; Case 1 : Choosing current or ( current + previous ) whichever is smaller ; Case 2 : ( a ) Altering sign and add to previous case 1 or value 0 ; Case 2 : ( b ) Adding current element with previous case 2 and updating the maximum ; Case 3 : ( a ) Altering sign and add to previous case 2 ; Case 3 : ( b ) Adding current element with previous case 3 ; Updating the maximum value of variable ans ; Return the final solution ; Driver code
< ? php function maxSum ( $ a , $ n ) { $ ans = 0 ; $ arr = array ( ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ arr [ $ i ] = $ a [ $ i - 1 ] ; $ dp = array ( array ( ) ) ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ dp [ $ i ] [ 0 ] = max ( $ arr [ $ i ] , $ dp [ $ i - 1 ] [ 0 ] + $ arr [ $ i ] ) ; $ dp [ $ i ] [ 1 ] = max ( 0 , $ dp [ $ i - 1 ] [ 0 ] ) - $ arr [ $ i ] ; if ( $ i >= 2 ) $ dp [ $ i ] [ 1 ] = max ( $ dp [ $ i ] [ 1 ] , $ dp [ $ i - 1 ] [ 1 ] + $ arr [ $ i ] ) ; if ( $ i >= 2 ) $ dp [ $ i ] [ 2 ] = $ dp [ $ i - 1 ] [ 1 ] - $ arr [ $ i ] ; if ( $ i >= 3 ) $ dp [ $ i ] [ 2 ] = max ( $ dp [ $ i ] [ 2 ] , $ dp [ $ i - 1 ] [ 2 ] + $ arr [ $ i ] ) ; $ ans = max ( $ ans , $ dp [ $ i ] [ 0 ] ) ; $ ans = max ( $ ans , $ dp [ $ i ] [ 1 ] ) ; $ ans = max ( $ ans , $ dp [ $ i ] [ 2 ] ) ; } return $ ans ; } $ arr = array ( -5 , 3 , 2 , 7 , -8 , 3 , 7 , -9 , 10 , 12 , -6 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , $ n ) ; ? >
Minimum cost to form a number X by adding up powers of 2 | Function to return the minimum cost ; Re - compute the array ; Add answers for set bits ; If bit is set ; Increase the counter ; Right shift the number ; Driver code
< ? php function MinimumCost ( $ a , $ n , $ x ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ a [ $ i ] = min ( $ a [ $ i ] , 2 * $ a [ $ i - 1 ] ) ; } $ ind = 0 ; $ sum = 0 ; while ( $ x ) { if ( $ x & 1 ) $ sum += $ a [ $ ind ] ; $ ind ++ ; $ x = $ x >> 1 ; } return $ sum ; } $ a = array ( 20 , 50 , 60 , 90 ) ; $ x = 7 ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo MinimumCost ( $ a , $ n , $ x ) ; ? >
Ways to form an array having integers in given range such that total sum is divisible by 2 | Function to return the number of ways to form an array of size n such that sum of all elements is divisible by 2 ; Represents first and last numbers of each type ( modulo 0 and 1 ) ; Count of numbers of each type between range ; Base Cases ; Ways to form array whose sum upto i numbers modulo 2 is 0 ; Ways to form array whose sum upto i numbers modulo 2 is 1 ; Return the required count of ways ; Driver Code
< ? php function countWays ( $ n , $ l , $ r ) { $ tL = $ l ; $ tR = $ r ; $ L = array_fill ( 0 , 2 , 0 ) ; $ R = array_fill ( 0 , 2 , 0 ) ; $ L [ $ l % 2 ] = $ l ; $ R [ $ r % 2 ] = $ r ; $ l ++ ; $ r -- ; if ( $ l <= $ tR && $ r >= $ tL ) { $ L [ $ l % 2 ] = $ l ; $ R [ $ r % 2 ] = $ r ; } $ cnt0 = 0 ; $ cnt1 = 0 ; if ( $ R [ 0 ] && $ L [ 0 ] ) $ cnt0 = ( $ R [ 0 ] - $ L [ 0 ] ) / 2 + 1 ; if ( $ R [ 1 ] && $ L [ 1 ] ) $ cnt1 = ( $ R [ 1 ] - $ L [ 1 ] ) / 2 + 1 ; $ dp = array ( ) ; $ dp [ 1 ] [ 0 ] = $ cnt0 ; $ dp [ 1 ] [ 1 ] = $ cnt1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] [ 0 ] = ( $ cnt0 * $ dp [ $ i - 1 ] [ 0 ] + $ cnt1 * $ dp [ $ i - 1 ] [ 1 ] ) ; $ dp [ $ i ] [ 1 ] = ( $ cnt0 * $ dp [ $ i - 1 ] [ 1 ] + $ cnt1 * $ dp [ $ i - 1 ] [ 0 ] ) ; } return $ dp [ $ n ] [ 0 ] ; } $ n = 2 ; $ l = 1 ; $ r = 3 ; echo countWays ( $ n , $ l , $ r ) ; ? >
Color N boxes using M colors such that K boxes have different color from the box on its left | PHP Program to Paint N boxes using M colors such that K boxes have color different from color of box on its left ; This function returns the required number of ways where idx is the current index and diff is number of boxes having different color from box on its left ; Base Case ; If already computed ; Either paint with same color as previous one ; Or paint with remaining ( M - 1 ) colors ; Driver code ; Multiply M since first box can be painted with any of the M colors and start solving from 2 nd box
< ? php $ M = 1001 ; $ MOD = 998244353 ; $ dp = array_fill ( 0 , $ M , array_fill ( 0 , $ M , -1 ) ) ; function solve ( $ idx , $ diff , $ N , $ M , $ K ) { global $ dp , $ MOD ; if ( $ idx > $ N ) { if ( $ diff == $ K ) return 1 ; return 0 ; } if ( $ dp [ $ idx ] [ $ diff ] != -1 ) return $ dp [ $ idx ] [ $ diff ] ; $ ans = solve ( $ idx + 1 , $ diff , $ N , $ M , $ K ) ; $ ans += ( $ M - 1 ) * solve ( $ idx + 1 , $ diff + 1 , $ N , $ M , $ K ) ; return $ dp [ $ idx ] [ $ diff ] = $ ans % $ MOD ; } $ N = 3 ; $ M = 3 ; $ K = 0 ; echo ( $ M * solve ( 2 , 0 , $ N , $ M , $ K ) ) ; ? >
Maximum path sum in an Inverted triangle | SET 2 | PHP program implementation of Max sum problem in a triangle ; Function for finding maximum sum ; Loop for bottom - up calculation ; For each element , check both elements just below the number and below left to the number add the maximum of them to it ; Return the maximum sum ; Driver Code
< ? php $ N = 3 ; function maxPathSum ( $ tri ) { global $ N ; $ ans = 0 ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { for ( $ j = 0 ; $ j < $ N - $ i ; $ j ++ ) { if ( $ j - 1 >= 0 ) $ tri [ $ i ] [ $ j ] += max ( $ tri [ $ i + 1 ] [ $ j ] , $ tri [ $ i + 1 ] [ $ j - 1 ] ) ; else $ tri [ $ i ] [ $ j ] += $ tri [ $ i + 1 ] [ $ j ] ; $ ans = max ( $ ans , $ tri [ $ i ] [ $ j ] ) ; } } return $ ans ; } $ tri = array ( array ( 1 , 5 , 3 ) , array ( 4 , 8 , 0 ) , array ( 1 , 0 , 0 ) ) ; echo maxPathSum ( $ tri ) ; ? >
Count no . of ordered subsets having a particular XOR value | Returns count of ordered subsets of arr [ ] with XOR value = K ; Find maximum element in arr [ ] ; Maximum possible XOR value ; The value of dp [ i ] [ j ] [ k ] is the number of subsets of length k having XOR of their elements as j from the set arr [ 0. . . i - 1 ] ; Initializing all the values of dp [ i ] [ j ] [ k ] as 0 ; The xor of empty subset is 0 ; Fill the dp table ; The answer is the number of subsets of all lengths from set arr [ 0. . n - 1 ] having XOR of elements as k ; Driver Code
< ? php function subsetXOR ( $ arr , $ n , $ K ) { $ max_ele = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ max_ele ) $ max_ele = $ arr [ $ i ] ; $ m = ( 1 << ( floor ( log ( $ max_ele , 2 ) ) + 1 ) ) - 1 ; $ dp = array ( array ( array ( ) ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ m ; $ j ++ ) for ( $ k = 0 ; $ k <= $ n ; $ k ++ ) $ dp [ $ i ] [ $ j ] [ $ k ] = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ m ; $ j ++ ) { for ( $ k = 0 ; $ k <= $ n ; $ k ++ ) { $ dp [ $ i ] [ $ j ] [ $ k ] = $ dp [ $ i - 1 ] [ $ j ] [ $ k ] ; if ( $ k != 0 ) { $ dp [ $ i ] [ $ j ] [ $ k ] += $ k * $ dp [ $ i - 1 ] [ $ j ^ $ arr [ $ i - 1 ] ] [ $ k - 1 ] ; } } } } $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ ans += $ dp [ $ n ] [ $ K ] [ $ i ] ; } return $ ans ; } $ arr = [ 1 , 2 , 3 ] ; $ k = 1 ; $ n = sizeof ( $ arr ) ; echo subsetXOR ( $ arr , $ n , $ k ) ; ? >
Possible cuts of a number such that maximum parts are divisible by 3 | PHP program to find the maximum number of numbers divisible by 3 in a large number ; This will contain the count of the splits ; This will keep sum of all successive integers , when they are indivisible by 3 ; This is the condition of finding a split ; Driver Code
< ? php function get_max_splits ( $ num_string ) { $ count = 0 ; $ running_sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ num_string ) ; $ i ++ ) { $ current_num = intval ( $ num_string [ $ i ] ) ; $ running_sum += $ current_num ; if ( $ current_num % 3 == 0 or ( $ running_sum != 0 and $ running_sum % 3 == 0 ) ) { $ count += 1 ; $ running_sum = 0 ; } } return $ count ; } print ( get_max_splits ( "12345" ) ) ; ? >
Form N | Returns the minimum cost to form a n - copy string . Here , x -> Cost to add / remove a single character ' G ' and y -> cost to append the string to itself ; Base Case : to form a 1 - copy string we need to perform an operation of type 1 ( i . e Add ) ; Case1 . Perform a Add operation on ( i - 1 ) - copy string , Case2 . Perform a type 2 operation on ( ( i + 1 ) / 2 ) - copy string ; Case1 . Perform a Add operation on ( i - 1 ) - copy string , Case2 . Perform a type 3 operation on ( i / 2 ) - copy string ; Driver Code
< ? php function findMinimumCost ( $ n , $ x , $ y ) { $ dp [ $ n + 1 ] = array ( ) ; $ dp [ 1 ] = $ x ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ i & 1 ) { $ dp [ $ i ] = min ( $ dp [ $ i - 1 ] + $ x , $ dp [ ( $ i + 1 ) / 2 ] + $ y + $ x ) ; } else { $ dp [ $ i ] = min ( $ dp [ $ i - 1 ] + $ x , $ dp [ $ i / 2 ] + $ y ) ; } } return $ dp [ $ n ] ; } $ n = 4 ; $ x = 2 ; $ y = 1 ; echo findMinimumCost ( $ n , $ x , $ y ) ; ? >