codeparrot/xlcost-text-to-code huggingface dataset:
Maximum product of 4 adjacent elements in matrix | PHP program to find out the maximum product in the matrix which four elements are adjacent to each other in one direction ; function to find max product ; iterate the rows . ; iterate the columns . ; check the maximum product in horizontal row . ; check the maximum product in vertical row . ; check the maximum product in diagonal going through down - right ; check the maximum product in diagonal going through up - right ; Driver Code
< ? php $ n = 5 ; function FindMaxProduct ( $ arr , $ n ) { $ max = 0 ; $ result ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ j - 3 ) >= 0 ) { $ result = $ arr [ $ i ] [ $ j ] * $ arr [ $ i ] [ $ j - 1 ] * $ arr [ $ i ] [ $ j - 2 ] * $ arr [ $ i ] [ $ j - 3 ] ; if ( $ max < $ result ) $ max = $ result ; } if ( ( $ i - 3 ) >= 0 ) { $ result = $ arr [ $ i ] [ $ j ] * $ arr [ $ i - 1 ] [ $ j ] * $ arr [ $ i - 2 ] [ $ j ] * $ arr [ $ i - 3 ] [ $ j ] ; if ( $ max < $ result ) $ max = $ result ; } if ( ( $ i - 3 ) >= 0 and ( $ j - 3 ) >= 0 ) { $ result = $ arr [ $ i ] [ $ j ] * $ arr [ $ i - 1 ] [ $ j - 1 ] * $ arr [ $ i - 2 ] [ $ j - 2 ] * $ arr [ $ i - 3 ] [ $ j - 3 ] ; if ( $ max < $ result ) $ max = $ result ; } if ( ( $ i - 3 ) >= 0 and ( $ j - 1 ) <= 0 ) { $ result = $ arr [ $ i ] [ $ j ] * $ arr [ $ i - 1 ] [ $ j + 1 ] * $ arr [ $ i - 2 ] [ $ j + 2 ] * $ arr [ $ i - 3 ] [ $ j + 3 ] ; if ( $ max < $ result ) $ max = $ result ; } } } return $ max ; } $ arr = array ( array ( 1 , 2 , 3 , 4 , 5 ) , array ( 6 , 7 , 8 , 9 , 1 ) , array ( 2 , 3 , 4 , 5 , 6 ) , array ( 7 , 8 , 9 , 1 , 0 ) , array ( 9 , 6 , 4 , 2 , 3 ) ) ; echo FindMaxProduct ( $ arr , $ n ) ; ? >
Minimum flip required to make Binary Matrix symmetric | PHP Program to find minimum flip required to make ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; finding the transpose of the matrix ; Finding the number of position where element are not same . ; Driver Code
< ? php $ N = 3 ; function minimumflip ( $ mat , $ n ) { global $ N ; $ transpose ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ transpose [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ flip = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ transpose [ $ i ] [ $ j ] != $ mat [ $ i ] [ $ j ] ) $ flip ++ ; return $ flip / 2 ; } $ n = 3 ; $ mat = array ( array ( 0 , 0 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 0 , 0 ) ) ; echo minimumflip ( $ mat , $ n ) , " STRNEWLINE " ; ? >
Minimum flip required to make Binary Matrix symmetric | PHP Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; Comparing elements across diagonal ; Driver Code
< ? php $ N = 3 ; function minimumflip ( $ mat , $ n ) { $ flip = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != $ mat [ $ j ] [ $ i ] ) $ flip ++ ; return $ flip ; } $ n = 3 ; $ mat = array ( array ( 0 , 0 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 0 , 0 ) ) ; echo minimumflip ( $ mat , $ n ) , " STRNEWLINE " ; ? >
Frequencies of even and odd numbers in a matrix | PHP Program to Find the frequency of even and odd numbers in a matrix ; function for calculating frequency ; modulo by 2 to check even and odd ; print Frequency of numbers ; Driver code
< ? php $ MAX = 100 ; function freq ( $ ar , $ m , $ n ) { $ even = 0 ; $ odd = 0 ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) { for ( $ j = 0 ; $ j < $ n ; ++ $ j ) { if ( ( $ ar [ $ i ] [ $ j ] % 2 ) == 0 ) ++ $ even ; else ++ $ odd ; } } echo " ▁ Frequency ▁ of ▁ odd ▁ number ▁ = ▁ " , $ odd , " STRNEWLINE " ; echo " ▁ Frequency ▁ of ▁ even ▁ number ▁ = ▁ " , $ even ; } $ m = 3 ; $ n = 3 ; $ array = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; freq ( $ array , $ m , $ n ) ; ? >
Center element of matrix equals sums of half diagonals | PHP Program to check if the center element is equal to the individual sum of all the half diagonals ; Function to Check center element is equal to the individual sum of all the half diagonals ; Find sums of half diagonals ; Driver code
< ? php $ MAX = 100 ; function HalfDiagonalSums ( $ mat , $ n ) { global $ MAX ; $ diag1_left = 1 ; $ diag1_right = 1 ; $ diag2_left = 1 ; $ diag2_right = 1 ; for ( $ i = 0 , $ j = $ n - 1 ; $ i < $ n ; $ i ++ , $ j -- ) { if ( $ i < $ n / 2 ) { $ diag1_left += $ mat [ $ i ] [ $ i ] ; $ diag2_left += $ mat [ $ j ] [ $ i ] ; } else if ( $ i > $ n / 2 ) { $ diag1_right += $ mat [ $ i ] [ $ i ] ; $ diag2_right += $ mat [ $ j ] [ $ i ] ; } } return ( $ diag1_left == $ diag2_right && $ diag2_right == $ diag2_left && $ diag1_right == $ diag2_left && $ diag2_right == $ mat [ $ n / 2 ] [ $ n / 2 ] ) ; } $ a = array ( array ( 2 , 9 , 1 , 4 , -2 ) , array ( 6 , 7 , 2 , 11 , 4 ) , array ( 4 , 2 , 9 , 2 , 4 ) , array ( 1 , 9 , 2 , 4 , 4 ) , array ( 0 , 2 , 4 , 2 , 5 ) ) ; if ( HalfDiagonalSums ( $ a , 5 ) == 0 ) echo " Yes " ; else echo " No " ; ? >
Program for Identity Matrix | PHP program to print Identity Matrix ; Checking if row is equal to column ; Driver Code
< ? php function Identity ( $ num ) { $ row ; $ col ; for ( $ row = 0 ; $ row < $ num ; $ row ++ ) { for ( $ col = 0 ; $ col < $ num ; $ col ++ ) { if ( $ row == $ col ) echo 1 , " ▁ " ; else echo 0 , " ▁ " ; } echo " STRNEWLINE " ; } return 0 ; } $ size = 5 ; identity ( $ size ) ; ? >
Program for Identity Matrix | PHP program to check if a given matrix is identity $MAX = 100 ; ; Driver Code
< ? php function isIdentity ( $ mat , $ N ) { for ( $ row = 0 ; $ row < $ N ; $ row ++ ) { for ( $ col = 0 ; $ col < $ N ; $ col ++ ) { if ( $ row == $ col and $ mat [ $ row ] [ $ col ] != 1 ) return false ; else if ( $ row != $ col && $ mat [ $ row ] [ $ col ] != 0 ) return false ; } } return true ; } $ N = 4 ; $ mat = array ( array ( 1 , 0 , 0 , 0 ) , array ( 0 , 1 , 0 , 0 ) , array ( 0 , 0 , 1 , 0 ) , array ( 0 , 0 , 0 , 1 ) ) ; if ( isIdentity ( $ mat , $ N ) ) echo " Yes ▁ " ; else echo " No ▁ " ; ? >
Ways of filling matrix such that product of all rows and all columns are equal to unity | PHP program to find number of ways to fill a matrix under given constraints ; Returns a raised power t under modulo mod ; Counting number of ways of filling the matrix ; Function calculating the answer ; if sum of numbers of rows and columns is odd i . e ( n + m ) % 2 == 1 and k = - 1 then there are 0 ways of filiing the matrix . ; If there is one row or one column then there is only one way of filling the matrix ; If the above cases are not followed then we find ways to fill the n - 1 rows and m - 1 columns which is 2 ^ ( ( m - 1 ) * ( n - 1 ) ) . ; Driver Code
< ? php $ mod = 100000007 ; function modPower ( $ a , $ t ) { global $ mod ; $ now = $ a ; $ ret = 1 ; while ( $ t ) { if ( $ t & 1 ) $ ret = $ now * ( $ ret % $ mod ) ; $ now = $ now * ( $ now % $ mod ) ; $ t >>= 1 ; } return $ ret ; } function countWays ( $ n , $ m , $ k ) { global $ mod ; if ( $ k == -1 and ( $ n + $ m ) % 2 == 1 ) return 0 ; if ( $ n == 1 or $ m == 1 ) return 1 ; return ( modPower ( modPower ( 2 , $ n - 1 ) , $ m - 1 ) % $ mod ) ; } $ n = 2 ; $ m = 7 ; $ k = 1 ; echo countWays ( $ n , $ m , $ k ) ; ? >
Mirror of matrix across diagonal | Efficient PHP program to find mirror of matrix across diagonal . ; traverse a matrix and swap mat [ i ] [ j ] with mat [ j ] [ i ] ; Utility function to print a matrix ; Driver Code
< ? php function imageSwap ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) $ mat [ $ i ] [ $ j ] = $ mat [ $ i ] [ $ j ] + $ mat [ $ j ] [ $ i ] - ( $ mat [ $ j ] [ $ i ] = $ mat [ $ i ] [ $ j ] ) ; } function printMatrix ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo ( $ mat [ $ i ] [ $ j ] ) ; echo ( " ▁ " ) ; } echo ( " STRNEWLINE " ) ; } } $ mat = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 9 , 10 , 11 , 12 ) , array ( 13 , 14 , 15 , 16 ) ) ; $ n = 4 ; imageSwap ( $ mat , $ n ) ; printMatrix ( $ mat , $ n ) ; ? >
Search in a row wise and column wise sorted matrix | Searches the element $x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for top right element ; if $mat [ $i ] [ $j ] < $x ; if ( $i == $n $j == - 1 ) ; Driver Code
< ? php function search ( & $ mat , $ n , $ x ) { $ i = 0 ; $ j = $ n - 1 ; while ( $ i < $ n && $ j >= 0 ) { if ( $ mat [ $ i ] [ $ j ] == $ x ) { echo " n ▁ found ▁ at ▁ " . $ i . " , ▁ " . $ j ; return 1 ; } if ( $ mat [ $ i ] [ $ j ] > $ x ) $ j -- ; else $ i ++ ; } echo " n ▁ Element ▁ not ▁ found " ; return 0 ; } $ mat = array ( array ( 10 , 20 , 30 , 40 ) , array ( 15 , 25 , 35 , 45 ) , array ( 27 , 29 , 37 , 48 ) , array ( 32 , 33 , 39 , 50 ) ) ; search ( $ mat , 4 , 29 ) ; ? >
Create a matrix with alternating rectangles of O and X | Function to print alternating rectangles of 0 and X ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Store given number of rows and columns for later use ; Iniitialize the character to be stoed in a [ ] [ ] ; Fill characters in a [ ] [ ] in spiral form . Every iteration fills one rectangle of either Xs or Os ; Fill the first row from the remaining rows ; Fill the last column from the remaining columns ; Fill the last row from the remaining rows ; Print the first column from the remaining columns ; Flip character for next iteration ; Print the filled matrix ; Driver Code
< ? php function fill0X ( $ m , $ n ) { $ k = 0 ; $ l = 0 ; $ r = $ m ; $ c = $ n ; $ x = ' X ' ; while ( $ k < $ m && $ l < $ n ) { for ( $ i = $ l ; $ i < $ n ; ++ $ i ) $ a [ $ k ] [ $ i ] = $ x ; $ k ++ ; for ( $ i = $ k ; $ i < $ m ; ++ $ i ) $ a [ $ i ] [ $ n - 1 ] = $ x ; $ n -- ; if ( $ k < $ m ) { for ( $ i = $ n - 1 ; $ i >= $ l ; -- $ i ) $ a [ $ m - 1 ] [ $ i ] = $ x ; $ m -- ; } if ( $ l < $ n ) { for ( $ i = $ m - 1 ; $ i >= $ k ; -- $ i ) $ a [ $ i ] [ $ l ] = $ x ; $ l ++ ; } $ x = ( $ x == '0' ) ? ' X ' : '0' ; } for ( $ i = 0 ; $ i < $ r ; $ i ++ ) { for ( $ j = 0 ; $ j < $ c ; $ j ++ ) echo ( $ a [ $ i ] [ $ j ] . " ▁ " ) ; echo " STRNEWLINE " ; } } echo " Output ▁ for ▁ m ▁ = ▁ 5 , ▁ n ▁ = ▁ 6 STRNEWLINE " ; fill0X ( 5 , 6 ) ; echo " Output for m = 4 , n = 4 " ; fill0X ( 4 , 4 ) ; echo " Output for m = 3 , n = 4 " ; fill0X ( 3 , 4 ) ; ? >
Minimum cost to sort a matrix of numbers from 0 to n ^ 2 | function to find the total energy required to rearrange the numbers ; nested loops to access the elements of the given matrix ; store quotient ; final destination location ( i_des , j_des ) of the element mat [ i ] [ j ] is being calculated ; energy required for the movement of the element mat [ i ] [ j ] is calculated and then accumulated in the ' tot _ energy ' ; required total energy ; Driver Code
< ? php function calculateEnergy ( $ mat , $ n ) { $ i_des ; $ j_des ; $ q ; $ tot_energy = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ q = ( int ) ( $ mat [ $ i ] [ $ j ] / $ n ) ; $ i_des = $ q ; $ j_des = $ mat [ $ i ] [ $ j ] - ( $ n * $ q ) ; $ tot_energy += abs ( $ i_des - $ i ) + abs ( $ j_des - $ j ) ; } } return $ tot_energy ; } $ mat = array ( array ( 4 , 7 , 0 , 3 ) , array ( 8 , 5 , 6 , 1 ) , array ( 9 , 11 , 10 , 2 ) , array ( 15 , 13 , 14 , 12 ) ) ; $ n = 4 ; echo " Total ▁ energy ▁ required ▁ = ▁ " , calculateEnergy ( $ mat , $ n ) , " ▁ units " ; ? >
Unique cells in a binary matrix | PHP program to count unique cells in a matrix ; Returns true if mat [ i ] [ j ] is unique ; checking in row calculating sumrow will be moving column wise ; checking in column calculating sumcol will be moving row wise ; Driver code
< ? php $ MAX = 100 ; function isUnique ( $ mat , $ i , $ j , $ n , $ m ) { global $ MAX ; $ sumrow = 0 ; for ( $ k = 0 ; $ k < $ m ; $ k ++ ) { $ sumrow += $ mat [ $ i ] [ $ k ] ; if ( $ sumrow > 1 ) return false ; } $ sumcol = 0 ; for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { $ sumcol += $ mat [ $ k ] [ $ j ] ; if ( $ sumcol > 1 ) return false ; } return true ; } function countUnique ( $ mat , $ n , $ m ) { $ uniquecount = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] && isUnique ( $ mat , $ i , $ j , $ n , $ m ) ) $ uniquecount ++ ; return $ uniquecount ; } $ mat = array ( array ( 0 , 1 , 0 , 0 ) , array ( 0 , 0 , 1 , 0 ) , array ( 1 , 0 , 0 , 1 ) ) ; echo countUnique ( $ mat , 3 , 4 ) ; ? >
Check if a given matrix is sparse or not | PHP code to check if a matrix is sparse . ; Count number of zeros in the matrix ; Driver Code
< ? php $ MAX = 100 ; function isSparse ( $ array , $ m , $ n ) { $ counter = 0 ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) for ( $ j = 0 ; $ j < $ n ; ++ $ j ) if ( $ array [ $ i ] [ $ j ] == 0 ) ++ $ counter ; return ( $ counter > ( ( $ m * $ n ) / 2 ) ) ; } $ array = array ( array ( 1 , 0 , 3 ) , array ( 0 , 0 , 4 ) , array ( 6 , 0 , 0 ) ) ; $ m = 3 ; $ n = 3 ; if ( isSparse ( $ array , $ m , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Row | PHP program to find common elements in two diagonals . ; Returns count of row wise same elements in two diagonals of mat [ n ] [ n ] ; Driver Code
< ? php $ MAX = 100 ; function countCommon ( $ mat , $ n ) { global $ MAX ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ mat [ $ i ] [ $ i ] == $ mat [ $ i ] [ $ n - $ i - 1 ] ) $ res ++ ; return $ res ; } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; echo countCommon ( $ mat , 3 ) ; ? >
Check if sums of i | Function to check the if sum of a row is same as corresponding column ; number of rows ; number of columns
< ? php function areSumSame ( $ a , $ n , $ m ) { $ sum1 = 0 ; $ sum2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 = 0 ; $ sum2 = 0 ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { $ sum1 += $ a [ $ i ] [ $ j ] ; $ sum2 += $ a [ $ j ] [ $ i ] ; } if ( $ sum1 == $ sum2 ) return true ; } return false ; } $ n = 4 ; $ m = 4 ; $ M = array ( array ( 1 , 2 , 3 , 4 ) , array ( 9 , 5 , 3 , 1 ) , array ( 0 , 3 , 5 , 6 ) , array ( 0 , 4 , 5 , 6 ) ) ; echo areSumSame ( $ M , $ n , $ m ) ; ? >
Find row number of a binary matrix having maximum number of 1 s | PHP program to find row with maximum 1 in row sorted binary matrix ; function for finding row with maximum 1 ; find left most position of 1 in a row find 1 st zero in a row ; Driver Code
< ? php $ N = 4 ; function findMax ( $ arr ) { global $ N ; $ row = 0 ; $ i ; $ j = $ N - 1 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { while ( $ arr [ $ i ] [ $ j ] == 1 && $ j >= 0 ) { $ row = $ i ; $ j -- ; } } echo " Row ▁ number ▁ = ▁ " , $ row + 1 ; echo " , MaxCount = " } $ arr = array ( array ( 0 , 0 , 0 , 1 ) , array ( 0 , 0 , 0 , 1 ) , array ( 0 , 0 , 0 , 0 ) , array ( 0 , 1 , 1 , 1 ) ) ; findMax ( $ arr ) ; ? >
Possible moves of knight | PHP program to find number of possible moves of knight ; To calculate possible moves ; All possible moves of a knight ; Check if each possible move is valid or not ; Position of knight after move ; count valid moves ; Return number of possible moves ; Driver Code
< ? php $ n = 4 ; $ m = 4 ; function findPossibleMoves ( $ mat , $ p , $ q ) { global $ n ; global $ m ; $ X = array ( 2 , 1 , -1 , -2 , -2 , -1 , 1 , 2 ) ; $ Y = array ( 1 , 2 , 2 , 1 , -1 , -2 , -2 , -1 ) ; $ count = 0 ; for ( $ i = 0 ; $ i < 8 ; $ i ++ ) { $ x = $ p + $ X [ $ i ] ; $ y = $ q + $ Y [ $ i ] ; if ( $ x >= 0 && $ y >= 0 && $ x < $ n && $ y < $ m && $ mat [ $ x ] [ $ y ] == 0 ) $ count ++ ; } return $ count ; } $ mat = array ( array ( 1 , 0 , 1 , 0 ) , array ( 0 , 1 , 1 , 1 ) , array ( 1 , 1 , 0 , 1 ) , array ( 0 , 1 , 1 , 1 ) ) ; $ p = 2 ; $ q = 2 ; echo findPossibleMoves ( $ mat , $ p , $ q ) ; ? >
Efficiently compute sums of diagonals of a matrix | A simple PHP program to find sum of diagonals ; Condition for principal diagonal ; Condition for secondary diagonal ; Driver code
< ? php $ MAX = 100 ; function printDiagonalSums ( $ mat , $ n ) { global $ MAX ; $ principal = 0 ; $ secondary = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) $ principal += $ mat [ $ i ] [ $ j ] ; if ( ( $ i + $ j ) == ( $ n - 1 ) ) $ secondary += $ mat [ $ i ] [ $ j ] ; } } echo " Principal ▁ Diagonal : " , $ principal , " STRNEWLINE " ; echo " Secondary ▁ Diagonal : " , $ secondary , " STRNEWLINE " ; } $ a = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) ) ; printDiagonalSums ( $ a , 4 ) ; ? >
Efficiently compute sums of diagonals of a matrix | An efficient PHP program to find sum of diagonals ; Driver Code
< ? php $ MAX = 100 ; function printDiagonalSums ( $ mat , $ n ) { global $ MAX ; $ principal = 0 ; $ secondary = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ principal += $ mat [ $ i ] [ $ i ] ; $ secondary += $ mat [ $ i ] [ $ n - $ i - 1 ] ; } echo " Principal ▁ Diagonal : " , $ principal , " STRNEWLINE " ; echo " Secondary ▁ Diagonal : " , $ secondary , " STRNEWLINE " ; } $ a = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) ) ; printDiagonalSums ( $ a , 4 ) ; ? >
Boundary elements of a Matrix | PHP program to print boundary element of matrix . ; Driver code
< ? php $ MAX = 100 ; function printBoundary ( $ a , $ m , $ n ) { global $ MAX ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == 0 ) echo $ a [ $ i ] [ $ j ] , " ▁ " ; else if ( $ i == $ m - 1 ) echo $ a [ $ i ] [ $ j ] , " ▁ " ; else if ( $ j == 0 ) echo $ a [ $ i ] [ $ j ] , " ▁ " ; else if ( $ j == $ n - 1 ) echo $ a [ $ i ] [ $ j ] , " ▁ " ; else echo " ▁ " , " ▁ " ; } echo " STRNEWLINE " ; } } $ a = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) ) ; printBoundary ( $ a , 4 , 4 ) ; ? >
Boundary elements of a Matrix | PHP program to find sum of boundary elements of matrix . ; Driver code
< ? php function getBoundarySum ( $ a , $ m , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == 0 ) $ sum += $ a [ $ i ] [ $ j ] ; else if ( $ i == $ m - 1 ) $ sum += $ a [ $ i ] [ $ j ] ; else if ( $ j == 0 ) $ sum += $ a [ $ i ] [ $ j ] ; else if ( $ j == $ n - 1 ) $ sum += $ a [ $ i ] [ $ j ] ; } } return $ sum ; } $ a = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) ) ; $ sum = getBoundarySum ( $ a , 4 , 4 ) ; echo " Sum ▁ of ▁ boundary ▁ elements ▁ is ▁ " , $ sum ; ? >
Print a matrix in a spiral form starting from a point | PHP program to print a matrix in spiral form . ; Driver code ; Function calling
< ? php $ MAX = 100 ; function printSpiral ( $ mat , $ r , $ c ) { global $ MAX ; $ i ; $ a = 0 ; $ b = 2 ; $ low_row = ( 0 > $ a ) ? 0 : $ a ; $ low_column = ( 0 > $ b ) ? 0 : $ b - 1 ; $ high_row = ( ( $ a + 1 ) >= $ r ) ? $ r - 1 : $ a + 1 ; $ high_column = ( ( $ b + 1 ) >= $ c ) ? $ c - 1 : $ b + 1 ; while ( ( $ low_row > 0 - $ r && $ low_column > 0 - $ c ) ) { for ( $ i = $ low_column + 1 ; $ i <= $ high_column && $ i < $ c && $ low_row >= 0 ; ++ $ i ) echo $ mat [ $ low_row ] [ $ i ] , " ▁ " ; $ low_row -= 1 ; for ( $ i = $ low_row + 2 ; $ i <= $ high_row && $ i < $ r && $ high_column < $ c ; ++ $ i ) echo $ mat [ $ i ] [ $ high_column ] , " ▁ " ; $ high_column += 1 ; for ( $ i = $ high_column - 2 ; $ i >= $ low_column && $ i >= 0 && $ high_row < $ r ; -- $ i ) echo $ mat [ $ high_row ] [ $ i ] , " ▁ " ; $ high_row += 1 ; for ( $ i = $ high_row - 2 ; $ i > $ low_row && $ i >= 0 && $ low_column >= 0 ; -- $ i ) echo $ mat [ $ i ] [ $ low_column ] , " ▁ " ; $ low_column -= 1 ; } echo " STRNEWLINE " ; } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; $ r = 3 ; $ c = 3 ; printSpiral ( $ mat , $ r , $ c ) ; ? >
Find difference between sums of two diagonals | PHP program to find the difference between the sum of diagonal . ; Initialize sums of diagonals ; finding sum of primary diagonal ; finding sum of secondary diagonal ; Absolute difference of the sums across the diagonals ; Driver Code
< ? php function difference ( $ arr , $ n ) { $ d1 = 0 ; $ d2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) $ d1 += $ arr [ $ i ] [ $ j ] ; if ( $ i == $ n - $ j - 1 ) $ d2 += $ arr [ $ i ] [ $ j ] ; } } return abs ( $ d1 - $ d2 ) ; } { $ n = 3 ; $ arr = array ( array ( 11 , 2 , 4 ) , array ( 4 , 5 , 6 ) , array ( 10 , 8 , -12 ) ) ; echo difference ( $ arr , $ n ) ; return 0 ; } ? >
Find difference between sums of two diagonals | PHP program to find the difference between the sum of diagonal . ; Initialize sums of diagonals ; Absolute difference of the sums across the diagonals ; Driver Code
< ? php function difference ( $ arr , $ n ) { $ d1 = 0 ; $ d2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ d1 += $ arr [ $ i ] [ $ i ] ; $ d2 += $ arr [ $ i ] [ $ n - $ i - 1 ] ; } return abs ( $ d1 - $ d2 ) ; } { $ n = 3 ; $ arr = array ( array ( 11 , 2 , 4 ) , array ( 4 , 5 , 6 ) , array ( 10 , 8 , -12 ) ) ; echo difference ( $ arr , $ n ) ; return 0 ; } ? >
Circular Matrix ( Construct a matrix with numbers 1 to m * n in spiral way ) | Fills a [ m ] [ n ] with values from 1 to m * n in spiral fashion . ; Initialize value to be filled in matrix ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver Code
< ? php function spiralFill ( $ m , $ n , & $ a ) { $ val = 1 ; $ k = 0 ; $ l = 0 ; while ( $ k < $ m && $ l < $ n ) { for ( $ i = $ l ; $ i < $ n ; ++ $ i ) $ a [ $ k ] [ $ i ] = $ val ++ ; $ k ++ ; for ( $ i = $ k ; $ i < $ m ; ++ $ i ) $ a [ $ i ] [ $ n - 1 ] = $ val ++ ; $ n -- ; if ( $ k < $ m ) { for ( $ i = $ n - 1 ; $ i >= $ l ; -- $ i ) $ a [ $ m - 1 ] [ $ i ] = $ val ++ ; $ m -- ; } if ( $ l < $ n ) { for ( $ i = $ m - 1 ; $ i >= $ k ; -- $ i ) $ a [ $ i ] [ $ l ] = $ val ++ ; $ l ++ ; } } } $ m = 4 ; $ n = 4 ; spiralFill ( $ m , $ n , $ a ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo ( $ a [ $ i ] [ $ j ] ) ; echo ( " ▁ " ) ; } echo ( " STRNEWLINE " ) ; } ? >
Maximum and Minimum in a square matrix . | PHP program for finding maximum and minimum in a matrix . ; Finds maximum and minimum in arr [ 0. . n - 1 ] [ 0. . n - 1 ] using pair wise comparisons ; Traverses rows one by one ; Compare elements from beginning and end of current row ; Driver Code
< ? php $ MAX = 100 ; function maxMin ( $ arr , $ n ) { $ min = PHP_INT_MAX ; $ max = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n / 2 ; $ j ++ ) { if ( $ arr [ $ i ] [ $ j ] > $ arr [ $ i ] [ $ n - $ j - 1 ] ) { if ( $ min > $ arr [ $ i ] [ $ n - $ j - 1 ] ) $ min = $ arr [ $ i ] [ $ n - $ j - 1 ] ; if ( $ max < $ arr [ $ i ] [ $ j ] ) $ max = $ arr [ $ i ] [ $ j ] ; } else { if ( $ min > $ arr [ $ i ] [ $ j ] ) $ min = $ arr [ $ i ] [ $ j ] ; if ( $ max < $ arr [ $ i ] [ $ n - $ j - 1 ] ) $ max = $ arr [ $ i ] [ $ n - $ j - 1 ] ; } } } echo " Maximum = " ▁ , ▁ $ max STRNEWLINE TABSYMBOL TABSYMBOL , " , Minimum = " } $ arr = array ( array ( 5 , 9 , 11 ) , array ( 25 , 0 , 14 ) , array ( 21 , 6 , 4 ) ) ; maxMin ( $ arr , 3 ) ; ? >