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
4mo

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!
Select
Add answer anonymously...
Across The Globe Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits