Diagonal Order Problem Statement

Given a 2D matrix, output all elements of the matrix in diagonal order, starting from the top-left element and proceeding diagonally.

Input:

Line 1: Two integers, R and C, denoting the number of rows and columns in the matrix.
Line 2: R * C integers separated by spaces, representing the elements of the matrix.

Output:

The elements of the matrix printed in diagonal order.

Example:

Input:
R = 3, C = 3
matrix = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
1 2 4 7 5 3 6 8 9

Constraints:

  • 1 <= R <= 103
  • 1 <= C <= 103

Note:

The diagonals are traversed alternately from bottom-left to top-right and top-right to bottom-left. Ensure that your solution efficiently handles large matrices and takes advantage of the matrix properties.

Be the first one to answer
Add answer anonymously...
ZS Business Technology Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter