Largest Zigzag Sequence Problem

Given a square matrix MAT of dimensions N x N, define a 'zigzag' sequence as one that starts from the top row and ends at the bottom row, with the requirement that no two consecutive elements of the sequence are from the same column.

Your task is to determine the maximum possible sum of any zigzag sequence in this matrix.

Input:

Integer T, the number of test cases.
For each test case:
Integer N.
N lines each with N space-separated integers MAT[i][j].

Output:

For each test case, output the maximum sum of a zigzag sequence, each result on a new line.

Example:

Input:
T = 1
N = 3
MAT = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output:
15

Explanation: The sequence could be 3 (from row 1), 6 (from row 2), 9 (from row 3), resulting in a total sum of 18.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 100
  • 1 <= MAT[i][j] <= 1000
AnswerBot
7d

Find the maximum sum of a zigzag sequence in a square matrix.

  • Iterate through each row and column to find the maximum sum of zigzag sequence

  • Keep track of the maximum sum obtained so far

  • Consider alterna...read more

Help your peers!
Add answer anonymously...
Across The Globe Software Developer Intern 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