PHP Program to Interchange elements of first and last rows in matrix Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1The approach is very simple, we can simply swap the elements of first and last row of the matrix inorder to get the desired matrix as output.Below is the implementation of the approach : PHP <?php // PHP code to swap the element of first // and last row and display the result $n = 4; function interchangeFirstLast(&$m) { global $n; $rows = $n; // swapping of element between first // and last rows for ($i = 0; $i < $n; $i++) { $t = $m[0][$i]; $m[0][$i] = $m[$rows - 1][$i]; $m[$rows - 1][$i] = $t; } } // Driver function // input in the array $m = array(array(8, 9, 7, 6), array(4, 7, 6, 5), array( 3, 2, 1, 8), array(9, 9, 7, 7)); interchangeFirstLast($m); // printing the interchanged matrix for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) echo $m[$i][$j] . " "; echo " "; } ?> Output 9 9 7 7 4 7 6 5 3 2 1 8 8 9 7 6 Complexity Analysis:Time Complexity: O(N) where N is no of rows; as we are using single loop for interchanging first and last rows of a given matrix.Auxiliary Space: O(1), as we are not using any extra space.Please refer complete article on Interchange elements of first and last rows in matrix for more details! Comment More infoAdvertise with us Next Article PHP Program to Interchange elements of first and last rows in matrix kartik Follow Improve Article Tags : Matrix Computer Science Fundamentals Web Technologies PHP PHP Programs DSA +2 More Practice Tags : Matrix Similar Reads PHP Program to Find Sum of All Matrix Elements Finding the sum of all elements in a matrix is a common operation in mathematical computations and programming. In PHP, this can be achieved using various approaches, including loops and array functions. In this article, we will explore different methods to calculate the sum of all elements in a mat 4 min read PHP Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21]Output :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : The approach is very simple. The idea is to run the loop for no_of_rows. Check each element in 2 min read PHP Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find an element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanati 3 min read PHP Program to Form coils in a matrix Given a positive integer n which represents the dimensions of a 4n x 4n matrix with values from 1 to n filled from left to right and top to bottom. Form two coils from the matrix and print the coils.Examples:Â Â Input : n = 1; Output : Coil 1 : 10 6 2 3 4 8 12 16 Coil 2 : 7 11 15 14 13 9 5 1 Explanati 3 min read Swipe First & Last Element of an Array in PHP Swapping the first and last elements of an array is a common operation that can be useful in various scenarios, such as rearranging data or preparing it for specific algorithms. Below are the approaches to swipe the first and last element of an array in PHP: Table of Content Using Temporary Variable 3 min read Determine the first and last iteration in a foreach loop in PHP? In PHP, determining the first and last iteration in a foreach loop helps identify specific elements in a looped collection. The first iteration can be identified by a counter starting at zero, while the last iteration can be detected by comparing keys or counts.There are many ways to solve this prob 5 min read PHP Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1Output: Yes ,All rows are rotated permutation of each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No, Explanation : As 3, 2, 1 i 2 min read How to Remove Last Element from an Array in PHP ? Removing the last element from an array is a common operation in PHP. There are several approaches to achieve this. In this article, we will explore different methods to remove the last element from an array.Table of ContentUsing array_pop() FunctionUsing array_slice() FunctionUsing unset() Function 3 min read How to Get first N number of Elements From an Array in PHP? Given an array, the task is to get the first N number of elements from the array in PHP. There are various approaches to achieve this task. In this article, we will explore all approaches with detailed explanations.These are the following approaches:Table of Content Using array_slice() FunctionUsing 3 min read Like