Largest zigzag sequence
Submissions: 3338   Accuracy:

38.38%

  Difficulty: Easy   Marks: 2
Associated Course(s):   Geeks Classes in Noida

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


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

to report an issue on this page.