Movatterモバイル変換


[0]ホーム

URL:


Open In App

Given a square matrix of order n × n, the task is to print its elements in Z form that is, print the first row, then the opposite diagonal (from top-right to bottom-left), and finally the last row of the matrix. For Example:

Input: [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ]
Output:1 2 3 5 7 8 9

Let's explore different methods to print the matrix in Z form in Python.

Using NumPy

This method uses NumPy’s advanced indexing to directly extract the first row, reverse diagonal, and last row in a single optimized line without explicit loops.

Python
importnumpyasnparr=[[4,5,6,8],[1,2,3,1],[7,8,9,4],[1,8,7,5]]m=np.array(arr)print(*m[0],*np.diag(np.fliplr(m))[1:-1],*m[-1])

Output
4 5 6 8 3 8 1 8 7 5

Explanation:

  • m[0] first row and np.fliplr(m) flips matrix left to right.
  • np.diag(...)[1:-1] extracts reverse diagonal excluding corners and m[-1] last row.
  • * unpacks all elements for printing.

Using List Comprehension

This approach combines rows and diagonals in a single list using list comprehension. It collects all required elements in order and prints them together fast and clean without extra loops.

Python
arr=[[4,5,6,8],[1,2,3,1],[7,8,9,4],[1,8,7,5]]n=len(arr)res=[*arr[0],*[arr[i][n-i-1]foriinrange(1,n-1)],*arr[-1]]print(*res)

Output
4 5 6 8 3 8 1 8 7 5

Explanation:

  • arr[0] first row and [arr[i][n - i - 1] ...] collects reverse diagonal elements.
  • arr[-1] last row and *res unpacks all values for clean printing.

Using Flattened Index Access

This method flattens the matrix into a single list and computes exact indices for the top row, diagonal, and bottom row, then prints those elements sequentially.

Python
arr=[[4,5,6,8],[1,2,3,1],[7,8,9,4],[1,8,7,5]]n=len(arr)flt=sum(arr,[])ind=list(range(n))+[i*n+(n-i-1)foriinrange(1,n-1)]+[n*(n-1)+jforjinrange(n)]print(*[flt[i]foriinind])

Output
4 5 6 8 3 8 1 8 7 5

Explanation:

  • flt = sum(arr, []) flattens the 2D matrix and range(n) top row indices.
  • Second list reverse diagonal indices and Last list bottom row indices.
  • Extract and print elements using computed indices.

Using Index-Based Traversal

This method uses nested loops to traverse each required position step by step first printing the top row, then the diagonal, and finally the bottom row in order.

Python
arr=[[4,5,6,8],[1,2,3,1],[7,8,9,4],[1,8,7,5]]n=len(arr[0])# Print first rowforjinrange(n):print(arr[0][j],end=" ")# Print opposite diagonalforiinrange(1,n-1):print(arr[i][n-i-1],end=" ")# Print last rowforjinrange(n):print(arr[n-1][j],end=" ")

Output
4 5 6 8 3 8 1 8 7 5

Explanation:

  • First for loop: arr[0][j] prints elements of the top row left to right.
  • Middle loop: for each row i from 1 to n-2, arr[i][n-i-1] computes the column index for the opposite diagonal and prints that element.
  • Last for loop: arr[n-1][j] prints all elements of the bottom row.

Please refer complete article onProgram to Print Matrix in Z form for more details!


Improve
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp