Largest sum Zigzag sequence in a matrix
Last Updated :
19 Sep, 2023
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
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:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int largestZigZagSumRec( int mat[][MAX], int i,
int j, int n)
{
if (i == n-1)
return mat[i][j];
int zzs = 0;
for ( int k=0; k<n; k++)
if (k != j)
zzs = max(zzs, largestZigZagSumRec(mat, i+1, k, n));
return zzs + mat[i][j];
}
int largestZigZag( int mat[][MAX], int n)
{
int res = 0;
for ( int j=0; j<n; j++)
res = max(res, largestZigZagSumRec(mat, 0, j, n));
return res;
}
int main()
{
int n = 3;
int mat[][MAX] = { {4, 2, 1},
{3, 9, 6},
{11, 3, 15}};
cout << "Largest zigzag sum: " << largestZigZag(mat, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
static int largestZigZagSumRec( int mat[][],
int i, int j, int n)
{
if (i == n- 1 )
return mat[i][j];
int zzs = 0 ;
for ( int k= 0 ; k<n; k++)
if (k != j)
zzs = Math.max(zzs,
largestZigZagSumRec(mat, i+ 1 , k, n));
return zzs + mat[i][j];
}
static int largestZigZag( int mat[][], int n)
{
int res = 0 ;
for ( int j= 0 ; j<n; j++)
res = Math.max(res,
largestZigZagSumRec(mat, 0 , j, n));
return res;
}
public static void main (String[] args)
{
int n = 3 ;
int mat[][] = { { 4 , 2 , 1 },
{ 3 , 9 , 6 },
{ 11 , 3 , 15 } };
System.out.println( "Largest zigzag sum: "
+ largestZigZag(mat, n));
}
}
|
Python 3
MAX = 100
def largestZigZagSumRec( mat, i, j, n):
if (i = = n - 1 ):
return mat[i][j]
zzs = 0
for k in range (n):
if (k ! = j):
zzs = max (zzs, largestZigZagSumRec(mat, i + 1 , k, n))
return zzs + mat[i][j]
def largestZigZag(mat, n):
res = 0
for j in range (n):
res = max (res, largestZigZagSumRec(mat, 0 , j, n))
return res
if __name__ = = "__main__" :
n = 3
mat = [ [ 4 , 2 , 1 ],
[ 3 , 9 , 6 ],
[ 11 , 3 , 15 ]]
print ( "Largest zigzag sum: " ,
largestZigZag(mat, n))
|
C#
using System;
class GFG {
static int largestZigZagSumRec( int [,]mat,
int i, int j, int n)
{
if (i == n-1)
return mat[i,j];
int zzs = 0;
for ( int k = 0; k < n; k++)
if (k != j)
zzs = Math.Max(zzs, largestZigZagSumRec(mat,
i + 1, k, n));
return zzs + mat[i,j];
}
static int largestZigZag( int [,]mat, int n)
{
int res = 0;
for ( int j = 0; j < n; j++)
res = Math.Max(res,
largestZigZagSumRec(mat, 0, j, n));
return res;
}
public static void Main ()
{
int n = 3;
int [,]mat = {{4, 2, 1},
{3, 9, 6},
{11, 3, 15}};
Console.WriteLine( "Largest zigzag sum: "
+ largestZigZag(mat, n));
}
}
|
Javascript
<script>
let MAX = 100;
function largestZigZagSumRec(mat,i,j,n)
{
if (i == n-1)
return mat[i][j];
let zzs = 0;
for (let k=0; k<n; k++)
if (k != j)
zzs = Math.max(zzs,
largestZigZagSumRec(mat, i+1, k, n));
return zzs + mat[i][j];
}
function largestZigZag(mat,n)
{
let res = 0;
for (let j=0; j<n; j++)
res = Math.max(res,
largestZigZagSumRec(mat, 0, j, n));
return res;
}
let n = 3;
let mat = [ [4, 2, 1],
[3, 9, 6],
[11, 3, 15]];
document.write( "Largest zigzag sum: " +
largestZigZag(mat, n))
</script>
|
PHP
<?php
$MAX = 100;
function largestZigZagSumRec( $mat , $i ,
$j , $n )
{
if ( $i == $n - 1)
return $mat [ $i ][ $j ];
$zzs = 0;
for ( $k = 0; $k < $n ; $k ++)
if ( $k != $j )
$zzs = max( $zzs , largestZigZagSumRec( $mat ,
$i + 1, $k , $n ));
return $zzs + $mat [ $i ][ $j ];
}
function largestZigZag( $mat , $n )
{
$res = 0;
for ( $j = 0; $j < $n ; $j ++)
$res = max( $res , largestZigZagSumRec(
$mat , 0, $j , $n ));
return $res ;
}
$n = 3;
$mat = array ( array (4, 2, 1),
array (3, 9, 6),
array (11, 3, 15));
echo "Largest zigzag sum: " , largestZigZag( $mat , $n );
?>
|
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:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int dp[MAX][MAX];
int largestZigZagSumRec( int mat[][MAX], int i,
int j, int n)
{
if (dp[i][j] != -1)
return dp[i][j];
if (i == n-1)
return (dp[i][j] = mat[i][j]);
int zzs = 0;
for ( int k=0; k<n; k++)
if (k != j)
zzs = max(zzs, largestZigZagSumRec(mat, i+1, k, n));
return (dp[i][j] = (zzs + mat[i][j]));
}
int largestZigZag( int mat[][MAX], int n)
{
memset (dp, -1, sizeof (dp));
int res = 0;
for ( int j=0; j<n; j++)
res = max(res, largestZigZagSumRec(mat, 0, j, n));
return res;
}
int main()
{
int n = 3;
int mat[][MAX] = { {4, 2, 1},
{3, 9, 6},
{11, 3, 15}};
cout << "Largest zigzag sum: " << largestZigZag(mat, n);
return 0;
}
|
Java
class GFG
{
static int MAX = 100 ;
static int [][]dp = new int [MAX][MAX];
static int largestZigZagSumRec( int mat[][], int i,
int j, int n)
{
if (dp[i][j] != - 1 )
return dp[i][j];
if (i == n - 1 )
return (dp[i][j] = mat[i][j]);
int zzs = 0 ;
for ( int k = 0 ; k < n; k++)
if (k != j)
zzs = Math.max(zzs, largestZigZagSumRec(mat,
i + 1 , k, n));
return (dp[i][j] = (zzs + mat[i][j]));
}
static int largestZigZag( int mat[][], int n)
{
for ( int i = 0 ; i < MAX; i++)
for ( int k = 0 ; k < MAX; k++)
dp[i][k] = - 1 ;
int res = 0 ;
for ( int j = 0 ; j < n; j++)
res = Math.max(res, largestZigZagSumRec(mat,
0 , j, n));
return res;
}
public static void main(String[] args)
{
int n = 3 ;
int mat[][] = { { 4 , 2 , 1 },
{ 3 , 9 , 6 },
{ 11 , 3 , 15 }};
System.out.print( "Largest zigzag sum: " +
largestZigZag(mat, n));
}
}
|
Python3
MAX = 100 ;
dp = [[ 0 for i in range ( MAX )] for j in range ( MAX )]
def largestZigZagSumRec(mat, i, j, n):
if (dp[i][j] ! = - 1 ):
return dp[i][j];
if (i = = n - 1 ):
dp[i][j] = mat[i][j];
return (dp[i][j]);
zzs = 0 ;
for k in range (n):
if (k ! = j):
zzs = max (zzs, largestZigZagSumRec(mat,
i + 1 , k, n));
dp[i][j] = (zzs + mat[i][j]);
return (dp[i][j]);
def largestZigZag(mat, n):
for i in range ( MAX ):
for k in range ( MAX ):
dp[i][k] = - 1 ;
res = 0 ;
for j in range (n):
res = max (res, largestZigZagSumRec(mat, 0 , j, n));
return res;
if __name__ = = '__main__' :
n = 3 ;
mat = [[ 4 , 2 , 1 ], [ 3 , 9 , 6 ], [ 11 , 3 , 15 ]];
print ( "Largest zigzag sum: " , largestZigZag(mat, n));
|
C#
using System;
class GFG
{
static int MAX = 100;
static int [,]dp = new int [MAX, MAX];
static int largestZigZagSumRec( int [,]mat, int i,
int j, int n)
{
if (dp[i, j] != -1)
return dp[i, j];
if (i == n - 1)
return (dp[i, j] = mat[i, j]);
int zzs = 0;
for ( int k = 0; k < n; k++)
if (k != j)
zzs = Math.Max(zzs, largestZigZagSumRec(mat,
i + 1, k, n));
return (dp[i, j] = (zzs + mat[i, j]));
}
static int largestZigZag( int [,]mat, int n)
{
for ( int i = 0; i < MAX; i++)
for ( int k = 0; k < MAX; k++)
dp[i, k] = -1;
int res = 0;
for ( int j = 0; j < n; j++)
res = Math.Max(res, largestZigZagSumRec(mat,
0, j, n));
return res;
}
public static void Main(String[] args)
{
int n = 3;
int [,]mat = { {4, 2, 1},
{3, 9, 6},
{11, 3, 15}};
Console.Write( "Largest zigzag sum: " +
largestZigZag(mat, n));
}
}
|
Javascript
<script>
let MAX = 100;
let dp= new Array(MAX);
function largestZigZagSumRec(mat,i,j,n)
{
if (dp[i][j] != -1)
return dp[i][j];
if (i == n - 1)
return (dp[i][j] = mat[i][j]);
let zzs = 0;
for (let k = 0; k < n; k++)
if (k != j)
zzs = Math.max(zzs, largestZigZagSumRec(mat,
i + 1, k, n));
return (dp[i][j] = (zzs + mat[i][j]));
}
function largestZigZag(mat,n)
{
for (let i = 0; i < MAX; i++)
{
dp[i]= new Array(MAX);
for (let k = 0; k < MAX; k++)
dp[i][k] = -1;
}
let res = 0;
for (let j = 0; j < n; j++)
res = Math.max(res, largestZigZagSumRec(mat,
0, j, n));
return res;
}
let n = 3;
let mat=[[4, 2, 1],[3, 9, 6],[11, 3, 15]];
document.write( "Largest zigzag sum: " +
largestZigZag(mat, n));
</script>
|
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 :
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int dp[MAX][MAX];
int largestZigZag( int mat[][MAX], int n)
{
memset (dp, 0, sizeof (dp));
for ( int j = 0; j < n; j++) {
dp[n - 1][j] = mat[n - 1][j];
}
for ( int i = n - 2; i >= 0; i--) {
for ( int j = 0; j < n; j++) {
for ( int k = 0; k < n; k++) {
if (k != j) {
dp[i][j] = max(dp[i][j], dp[i + 1][k]);
}
}
dp[i][j] += mat[i][j];
}
}
int res = 0;
for ( int j = 0; j < n; j++) {
res = max(res, dp[0][j]);
}
return res;
}
int main()
{
int n = 3;
int mat[][MAX]
= { { 4, 2, 1 }, { 3, 9, 6 }, { 11, 3, 15 } };
cout << "Largest zigzag sum: " << largestZigZag(mat, n);
return 0;
}
|
Java
public class LargestZigZag {
private static final int MAX = 100 ;
public static int largestZigZag( int [][] mat, int n) {
int [][] dp = new int [MAX][MAX];
for ( int j = 0 ; j < n; j++) {
dp[n - 1 ][j] = mat[n - 1 ][j];
}
for ( int i = n - 2 ; i >= 0 ; i--) {
for ( int j = 0 ; j < n; j++) {
for ( int k = 0 ; k < n; k++) {
if (k != j) {
dp[i][j] = Math.max(dp[i][j], dp[i + 1 ][k]);
}
}
dp[i][j] += mat[i][j];
}
}
int res = 0 ;
for ( int j = 0 ; j < n; j++) {
res = Math.max(res, dp[ 0 ][j]);
}
return res;
}
public static void main(String[] args) {
int n = 3 ;
int [][] mat = {{ 4 , 2 , 1 }, { 3 , 9 , 6 }, { 11 , 3 , 15 }};
System.out.println( "Largest zigzag sum: " + largestZigZag(mat, n));
}
}
|
Python3
MAX = 100
def largestZigZag(mat, n):
dp = [[ 0 ] * MAX for _ in range ( MAX )]
for j in range (n):
dp[n - 1 ][j] = mat[n - 1 ][j]
for i in range (n - 2 , - 1 , - 1 ):
for j in range (n):
for k in range (n):
if k ! = j:
dp[i][j] = max (dp[i][j], dp[i + 1 ][k])
dp[i][j] + = mat[i][j]
res = 0
for j in range (n):
res = max (res, dp[ 0 ][j])
return res
if __name__ = = "__main__" :
n = 3
mat = [[ 4 , 2 , 1 ], [ 3 , 9 , 6 ], [ 11 , 3 , 15 ]]
print ( "Largest zigzag sum:" , largestZigZag(mat, n))
|
C#
using System;
class GFG {
const int MAX = 100;
static int [, ] dp = new int [MAX, MAX];
static int LargestZigZag( int [, ] mat, int n)
{
Array.Clear(dp, 0, dp.Length);
for ( int j = 0; j < n; j++) {
dp[n - 1, j] = mat[n - 1, j];
}
for ( int i = n - 2; i >= 0; i--) {
for ( int j = 0; j < n; j++) {
for ( int k = 0; k < n; k++) {
if (k != j) {
dp[i, j] = Math.Max(dp[i, j],
dp[i + 1, k]);
}
}
dp[i, j] += mat[i, j];
}
}
int res = 0;
for ( int j = 0; j < n; j++) {
res = Math.Max(res, dp[0, j]);
}
return res;
}
static void Main()
{
int n = 3;
int [, ] mat
= { { 4, 2, 1 }, { 3, 9, 6 }, { 11, 3, 15 } };
Console.WriteLine( "Largest zigzag sum: "
+ LargestZigZag(mat, n));
}
}
|
Javascript
const MAX = 100;
function largestZigZag(mat, n) {
const dp = new Array(n).fill(0).map(() => new Array(n).fill(0));
for (let j = 0; j < n; j++) {
dp[n - 1][j] = mat[n - 1][j];
}
for (let i = n - 2; i >= 0; i--) {
for (let j = 0; j < n; j++) {
for (let k = 0; k < n; k++) {
if (k !== j) {
dp[i][j] = Math.max(dp[i][j], dp[i + 1][k]);
}
}
dp[i][j] += mat[i][j];
}
}
let res = 0;
for (let j = 0; j < n; j++) {
res = Math.max(res, dp[0][j]);
}
return res;
}
const n = 3;
const mat = [
[4, 2, 1],
[3, 9, 6],
[11, 3, 15]
];
console.log( "Largest zigzag sum:" , largestZigZag(mat, n));
|
Output
Largest zigzag sum: 28
Time Complexity: O(Max*MAX)
Auxiliary Space: O(Max*MAX)
Share your thoughts in the comments
Please Login to comment...