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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…