Geeksforgeeks

Timer is Running

Associated Course(s):
Geeks Classes
Geeks Classes- Algorithms

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

18

28

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.

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

ykaushik | 261 |

_saswata | 232 |

kirtidee18 | 1057 |

ykaushik | 733 |

aggarwalvatsal5 | 709 |

pmr_3689 | 570 |

harbilassingh | 530 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4823 |

Login to report an issue on this page.