Let’s understand it better through the given diagram:
Note: Working with 2D matrices frequently involves iterating over the matrix’s rows and columns and executing operations on the individual elements.
Here are some of the reasons why 2D arrays are important in data structures and algorithms:
Let us now discuss various 2D matrix examples:
matrix
in spiral order:Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
Solution:
class Solution {
public List<Integer> spiralOrder(int[][] matrix)
{
int n = matrix.length;
int m = matrix[0].length;
ArrayList<Integer> arr = new ArrayList<>();
int top = 0 ;
int bottom = n-1;
int right = m-1;
int left = 0;
while(top<=bottom && left<=right){
for(int i = left ; i<=right ;i++){
arr.add(matrix[top][i]);
}
top++;
for(int i = top ;i<=bottom;i++ ){
arr.add(matrix[i][right]);
}
right--;
if(top<=bottom){
for(int i = right;i>=left;i--){
arr.add(matrix[bottom][i]);
}
bottom--;
}
if(left<=right){
for(int i = bottom;i>=top;i--){
arr.add(matrix[i][left]);
}
left++;
}
}
return arr;
}
public static void main(String[] args) {
Solution obj= new Solution();
Scanner sc= new Scanner (System.in);
int array[][] = new int[3][3];
// Read the matrix values
System.out.println("Enter the elements of the array: ");
//loop for row
for (int i = 0; i < 3; i++) {
//inner for loop for column
for (int j = 0; j < 3; j++)
array[i][j] = sc.nextInt();
}
System.out.println(obj.spiralOrder(array));
}
}
To print the matrix in spiral the loop iterates through the matrix in four directions:
left
to right
, adding each element to the arr
ArrayList.top
to bottom
, adding each element to the arr
ArrayList.right
to left
, adding each element to the arr
ArrayList.bottom
to top
, adding each element to the arr
ArrayList.Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]]
Solution:
public class Solution {
public void rotate(int[][] M) {
for (int i = 0; i < (M.length+1)/2; i++) {
for (int j = 0; j < M.length/2; j++) {
int tmp = M[i][j];
M[i][j] = M[M.length-j-1][i];
M[M.length-j-1][i] = M[M.length-i-1][M.length-j-1];
M[M.length-i-1][M.length-j-1] = M[j][M.length-i-1];
M[j][M.length-i-1] = tmp;
}
}
}
}
Instead of using the longer method, the approach used in this solution is to divide the matrix into smaller concentric squares and rotate each square individually. That is,
Note: also read about DSA: SubArray
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…