codeparrot/xlcost-text-to-code huggingface dataset:
Assembly Line Scheduling | DP | A PHP program to find minimum possible time by the car chassis to complete ; Utility function to find minimum of two numbers ; time taken to leave first station in line 1 ; time taken to leave first station in line 2 ; Fill tables T1 [ ] and T2 [ ] using the above given recursive relations ; Consider exit times and return minimum ; Driver Code
< ? php $ NUM_LINE = 2 ; $ NUM_STATION = 4 ; function carAssembly ( $ a , $ t , $ e , $ x ) { global $ NUM_LINE , $ NUM_STATION ; $ T1 = array ( ) ; $ T2 = array ( ) ; $ i ; $ T1 [ 0 ] = $ e [ 0 ] + $ a [ 0 ] [ 0 ] ; $ T2 [ 0 ] = $ e [ 1 ] + $ a [ 1 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ NUM_STATION ; ++ $ i ) { $ T1 [ $ i ] = min ( $ T1 [ $ i - 1 ] + $ a [ 0 ] [ $ i ] , $ T2 [ $ i - 1 ] + $ t [ 1 ] [ $ i ] + $ a [ 0 ] [ $ i ] ) ; $ T2 [ $ i ] = min ( $ T2 [ $ i - 1 ] + $ a [ 1 ] [ $ i ] , $ T1 [ $ i - 1 ] + $ t [ 0 ] [ $ i ] + $ a [ 1 ] [ $ i ] ) ; } return min ( $ T1 [ $ NUM_STATION - 1 ] + $ x [ 0 ] , $ T2 [ $ NUM_STATION - 1 ] + $ x [ 1 ] ) ; } $ a = array ( array ( 4 , 5 , 3 , 2 ) , array ( 2 , 10 , 1 , 4 ) ) ; $ t = array ( array ( 0 , 7 , 4 , 5 ) , array ( 0 , 9 , 2 , 8 ) ) ; $ e = array ( 10 , 12 ) ; $ x = array ( 18 , 7 ) ; echo carAssembly ( $ a , $ t , $ e , $ x ) ; ? >
Longest Common Substring | DP | Returns length of longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Notethat LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; Driver Code
< ? php function LCSubStr ( $ X , $ Y , $ m , $ n ) { $ LCSuff = array_fill ( 0 , $ m + 1 , array_fill ( 0 , $ n + 1 , NULL ) ) ; $ result = 0 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( $ i == 0 $ j == 0 ) $ LCSuff [ $ i ] [ $ j ] = 0 ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) { $ LCSuff [ $ i ] [ $ j ] = $ LCSuff [ $ i - 1 ] [ $ j - 1 ] + 1 ; $ result = max ( $ result , $ LCSuff [ $ i ] [ $ j ] ) ; } else $ LCSuff [ $ i ] [ $ j ] = 0 ; } } return $ result ; } $ X = " OldSite : GeeksforGeeks . org " ; $ Y = " NewSite : GeeksQuiz . com " ; $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; echo " Length ▁ of ▁ Longest ▁ Common ▁ Substring ▁ is ▁ " . LCSubStr ( $ X , $ Y , $ m , $ n ) ; ? >
Maximum Subarray Sum using Divide and Conquer algorithm | Find the maximum possible sum in arr [ ] such that arr [ m ] is part of it ; Include elements on left of mid . ; Include elements on right of mid ; Return sum of elements on left and right of mid returning only left_sum + right_sum will fail for [ - 2 , 1 ] ; Returns sum of maximum sum subarray in aa [ l . . h ] ; Base Case : Only one element ; Find middle point ; Return maximum of following three possible cases a ) Maximum subarray sum in left half b ) Maximum subarray sum in right half c ) Maximum subarray sum such that the subarray crosses the midpoint ; Driver Code
< ? php function maxCrossingSum ( & $ arr , $ l , $ m , $ h ) { $ sum = 0 ; $ left_sum = PHP_INT_MIN ; for ( $ i = $ m ; $ i >= $ l ; $ i -- ) { $ sum = $ sum + $ arr [ $ i ] ; if ( $ sum > $ left_sum ) $ left_sum = $ sum ; } $ sum = 0 ; $ right_sum = PHP_INT_MIN ; for ( $ i = $ m + 1 ; $ i <= $ h ; $ i ++ ) { $ sum = $ sum + $ arr [ $ i ] ; if ( $ sum > $ right_sum ) $ right_sum = $ sum ; } return max ( $ left_sum + $ right_sum , $ left_sum , $ right_sum ) ; } function maxSubArraySum ( & $ arr , $ l , $ h ) { if ( $ l == $ h ) return $ arr [ $ l ] ; $ m = intval ( ( $ l + $ h ) / 2 ) ; return max ( maxSubArraySum ( $ arr , $ l , $ m ) , maxSubArraySum ( $ arr , $ m + 1 , $ h ) , maxCrossingSum ( $ arr , $ l , $ m , $ h ) ) ; } $ arr = array ( 2 , 3 , 4 , 5 , 7 ) ; $ n = count ( $ arr ) ; $ max_sum = maxSubArraySum ( $ arr , 0 , $ n - 1 ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_sum ; ? >
Program to find amount of water in a given glass | Returns the amount of water in jth glass of ith row ; A row number i has maximum i columns . So input column number must be less than i ; There will be i * ( i + 1 ) / 2 glasses till ith row ( including ith row ) and Initialize all glasses as empty ; Put all water in first glass ; Now let the water flow to the downward glasses till the row number is less than or / equal to i ( given row ) correction : X can be zero for side glasses as they have lower rate to fill ; Fill glasses in a given row . Number of columns in a row is equal to row number ; Get the water from current glass ; Keep the amount less than or equal to capacity in current glass ; Get the remaining amount ; Distribute the remaining amount to the down two glasses ; The index of jth glass in ith row will be i * ( i - 1 ) / 2 + j - 1 ; Driver Code ; Total amount of water
< ? php function findWater ( $ i , $ j , $ X ) { if ( $ j > $ i ) { echo " Incorrect ▁ Input STRNEWLINE " ; return ; } $ glass = array_fill ( 0 , ( int ) ( $ i * ( $ i + 1 ) / 2 ) , 0 ) ; $ index = 0 ; $ glass [ $ index ] = $ X ; for ( $ row = 1 ; $ row < $ i ; ++ $ row ) { for ( $ col = 1 ; $ col <= $ row ; ++ $ col , ++ $ index ) { $ X = $ glass [ $ index ] ; $ glass [ $ index ] = ( $ X >= 1.0 ) ? 1.0 : $ X ; $ X = ( $ X >= 1.0 ) ? ( $ X - 1 ) : 0.0 ; $ glass [ $ index + $ row ] += ( double ) ( $ X / 2 ) ; $ glass [ $ index + $ row + 1 ] += ( double ) ( $ X / 2 ) ; } } return $ glass [ ( int ) ( $ i * ( $ i - 1 ) / 2 + $ j - 1 ) ] ; } $ i = 2 ; $ j = 2 ; $ X = 2.0 ; echo " Amount ▁ of ▁ water ▁ in ▁ jth ▁ " , " glass ▁ of ▁ ith ▁ row ▁ is : ▁ " . str_pad ( findWater ( $ i , $ j , $ X ) , 8 , '0' ) ; ? >
Palindrome Partitioning | DP | Returns the minimum number of cuts needed to partition a string such that every part is a palindrome ; Get the length of the string ; Create two arrays to build the solution in bottom up manner C [ i ] [ j ] = Minimum number of cuts needed for palindrome partitioning of substring str [ i . . j ] P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false Note that C [ i ] [ j ] is 0 if P [ i ] [ j ] is true ; Every substring of length 1 is a palindrome ; L is substring length . Build the solution in a bottom - up manner by considering all substrings of length starting from 2 to n . The loop structure is same as Matrix Chain Multiplication problem ( See https : www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) ; For substring of length L , set different possible starting indexes ; Set ending index ; If L is 2 , then we just need to compare two characters . Else need to check two corner characters and value of P [ i + 1 ] [ j - 1 ] ; IF str [ i . . j ] is palindrome , then C [ i ] [ j ] is 0 ; Make a cut at every possible location starting from i to j , and get the minimum cost cut . ; Return the min cut value for complete string . i . e . , str [ 0. . n - 1 ] ; Driver program to test the above function
< ? php function minPalPartion ( $ str ) { $ n = strlen ( $ str ) ; $ C = array_fill ( 0 , $ n , array_fill ( 0 , $ n , NULL ) ) ; $ P = array_fill ( false , $ n , array_fill ( false , $ n , NULL ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ P [ $ i ] [ $ i ] = true ; $ C [ $ i ] [ $ i ] = 0 ; } for ( $ L = 2 ; $ L <= $ n ; $ L ++ ) { for ( $ i = 0 ; $ i < $ n - $ L + 1 ; $ i ++ ) { $ j = $ i + $ L - 1 ; if ( $ L == 2 ) $ P [ $ i ] [ $ j ] = ( $ str [ $ i ] == $ str [ $ j ] ) ; else $ P [ $ i ] [ $ j ] = ( $ str [ $ i ] == $ str [ $ j ] ) && $ P [ $ i + 1 ] [ $ j - 1 ] ; if ( $ P [ $ i ] [ $ j ] == true ) $ C [ $ i ] [ $ j ] = 0 ; else { $ C [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = $ i ; $ k <= $ j - 1 ; $ k ++ ) $ C [ $ i ] [ $ j ] = min ( $ C [ $ i ] [ $ j ] , $ C [ $ i ] [ $ k ] + $ C [ $ k + 1 ] [ $ j ] + 1 ) ; } } } return $ C [ 0 ] [ $ n - 1 ] ; } $ str = " ababbbabbababa " ; echo " Min ▁ cuts ▁ needed ▁ for ▁ Palindrome ▁ Partitioning ▁ is ▁ " . minPalPartion ( $ str ) ; return 0 ; ? >
String matching with * ( that matches with any ) in any of the two strings | Function to check if the two strings can be matched or not ; if the string don 't have *  then character at that position  must be same. ; Driver Code
< ? php function doMatch ( $ A , $ B ) { for ( $ i = 0 ; $ i < strlen ( $ A ) ; $ i ++ ) if ( $ A [ $ i ] != ' * ' && $ B [ $ i ] != ' * ' ) if ( $ A [ $ i ] != $ B [ $ i ] ) return false ; return true ; } $ A = " gee * sforgeeks " ; $ B = " geeksforgeeks " ; echo doMatch ( $ A , $ B ) ; ? >
Find Nth term of the series 0 , 2 , 4 , 8 , 12 , 18. . . | Calculate Nth term of series ; Driver Code
< ? php function nthTerm ( $ N ) { return ( int ) ( ( $ N + $ N * ( $ N - 1 ) ) / 2 ) ; } $ N = 5 ; echo nthTerm ( $ N ) ; ? >
Number of substrings of one string present in other | PHP program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code
< ? php function countSubstrs ( $ s1 , $ s2 ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) { $ s3 = " " ; for ( $ j = $ i ; $ j < strlen ( $ s1 ) ; $ j ++ ) { $ s3 += $ s1 [ $ j ] ; if ( stripos ( $ s2 , $ s3 , 0 ) != -1 ) $ ans ++ ; } } return $ ans ; } $ s1 = " aab " ; $ s2 = " aaaab " ; echo countSubstrs ( $ s1 , $ s2 ) ; ? >
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function for finding factorial of N ; return factorial of N ; Function for calculating Nth term of series ; Driver code
< ? php function factorial ( $ N ) { $ fact = 1 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function nthTerm ( $ N ) { return ( factorial ( $ N ) * ( $ N + 2 ) / 2 ) ; } $ N = 6 ; echo nthTerm ( $ N ) ; ? >
Match Expression where a single special character in pattern can match one or more characters | Returns true if pat matches with text ; i is used as an index in pattern and j as an index in text ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code
< ? php function regexMatch ( $ text , $ pat ) { $ lenText = strlen ( $ text ) ; $ letPat = strlen ( $ pat ) ; $ i = 0 ; $ j = 0 ; while ( $ i < $ letPat ) { if ( $ pat [ $ i ] != ' # ' ) { if ( $ pat [ $ i ] != $ text [ $ j ] ) return false ; $ i ++ ; $ j ++ ; } else { $ j ++ ; while ( $ text [ $ j ] != $ pat [ $ i + 1 ] ) $ j ++ ; $ i ++ ; } } return ( $ j == $ lenText ) ; } $ str = " ABABABA " ; $ pat = " A # B # A " ; if ( regexMatch ( $ str , $ pat ) ) echo " yes " ; else echo " no " ; ? >
Binary Indexed Tree : Range Updates and Point Queries | Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver Code ; Find the element at Index 4 ; Find the element at Index 3
< ? php function update ( & $ arr , $ l , $ r , $ val ) { $ arr [ $ l ] += $ val ; if ( $ r + 1 < sizeof ( $ arr ) ) $ arr [ $ r + 1 ] -= $ val ; } function getElement ( & $ arr , $ i ) { $ res = 0 ; for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) $ res += $ arr [ $ j ] ; return $ res ; } $ arr = array ( 0 , 0 , 0 , 0 , 0 ) ; $ n = sizeof ( $ arr ) ; $ l = 2 ; $ r = 4 ; $ val = 2 ; update ( $ arr , $ l , $ r , $ val ) ; $ index = 4 ; echo ( " Element ▁ at ▁ index ▁ " . $ index . " ▁ is ▁ " . getElement ( $ arr , $ index ) . " STRNEWLINE " ) ; $ l = 0 ; $ r = 3 ; $ val = 4 ; update ( $ arr , $ l , $ r , $ val ) ; $ index = 3 ; echo ( " Element ▁ at ▁ index ▁ " . $ index . " ▁ is ▁ " . getElement ( $ arr , $ index ) ) ; ? >
Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | Function to find factorial of a number ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call
< ? php function factorial ( $ n ) { if ( $ n <= 1 ) return 1 ; return $ n * factorial ( $ n - 1 ) ; } function nCr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ n - $ r ) * factorial ( $ r ) ) ; } function NumberOfWays ( $ n , $ x , $ y ) { return nCr ( 2 * $ n - $ x - $ y , $ n - $ x ) * factorial ( $ n ) * factorial ( $ n ) ; } $ n = 5 ; $ x = 4 ; $ y = 2 ; echo ( NumberOfWays ( $ n , $ x , $ y ) ) ; ? >
Count Pairs from two arrays with even sum | Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code ; This code is contributes by AnkitRai01
< ? php function count_pairs ( $ a , $ b , $ n , $ m ) { $ odd1 = 0 ; $ even1 = 0 ; $ odd2 = 0 ; $ even2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 == 1 ) $ odd1 ++ ; else $ even1 ++ ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ b [ $ i ] % 2 == 1 ) $ odd2 ++ ; else $ even2 ++ ; } $ pairs = min ( $ odd1 , $ odd2 ) + min ( $ even1 , $ even2 ) ; return $ pairs ; } $ a = array ( 9 , 14 , 6 , 2 , 11 ) ; $ b = array ( 8 , 4 , 7 , 20 ) ; $ n = count ( $ a ) ; $ m = count ( $ b ) ; echo count_pairs ( $ a , $ b , $ n , $ m ) ; ? >
Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code
< ? php function getValueOfF ( $ n ) { if ( $ n == 1 ) return 0 ; if ( $ n == 2 ) return 1 ; $ cnt = 0 ; while ( $ n % 2 == 0 ) { $ cnt += 1 ; $ n /= 2 ; } return 2 * $ cnt ; } $ n = 20 ; echo getValueOfF ( $ n ) ; ? >
Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code
< ? php function A_mod_B ( $ N , $ a ) { $ len = strlen ( $ N ) ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ ans = ( $ ans * 10 + ( int ) $ N [ $ i ] - '0' ) % $ a ; return $ ans % $ a ; } function findMod ( $ N ) { $ mod = A_mod_B ( $ N , 4 ) ; $ ans = ( 1 + pow ( 2 , $ mod ) + pow ( 3 , $ mod ) + pow ( 4 , $ mod ) ) ; return ( $ ans % 5 ) ; } $ N = "4" ; echo findMod ( $ N ) ; ? >
Elements greater than the previous and next element in an Array | Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code
< ? php function printElements ( $ arr , $ n ) { for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] and $ arr [ $ i ] > $ arr [ $ i + 1 ] ) echo $ arr [ $ i ] . " ▁ " ; } } $ arr = array ( 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 ) ; $ n = sizeof ( $ arr ) ; printElements ( $ arr , $ n ) ;
Sum of the series Kn + ( K ( n | Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
< ? php function exponent ( $ A , $ B ) { if ( $ A == 0 ) return 0 ; if ( $ B == 0 ) return 1 ; if ( $ B % 2 == 0 ) { $ y = exponent ( $ A , $ B / 2 ) ; $ y = ( $ y * $ y ) ; } else { $ y = $ A ; $ y = ( $ y * exponent ( $ A , $ B - 1 ) ) ; } return $ y ; } function sum ( $ k , $ n ) { $ sum = exponent ( $ k , $ n + 1 ) - exponent ( $ k - 1 , $ n + 1 ) ; return $ sum ; } $ n = 3 ; $ K = 3 ; echo sum ( $ K , $ n ) ; ? >
Minimize the cost to split a number | check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code
< ? php function isPrime ( $ x ) { for ( $ i = 2 ; $ i * $ i <= $ x ; $ i ++ ) { if ( $ x % $ i == 0 ) return 0 ; } return 1 ; } function minimumCost ( $ n ) { if ( isPrime ( $ n ) ) return 1 ; if ( $ n % 2 == 1 && isPrime ( $ n - 2 ) ) return 2 ; if ( $ n % 2 == 0 ) return 2 ; return 3 ; } $ n = 6 ; echo ( minimumCost ( $ n ) ) ;
Find amount of water wasted after filling the tank | Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code
< ? php function wastedWater ( $ V , $ M , $ N ) { $ amt_per_min = $ M - $ N ; $ time_to_fill = $ V / $ amt_per_min ; $ wasted_amt = $ N * $ time_to_fill ; return $ wasted_amt ; } $ V = 700 ; $ M = 10 ; $ N = 3 ; echo wastedWater ( $ V , $ M , $ N ) , " STRNEWLINE " ; $ V = 1000 ; $ M = 100 ; $ N = 50 ; echo wastedWater ( $ V , $ M , $ N ) ;
Smallest and Largest N | Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code
< ? php function nDigitPerfectCubes ( $ n ) { print ( pow ( ceil ( pow ( pow ( 10 , ( $ n - 1 ) ) , 1 / 3 ) ) , 3 ) . " ▁ " ) ; print ( ( int ) pow ( ceil ( pow ( pow ( 10 , ( $ n ) ) , 1 / 3 ) ) - 1 , 3 ) ) ; } $ n = 3 ; nDigitPerfectCubes ( $ n ) ; ? >
Count numbers which are divisible by all the numbers from 2 to 10 | Function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; Driver code
< ? php function countNumbers ( $ n ) { return ( int ) ( $ n / 2520 ) ; } $ n = 3000 ; echo ( countNumbers ( $ n ) ) ; ? >
Sum of all odd factors of numbers in the range [ l , r ] | PHP implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
< ? php $ MAX = 10001 ; $ prefix = array_fill ( 0 , $ MAX , 0 ) ; function sieve_modified ( ) { global $ prefix , $ MAX ; for ( $ i = 1 ; $ i < $ MAX ; $ i += 2 ) { for ( $ j = $ i ; $ j < $ MAX ; $ j += $ i ) $ prefix [ $ j ] += $ i ; } for ( $ i = 1 ; $ i < $ MAX ; $ i ++ ) $ prefix [ $ i ] += $ prefix [ $ i - 1 ] ; } function sumOddFactors ( $ L , $ R ) { global $ prefix ; return ( $ prefix [ $ R ] - $ prefix [ $ L - 1 ] ) ; } sieve_modified ( ) ; $ l = 6 ; $ r = 10 ; echo sumOddFactors ( $ l , $ r ) ;
XOR of a submatrix queries | PHP implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries
< ? php $ n = 3 ; function preComputeXor ( $ arr , & $ prefix_xor ) { global $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ j == 0 ) $ prefix_xor [ $ i ] [ $ j ] = $ arr [ $ i ] [ $ j ] ; else $ prefix_xor [ $ i ] [ $ j ] = ( $ prefix_xor [ $ i ] [ $ j - 1 ] ^ $ arr [ $ i ] [ $ j ] ) ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 1 ; $ j < $ n ; $ j ++ ) $ prefix_xor [ $ j ] [ $ i ] = ( $ prefix_xor [ $ j - 1 ] [ $ i ] ^ $ prefix_xor [ $ j ] [ $ i ] ) ; } function ansQuerie ( $ prefix_xor , $ x1 , $ y1 , $ x2 , $ y2 ) { $ xor_1 = $ xor_2 = $ xor_3 = 0 ; if ( $ x1 != 0 ) $ xor_1 = $ prefix_xor [ $ x1 - 1 ] [ $ y2 ] ; if ( $ y1 != 0 ) $ xor_2 = $ prefix_xor [ $ x2 ] [ $ y1 - 1 ] ; if ( $ x1 != 0 and $ y1 != 0 ) $ xor_3 = $ prefix_xor [ $ x1 - 1 ] [ $ y1 - 1 ] ; return ( ( $ prefix_xor [ $ x2 ] [ $ y2 ] ^ $ xor_1 ) ^ ( $ xor_2 ^ $ xor_3 ) ) ; } $ arr = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; $ prefix_xor = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; preComputeXor ( $ arr , $ prefix_xor ) ; echo ansQuerie ( $ prefix_xor , 1 , 1 , 2 , 2 ) . " " ; echo ansQuerie ( $ prefix_xor , 1 , 2 , 2 , 2 ) ; ? >
Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code
< ? php function isDigitSumOne ( $ nm ) { if ( $ nm % 9 == 1 ) return true ; else return false ; } function printValidNums ( $ n ) { $ cbrt_n = ceil ( pow ( $ n , 1 / 3 ) ) ; for ( $ i = 1 ; $ i <= $ cbrt_n ; $ i ++ ) { $ cube = pow ( $ i , 3 ) ; if ( $ cube >= 1 && $ cube <= $ n && isDigitSumOne ( $ cube ) ) echo $ cube , " ▁ " ; } } $ n = 1000 ; printValidNums ( $ n ) ; ? >
Count the number of rhombi possible inside a rectangle of given size | Function to return the count of rhombi possible ; All possible diagonal lengths ; Update rhombi possible with the current diagonal lengths ; Return the total count of rhombi possible ; Driver code
< ? php function countRhombi ( $ h , $ w ) { $ ct = 0 ; for ( $ i = 2 ; $ i <= $ h ; $ i += 2 ) for ( $ j = 2 ; $ j <= $ w ; $ j += 2 ) $ ct += ( $ h - $ i + 1 ) * ( $ w - $ j + 1 ) ; return $ ct ; } $ h = 2 ; $ w = 2 ; echo ( countRhombi ( $ h , $ w ) ) ; ? >
Program to calculate the area between two Concentric Circles | Function to find area between the two given concentric circles ; Declare value of pi ; Calculate area of outer circle ; Calculate area of inner circle ; Difference in areas ; Driver Code
< ? php function calculateArea ( $ x , $ y ) { $ pi = 3.1415926536 ; $ arx = $ pi * $ x * $ x ; $ ary = $ pi * $ y * $ y ; return ( $ arx - $ ary ) ; } $ x = 2 ; $ y = 1 ; echo calculateArea ( $ x , $ y ) ; ? >
Find the winner by adding Pairwise difference of elements in the array until Possible | Function to calculate gcd ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver Code
< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function getWinner ( $ arr , $ n ) { $ gcd = $ arr [ 0 ] ; $ maxEle = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ gcd = __gcd ( $ gcd , $ arr [ $ i ] ) ; $ maxEle = max ( $ maxEle , $ arr [ $ i ] ) ; } $ totalMoves = ( $ maxEle / $ gcd ) - $ n ; if ( $ totalMoves % 2 == 1 ) return ' A ' ; return ' B ' ; } $ arr = array ( 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) ; echo getWinner ( $ arr , $ n ) ; ? >
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | PHP implementation of the approach ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; To store the count of possible pairs ; Check all possible pairs ; Calculating the value of ( ( n % i ) % j ) % n ; If value is equal to maximum ; Return the number of possible pairs ; Driver code
< ? php function countPairs ( $ n ) { $ num = ( ( $ n / 2 ) + 1 ) ; $ max = $ n % $ num ; $ count = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { $ val = ( ( $ n % $ i ) % $ j ) % $ n ; if ( $ val == $ max ) $ count ++ ; } } return $ count ; } $ n = 5 ; echo ( countPairs ( $ n ) ) ; ? >
Program to check if a number is divisible by any of its digits | Function to check if given number is divisible by any of its digits ; check if any of digit divides n ; check if K divides N ; Driver Code
< ? php function isDivisible ( $ n ) { $ temp = $ n ; while ( $ n ) { $ k = $ n % 10 ; if ( $ temp % $ k == 0 ) return " YES " ; $ n = floor ( $ n / 10 ) ; } return " NO " ; } $ n = 9876543 ; echo isDivisible ( $ n ) ; ? >
Program to find sum of harmonic series | Function to return sum of harmonic series ; Driver Code
< ? php function sum ( $ n ) { $ i ; $ s = 0.0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ s = $ s + 1 / $ i ; return $ s ; } $ n = 5 ; echo ( " Sum ▁ is ▁ " ) ; echo ( sum ( $ n ) ) ; ? >