codeparrot/xlcost-text-to-code huggingface dataset:
Sum of series with alternate signed squares of AP | function to calculate series sum ; Driver code
< ? php function seiresSum ( $ n , $ a ) { return $ n * ( $ a [ 0 ] * $ a [ 0 ] - $ a [ 2 * $ n - 1 ] * $ a [ 2 * $ n - 1 ] ) / ( 2 * $ n - 1 ) ; } $ n = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; echo seiresSum ( $ n , $ a ) ; ? >
Find nth number that contains the digit k or divisible by k . | Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy the criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver code
< ? php function checkdigit ( $ n , $ k ) { while ( $ n ) { $ rem = $ n % 10 ; if ( $ rem == $ k ) return 1 ; $ n = $ n / 10 ; } return 0 ; } function findNthNumber ( $ n , $ k ) { for ( $ i = $ k + 1 , $ count = 1 ; $ count < $ n ; $ i ++ ) { if ( checkdigit ( $ i , $ k ) || ( $ i % $ k == 0 ) ) $ count ++ ; if ( $ count == $ n ) return $ i ; } return -1 ; } $ n = 10 ; $ k = 2 ; echo findNthNumber ( $ n , $ k ) ; ? >
Middle of three using minimum comparisons | PHP program to find middle of three distinct numbers ; Function to find the middle of three number ; Checking for b ; Checking for a ; Driver Code
< ? php function middleOfThree ( $ a , $ b , $ c ) { function middleOfThree ( $ a , $ b , $ c ) { if ( ( $ a < $ b && $ b < $ c ) or ( $ c < $ b && $ b < $ a ) ) return $ b ; else if ( ( $ b < $ a and $ a < $ c ) or ( $ c < $ a and $ a < $ b ) ) return $ a ; else return $ c ; } $ a = 20 ; $ b = 30 ; $ c = 40 ; echo middleOfThree ( $ a , $ b , $ c ) ; ? >
Find the minimum cost to reach destination using a train | A Dynamic Programming based solution to find min cost to reach station N - 1 from station 0. ; This function returns the smallest possible cost to reach station N - 1 from station 0. ; dist [ i ] stores minimum cost to reach station i from station 0. ; Go through every station and check if using it as an intermediate station gives better path ; Driver program to test above function
< ? php $ INF = PHP_INT_MAX ; $ N = 4 ; function minCost ( $ cost ) { global $ INF ; global $ N ; $ dist [ $ N ] = array ( ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ dist [ $ i ] = $ INF ; $ dist [ 0 ] = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ dist [ $ j ] > $ dist [ $ i ] + $ cost [ $ i ] [ $ j ] ) $ dist [ $ j ] = $ dist [ $ i ] + $ cost [ $ i ] [ $ j ] ; return $ dist [ $ N - 1 ] ; } $ cost = array ( array ( 0 , 15 , 80 , 90 ) , array ( INF , 0 , 40 , 50 ) , array ( INF , INF , 0 , 70 ) , array ( INF , INF , INF , 0 ) ) ; echo " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " , $ N , " ▁ is ▁ " , minCost ( $ cost ) ; ? >
Number of loops of size k starting from a specific node | Return the Number of ways from a node to make a loop of size K in undirected complete connected graph of N nodes ; Driver Code
< ? php function numOfways ( $ n , $ k ) { $ p = 1 ; if ( $ k % 2 ) $ p = -1 ; return ( pow ( $ n - 1 , $ k ) + $ p * ( $ n - 1 ) ) / $ n ; } $ n = 4 ; $ k = 2 ; echo numOfways ( $ n , $ k ) ; ? >
Length of the chord of the circle whose radius and the angle subtended at the center by the chord is given | Function to find the length of the chord ; Driver code
< ? php function length_of_chord ( $ r , $ x ) { echo " The ▁ length ▁ of ▁ the ▁ chord " , " ▁ of ▁ the ▁ circle ▁ is ▁ " , 2 * $ r * sin ( $ x * ( 3.14 / 180 ) ) ; } $ r = 4 ; $ x = 63 ; length_of_chord ( $ r , $ x ) ; ? >
Area of a square inscribed in a circle which is inscribed in an equilateral triangle | Function to find the area of the square ; a cannot be negative ; area of the square ; Driver code
< ? php function area ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = sqrt ( $ a ) / 6 ; return $ area ; } $ a = 10 ; echo area ( $ a ) ; ? >
Length of longest rod that can fit into a cuboid | Function to find the length ; temporary variable to hold the intermediate result ; length of longest rod is calculated using square root function ; Driver code ; calling longestRodInCuboid ( ) function to get the length of longest rod
< ? php function longestRodInCuboid ( $ length , $ breadth , $ height ) { $ result ; $ temp ; $ temp = $ length * $ length + $ breadth * $ breadth + $ height * $ height ; $ result = sqrt ( $ temp ) ; return $ result ; } $ length = 12 ; $ breadth = 9 ; $ height = 8 ; echo longestRodInCuboid ( $ length , $ breadth , $ height ) ; ? >
Check whether a given point lies on or inside the rectangle | Set 3 | function to Check whether a given point lies inside or on the rectangle or not ; Driver code
< ? php function LiesInsieRectangle ( $ a , $ b , $ x , $ y ) { if ( $ x - $ y - $ b <= 0 && $ x - $ y + $ b >= 0 && $ x + $ y - 2 * $ a + $ b <= 0 && $ x + $ y - $ b >= 0 ) return true ; return false ; } $ a = 7 ; $ b = 2 ; $ x = 4 ; $ y = 5 ; if ( LiesInsieRectangle ( $ a , $ b , $ x , $ y ) ) echo " Given ▁ point ▁ lies ▁ " . " inside ▁ the ▁ rectangle " ; else echo " Given ▁ point ▁ does ▁ not " . " ▁ lie ▁ on ▁ the ▁ rectangle " ; ? >
Maximize volume of cuboid with given sum of sides | Return the maximum volume . ; for length ; for breadth ; for height ; calculating maximum volume . ; Driver Code
< ? php function maxvolume ( $ s ) { $ maxvalue = 0 ; for ( $ i = 1 ; $ i <= $ s - 2 ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ s - 1 ; $ j ++ ) { $ k = $ s - $ i - $ j ; $ maxvalue = max ( $ maxvalue , $ i * $ j * $ k ) ; } } return $ maxvalue ; } $ s = 8 ; echo ( maxvolume ( $ s ) ) ; ? >
Maximize volume of cuboid with given sum of sides | Return the maximum volume . ; finding length ; finding breadth ; finding height ; Driven Code
< ? php function maxvolume ( $ s ) { $ length = ( int ) ( $ s / 3 ) ; $ s -= $ length ; $ breadth = ( int ) ( $ s / 2 ) ; $ height = $ s - $ breadth ; return $ length * $ breadth * $ height ; } $ s = 8 ; echo ( maxvolume ( $ s ) ) ; ? >
Area of a Hexagon | function for calculating area of the hexagon . ; Length of a side
< ? php function hexagonArea ( $ s ) { return ( ( 3 * sqrt ( 3 ) * ( $ s * $ s ) ) / 2 ) ; } $ s = 4 ; echo ( " Area ▁ : ▁ " ) ; echo ( hexagonArea ( $ s ) ) ; ? >
Maximum number of squares that can fit in a right angle isosceles triangle | function for finding max squares ; return in O ( 1 ) with derived formula ; Driver Code
< ? php function maxSquare ( $ b , $ m ) { return ( $ b / $ m - 1 ) * ( $ b / $ m ) / 2 ; } $ b = 10 ; $ m = 2 ; echo maxSquare ( $ b , $ m ) ;
Check if right triangle possible from given area and hypotenuse | Prints three sides of a right triangle from given area and hypotenuse if triangle is possible , else prints - 1. ; Descriminant of the equation ; applying the linear equation formula to find both the roots ; Driver code
< ? php function findRightAngle ( $ A , $ H ) { $ D = pow ( $ H , 4 ) - 16 * $ A * $ A ; if ( $ D >= 0 ) { $ root1 = ( $ H * $ H + sqrt ( $ D ) ) / 2 ; $ root2 = ( $ H * $ H - sqrt ( $ D ) ) / 2 ; $ a = sqrt ( $ root1 ) ; $ b = sqrt ( $ root2 ) ; if ( $ b >= $ a ) echo $ a , " ▁ " , $ b , " ▁ " , $ H ; else echo $ b , " ▁ " , $ a , " ▁ " , $ H ; } else echo " - 1" ; } findRightAngle ( 6 , 5 ) ;
Maximum number of 2 x2 squares that can be fit inside a right isosceles triangle | PHP program to count number of 2 x 2 squares in a right isosceles triangle ; removing the extra part we would always need ; Since each square has base of length of 2 ; Driver code
< ? php function numberOfSquares ( $ base ) { $ base = ( $ base - 2 ) ; $ base = intdiv ( $ base , 2 ) ; return $ base * ( $ base + 1 ) / 2 ; } $ base = 8 ; echo numberOfSquares ( $ base ) ; ? >
Fibonacci Cube Graph | function to find fibonacci number ; function for finding number of vertices in fibonacci cube graph ; return fibonacci number for f ( n + 2 ) ; Driver Code
< ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; } function findVertices ( $ n ) { return fib ( $ n + 2 ) ; } $ n = 3 ; echo findVertices ( $ n ) ; ? >
Sorting rows of matrix in ascending order followed by columns in descending order | PHP implementation to sort the rows of matrix in ascending order followed by sorting the columns in descending order ; function to sort each row of the matrix according to the order specified by ascending . ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of mat [ ] [ ] ; get transpose of mat [ ] [ ] ; again sort rows of mat [ ] [ ] in descending order . ; again get transpose of mat [ ] [ ] ; function to print the matrix ; Driver Code
< ? php $ MAX_SIZE = 10 ; function sortByRow ( & $ mat , $ n , $ ascending ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ ascending ) sort ( $ mat [ $ i ] ) ; else rsort ( $ mat [ $ i ] ) ; } } function transpose ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ temp = $ mat [ $ i ] [ $ j ] ; $ mat [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ mat [ $ j ] [ $ i ] = $ temp ; } } } function sortMatRowAndColWise ( & $ mat , $ n ) { sortByRow ( $ mat , $ n , true ) ; transpose ( $ mat , $ n ) ; sortByRow ( $ mat , $ n , false ) ; transpose ( $ mat , $ n ) ; } function printMat ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ mat [ $ i ] [ $ j ] . " ▁ " ; echo " STRNEWLINE " ; } } $ n = 3 ; $ mat = array ( array ( 3 , 2 , 1 ) , array ( 9 , 8 , 7 ) , array ( 6 , 5 , 4 ) ) ; echo " Original ▁ Matrix : STRNEWLINE " ; printMat ( $ mat , $ n ) ; sortMatRowAndColWise ( $ mat , $ n ) ; echo " Matrix After Sorting : " ; printMat ( $ mat , $ n ) ; ? >
Sort the matrix row | PHP implementation to sort the matrix row - wise and column - wise ; function to sort each row of the matrix ; sorting row number ' i ' ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of mat [ ] [ ] ; get transpose of mat [ ] [ ] ; again sort rows of mat [ ] [ ] ; again get transpose of mat [ ] [ ] ; function to print the matrix ; Driver Code
< ? php $ MAX_SIZE = 10 ; function sortByRow ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) sort ( $ mat [ $ i ] ) ; } function transpose ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ t = $ mat [ $ i ] [ $ j ] ; $ mat [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ mat [ $ j ] [ $ i ] = $ t ; } } } function sortMatRowAndColWise ( & $ mat , $ n ) { sortByRow ( $ mat , $ n ) ; transpose ( $ mat , $ n ) ; sortByRow ( $ mat , $ n ) ; transpose ( $ mat , $ n ) ; } function printMat ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ mat [ $ i ] [ $ j ] . " ▁ " ; echo " STRNEWLINE " ; } } $ mat = array ( array ( 4 , 1 , 3 ) , array ( 9 , 6 , 8 ) , array ( 5 , 2 , 7 ) ) ; $ n = 3 ; echo " Original ▁ Matrix : STRNEWLINE " ; printMat ( $ mat , $ n ) ; sortMatRowAndColWise ( $ mat , $ n ) ; echo " Matrix After Sorting : " ; printMat ( $ mat , $ n ) ; ? >
Magic Square | Even Order | Function for calculating Magic square ; filling matrix with its count value starting from 1 ; ; change value of Array elements at fix location as per rule ( n * n + 1 ) - arr [ i ] [ j ] Top Left corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Top Right corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Bottom Left corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Bottom Right corner of Matrix ( order ( n / 4 ) * ( n / 4 ) ) ; Centre of Matrix ( order ( n / 2 ) * ( n / 2 ) ) ; Printing the magic - square ; Driver Code ; Function call
< ? php function doublyEven ( $ n ) { $ arr = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ i ) + $ j + 1 ; for ( $ i = 0 ; $ i < $ n / 4 ; $ i ++ ) for ( $ j = 0 ; $ j < $ n / 4 ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ; for ( $ i = 0 ; $ i < $ n / 4 ; $ i ++ ) for ( $ j = 3 * ( $ n / 4 ) ; $ j < $ n ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ; for ( $ i = 3 * $ n / 4 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n / 4 ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ; for ( $ i = 3 * $ n / 4 ; $ i < $ n ; $ i ++ ) for ( $ j = 3 * $ n / 4 ; $ j < $ n ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ; for ( $ i = $ n / 4 ; $ i < 3 * $ n / 4 ; $ i ++ ) for ( $ j = $ n / 4 ; $ j < 3 * $ n / 4 ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ arr [ $ i ] [ $ j ] . " ▁ " ; echo " STRNEWLINE " ; } } $ n = 8 ; doublyEven ( $ n ) ; ? >
Kronecker Product of two matrices | rowa and cola are no of rows and columns of matrix A rowb and colb are no of rows and columns of matrix B ; Function to computes the Kronecker Product of two matrices ; i loops till rowa ; k loops till rowb ; j loops till cola ; l loops till colb ; Each element of matrix A is multiplied by whole Matrix B resp and stored as Matrix C ; Driver Code
< ? php $ cola = 2 ; $ rowa = 3 ; $ colb = 3 ; $ rowb = 2 ; function Kroneckerproduct ( $ A , $ B ) { global $ cola ; global $ rowa ; global $ colb ; global $ rowb ; $ C ; for ( $ i = 0 ; $ i < $ rowa ; $ i ++ ) { for ( $ k = 0 ; $ k < $ rowb ; $ k ++ ) { for ( $ j = 0 ; $ j < $ cola ; $ j ++ ) { for ( $ l = 0 ; $ l < $ colb ; $ l ++ ) { $ C [ $ i + $ l + 1 ] [ $ j + $ k + 1 ] = $ A [ $ i ] [ $ j ] * $ B [ $ k ] [ $ l ] ; echo ( $ C [ $ i + $ l + 1 ] [ $ j + $ k + 1 ] ) , " TABSYMBOL " ; } } echo " STRNEWLINE " ; } } } $ A = array ( array ( 1 , 2 ) , array ( 3 , 4 ) , array ( 1 , 0 ) ) ; $ B = array ( array ( 0 , 5 , 2 ) , array ( 6 , 7 , 3 ) ) ; Kroneckerproduct ( $ A , $ B ) ; ? >
Program to check if matrix is lower triangular | PHP Program to check lower triangular matrix . ; Function to check matrix is in lower triangular form or not . ; Driver Code ; Function call
< ? php $ N = 4 ; function isLowerTriangularMatrix ( $ mat ) { global $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != 0 ) return false ; return true ; } $ mat = array ( array ( 1 , 0 , 0 , 0 ) , array ( 1 , 4 , 0 , 0 ) , array ( 4 , 6 , 2 , 0 ) , array ( 0 , 4 , 7 , 6 ) ) ; if ( isLowerTriangularMatrix ( $ mat ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Program to check if matrix is upper triangular | PHP Program to check upper triangular matrix . ; Function to check matrix is in upper triangular form or not . ; Driver Code
< ? php $ N = 4 ; function isUpperTriangularMatrix ( $ mat ) { global $ N ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != 0 ) return false ; return true ; } $ mat = array ( array ( 1 , 3 , 5 , 3 ) , array ( 0 , 4 , 6 , 2 ) , array ( 0 , 0 , 2 , 5 ) , array ( 0 , 0 , 0 , 6 ) ) ; if ( isUpperTriangularMatrix ( $ mat ) ) echo " Yes " ; else echo " No " ; ? >
Counting sets of 1 s and 0 s in a binary matrix | no of columns ; no of rows ; function to calculate the number of non empty sets of cell ; stores the final answer ; traverses row - wise ; traverses column wise ; at the end subtract n * m as no of single sets have been added twice . ; Driver Code
< ? php $ m = 3 ; $ n = 2 ; function countSets ( $ a ) { global $ m , $ n ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ u = 0 ; $ v = 0 ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) $ a [ $ i ] [ $ j ] ? $ u ++ : $ v ++ ; $ res += pow ( 2 , $ u ) - 1 + pow ( 2 , $ v ) - 1 ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ u = 0 ; $ v = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ a [ $ j ] [ $ i ] ? $ u ++ : $ v ++ ; $ res += pow ( 2 , $ u ) - 1 + pow ( 2 , $ v ) - 1 ; } return $ res - ( $ n * $ m ) ; } $ a = array ( array ( 1 , 0 , 1 ) , array ( 0 , 1 , 0 ) ) ; echo countSets ( $ a ) ; ? >
Program to check if a matrix is symmetric | Fills transpose of mat [ N ] [ N ] in tr [ N ] [ N ] ; Returns true if mat [ N ] [ N ] is symmetric , else false ; Driver code
< ? php for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != $ tr [ $ i ] [ $ j ] ) return false ; return true ; } function isSymmetric ( $ mat , $ N ) { $ tr = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ tr [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ mat = array ( array ( 1 , 3 , 5 ) , array ( 3 , 2 , 4 ) , array ( 5 , 4 , 1 ) ) ; if ( isSymmetric ( $ mat , 3 ) ) echo " Yes " ; else echo " No " ; ? >
Program to check if a matrix is symmetric | Efficient PHP code for check a matrix is symmetric or not . ; Returns true if mat [ N ] [ N ] is symmetric , else false ; Driver code
< ? php $ MAX = 100 ; function isSymmetric ( $ mat , $ N ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != $ mat [ $ j ] [ $ i ] ) return false ; return true ; } $ mat = array ( array ( 1 , 3 , 5 ) , array ( 3 , 2 , 4 ) , array ( 5 , 4 , 1 ) ) ; if ( isSymmetric ( $ mat , 3 ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Program to find Normal and Trace of a matrix | Size of given matrix ; Returns Normal of a matrix of size n x n ; Returns trace of a matrix of size n x n ; Driver Code
< ? php $ MAX = 100 ; function findNormal ( $ mat , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ mat [ $ i ] [ $ j ] * $ mat [ $ i ] [ $ j ] ; return floor ( sqrt ( $ sum ) ) ; } function findTrace ( $ mat , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ mat [ $ i ] [ $ i ] ; return $ sum ; } $ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 , 4 ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; echo " Trace ▁ of ▁ Matrix ▁ = ▁ " , findTrace ( $ mat , 5 ) , " STRNEWLINE " ; echo " Normal ▁ of ▁ Matrix ▁ = ▁ " , findNormal ( $ mat , 5 ) ; ? >
Maximum determinant of a matrix with every values either 0 or n | Function for maximum determinant ; Function to print resulatant matrix ; three position where 0 appears ; position where n appears ; Driver code
< ? php function maxDet ( $ n ) { return ( 2 * $ n * $ n * $ n ) ; } function resMatrix ( $ n ) { for ( $ i = 0 ; $ i < 3 ; $ i ++ ) { for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { if ( $ i == 0 && $ j == 2 ) echo "0 ▁ " ; else if ( $ i == 1 && $ j == 0 ) echo "0 ▁ " ; else if ( $ i == 2 && $ j == 1 ) echo "0 ▁ " ; else echo $ n , " ▁ " ; } echo " STRNEWLINE " ; } } $ n = 15 ; echo " Maximum ▁ Determinant ▁ = ▁ " , maxDet ( $ n ) ; echo " Resultant Matrix : " resMatrix ( $ n ) ; ? >
Count Negative Numbers in a Column | PHP implementation of Naive method to count of negative numbers in M [ n ] [ m ] ; Follow the path shown using arrows above ; no more negative numbers in this row ; Driver Code
< ? php function countNegative ( $ M , $ n , $ m ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ M [ $ i ] [ $ j ] < 0 ) $ count += 1 ; else break ; } } return $ count ; } $ M = array ( array ( -3 , -2 , -1 , 1 ) , array ( -2 , 2 , 3 , 4 ) , array ( 4 , 5 , 7 , 8 ) ) ; echo countNegative ( $ M , 3 , 4 ) ; ? >
Count Negative Numbers in a Column | Function to count negative number ; initialize result ; Start with top right corner ; Follow the path shown using arrows above ; j is the index of the last negative number in this row . So there must be ( j + 1 ) ; negative numbers in this row . ; move to the left and see if we can find a negative number there ; Driver Code
< ? php function countNegative ( $ M , $ n , $ m ) { $ count = 0 ; $ i = 0 ; $ j = $ m - 1 ; while ( $ j >= 0 and $ i < $ n ) { if ( $ M [ $ i ] [ $ j ] < 0 ) { $ count += $ j + 1 ; $ i += 1 ; } else $ j -= 1 ; } return $ count ; } $ M = array ( array ( -3 , -2 , -1 , 1 ) , array ( -2 , 2 , 3 , 4 ) , array ( 4 , 5 , 7 , 8 ) ) ; echo countNegative ( $ M , 3 , 4 ) ; return 0 ; ? >
Find a specific pair in Matrix | A Naive method to find maximum value of $mat [ d ] [ e ] - ma [ a ] [ b ] such that $d > $a and $e > $b ; The function returns maximum value A ( d , e ) - A ( a , b ) over all choices of indexes such that both $d > $a and $e > $b . ; stores maximum value ; Consider all possible pairs $mat [ $a ] [ $b ] and $mat [ $d ] [ $e ] ; Driver Code
< ? php $ N = 5 ; function findMaxValue ( & $ mat ) { global $ N ; $ maxValue = PHP_INT_MIN ; for ( $ a = 0 ; $ a < $ N - 1 ; $ a ++ ) for ( $ b = 0 ; $ b < $ N - 1 ; $ b ++ ) for ( $ d = $ a + 1 ; $ d < $ N ; $ d ++ ) for ( $ e = $ b + 1 ; $ e < $ N ; $ e ++ ) if ( $ maxValue < ( $ mat [ $ d ] [ $ e ] - $ mat [ $ a ] [ $ b ] ) ) $ maxValue = $ mat [ $ d ] [ $ e ] - $ mat [ $ a ] [ $ b ] ; return $ maxValue ; } $ mat = array ( array ( 1 , 2 , -1 , -4 , -20 ) , array ( -8 , -3 , 4 , 2 , 1 ) , array ( 3 , 8 , 6 , 1 , 3 ) , array ( -4 , -1 , 1 , 7 , -6 ) , array ( 0 , -4 , 10 , -5 , 1 ) ) ; echo " Maximum ▁ Value ▁ is ▁ " . findMaxValue ( $ mat ) ; ? >
Find a specific pair in Matrix | An efficient method to find maximum value of mat [ d ] - ma [ a ] [ b ] such that c > a and d > b ; The function returns maximum value A ( c , d ) - A ( a , b ) over all choices of indexes such that both c > a and d > b . ; stores maximum value ; maxArr [ i ] [ j ] stores max of elements in matrix from ( i , j ) to ( N - 1 , N - 1 ) ; last element of maxArr will be same 's as of  the input matrix ; preprocess last row Initialize max ; preprocess last column Initialize max ; preprocess rest of the matrix from bottom ; Update maxValue ; set maxArr ( i , j ) ; Driver Code
< ? php $ N = 5 ; function findMaxValue ( $ mat ) { global $ N ; $ maxValue = PHP_INT_MIN ; $ maxArr [ $ N ] [ $ N ] = array ( ) ; $ maxArr [ $ N - 1 ] [ $ N - 1 ] = $ mat [ $ N - 1 ] [ $ N - 1 ] ; $ maxv = $ mat [ $ N - 1 ] [ $ N - 1 ] ; for ( $ j = $ N - 2 ; $ j >= 0 ; $ j -- ) { if ( $ mat [ $ N - 1 ] [ $ j ] > $ maxv ) $ maxv = $ mat [ $ N - 1 ] [ $ j ] ; $ maxArr [ $ N - 1 ] [ $ j ] = $ maxv ; } $ maxv = $ mat [ $ N - 1 ] [ $ N - 1 ] ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { if ( $ mat [ $ i ] [ $ N - 1 ] > $ maxv ) $ maxv = $ mat [ $ i ] [ $ N - 1 ] ; $ maxArr [ $ i ] [ $ N - 1 ] = $ maxv ; } for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { for ( $ j = $ N - 2 ; $ j >= 0 ; $ j -- ) { if ( $ maxArr [ $ i + 1 ] [ $ j + 1 ] - $ mat [ $ i ] [ $ j ] > $ maxValue ) $ maxValue = $ maxArr [ $ i + 1 ] [ $ j + 1 ] - $ mat [ $ i ] [ $ j ] ; $ maxArr [ $ i ] [ $ j ] = max ( $ mat [ $ i ] [ $ j ] , max ( $ maxArr [ $ i ] [ $ j + 1 ] , $ maxArr [ $ i + 1 ] [ $ j ] ) ) ; } } return $ maxValue ; } $ mat = array ( array ( 1 , 2 , -1 , -4 , -20 ) , array ( -8 , -3 , 4 , 2 , 1 ) , array ( 3 , 8 , 6 , 1 , 3 ) , array ( -4 , -1 , 1 , 7 , -6 ) , array ( 0 , -4 , 10 , -5 , 1 ) ) ; echo " Maximum ▁ Value ▁ is ▁ " . findMaxValue ( $ mat ) ; ? >
Given an n x n square matrix , find sum of all sub | size k x k Size of given matrix ; function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; row number of first cell in current sub - square of size k x k ; column of first cell in current sub - square of size k x k ; Calculate and print sum of current sub - square ; Line separator for sub - squares starting with next row ; Driver Code
< ? php $ n = 5 ; function printSumSimple ( $ mat , $ k ) { global $ n ; if ( $ k > $ n ) return ; for ( $ i = 0 ; $ i < $ n - $ k + 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n - $ k + 1 ; $ j ++ ) { $ sum = 0 ; for ( $ p = $ i ; $ p < $ k + $ i ; $ p ++ ) for ( $ q = $ j ; $ q < $ k + $ j ; $ q ++ ) $ sum += $ mat [ $ p ] [ $ q ] ; echo $ sum , " " ; } echo " STRNEWLINE " ; } } $ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 , ) , array ( 3 , 3 , 3 , 3 , 3 , ) , array ( 4 , 4 , 4 , 4 , 4 , ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; $ k = 3 ; printSumSimple ( $ mat , $ k ) ; ? >