Largest zigzag sequence
Submissions: 4301   Accuracy:

40.3%

  Difficulty: Easy   Marks: 2
Associated Course(s):   Sudo Placement 2019

Given a matrix of size n x n, the task is to find sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence cannot belong to same column.

Examples:

Input : M[][] =   3  1  2
                  4  8  5
                  6  9  7
Output : 18
Zigzag sequence is: 3->8->7
Another such sequence is 2->4->7

Input : M[][] =   4  2  1
                  3  9  6
                  11  3 15
Output : 28


Input:

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the matrix. Then in the next line are N*N space separated values of the matrix.

Output:
For each test case in a new line print the required max sum.

Constraints:
1<=T<=100
1<=N<=100
1<=M[][]<=1000

Example:
Input:

2
3
3  1  2  4  8  5 6  9  7
3
4 2 1 3 9 6 11 3 15
Output:
18
28

** For More Input/Output Examples Use 'Expected Output' option **

Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.