VOOZH about

URL: https://www.geeksforgeeks.org/dsa/largest-sum-zig-zag-sequence-in-a-matrix/

⇱ Largest sum Zigzag sequence in a matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Largest sum Zigzag sequence in a matrix

Last Updated : 23 Jul, 2025

Given a matrix of size n x n, find the 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 the same column. 

Examples:

Input : mat[][] = 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 : mat[][] = 4 2 1
3 9 6
11 3 15
Output : 28
Recommended Practice

This problem has an Optimal Substructure

Maximum Zigzag sum starting from arr[i][j] to a 
bottom cell can be written as :
zzs(i, j) = arr[i][j] + max(zzs(i+1, k)),
where k = 0, 1, 2 and k != j
zzs(i, j) = arr[i][j], if i = n-1
We have to find the largest among all as
Result = zzs(0, j) where 0 <= j < n

Implementation:


Output
Largest zigzag sum: 28


Time complexity: O(n^2)

We need to traverse the entire matrix to find the largest zigzag sum. The outer loop iterates over all the elements of the first row, and the inner loop calls the recursive function for each element in the first row. The recursive function then traverses the matrix in a zigzag fashion and finds the largest sum. Therefore the time complexity of this algorithm is O(n^2), where n represents the number of rows in the matrix.

Space complexity: O(n)

We need to store the intermediate results of the recursive calls in order to avoid recomputing them. Therefore, the space complexity of this algorithm is O(n), where n represents the number of rows in the matrix.

Overlapping Subproblems
Considering the above implementation, for a matrix mat[][] of size 3 x 3, to find the zigzag sum(zzs) for an element mat(i,j), the following recursion tree is formed.

Recursion tree for cell (0, 0)
zzs(0,0)
/ \
zzs(1,1) zzs(1,2)
/ \ / \
zzs(2,0) zzs(2,2) zzs(2,0) zzs(2,1)
Recursion tree for cell (0, 1)
zzs(0,1)
/ \
zzs(1,0) zzs(1,2)
/ \ / \
zzs(2,1) zzs(2,2) zzs(2,0) zzs(2,1)
Recursion tree for cell (0, 2)
zzs(0,2)
/ \
zzs(1,0) zzs(1,1)
/ \ / \
zzs(2,1) zzs(2,2) zzs(2,0) zzs(2,2)

We can see that there are many subproblems that are solved again and again. So this problem has Overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memoization or Tabulation. Following is a tabulated implementation for the LIS problem.

Implementation:


Output
Largest zigzag sum: 28

Time Complexity: O(Max*MAX)
Auxiliary Space: O(Max*MAX)


Efficient approach: Using DP Tabulation method ( Iterative approach )

The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memoization(top-down) because memoization method needs extra stack space of recursion calls.

Steps to solve this problem :

  • Create a dp matrix to store the solution of the subproblems and initialize it with 0.
  • Initialize base cases by updating values of last row values of dp.  
  • Now Iterate over subproblems to get the value of current problem form previous computation of subproblems stored in dp
  • Initialize a variable res to store the final answer and update it by traversing over dp. 
  • At last return and print res.

Implementation :


Output
Largest zigzag sum: 28

Time Complexity: O(Max*MAX)
Auxiliary Space: O(Max*MAX)

Comment
Article Tags: