# Write a python program to find the sum of a zig-zag pattern in a given matrix using the recursion

ADVERTISEMENTS

Write a __python program__ to find the sum of a zig-zag pattern in a given matrix using the recursion. There are you will learn how to find the sum of a zig-zag pattern in a given matrix using the recursion.

Take an example to find the sum of a zig-zag pattern through a __python program__:

```
# Write a python program to find the sum of a zig-zag
# pattern in a given matrix using the recursion
# This function will calculate
# the sum of the list elements
def zigZagSum(arru, i, j, n):
pass
# If we have reached bottom
if i == n - 1:
return arru[i][j]
# This will find the largest sum in the sequence
zzs = 0
for k in range (n):
if k != j:
zzs = max(zzs, zigZagSum(arru, i + 1, k, n))
return zzs + arru[i][j]
# This function will calculate
# the largest sum of elements
def largestZigZag(arru, n):
pass
res = 0
# This will consider all cells of top row as starting point
for j in range (n):
res = max(res, zigZagSum(arru, 0, j, n))
return res
# This is the driven calculation
# It's the size matrix dimensions
n = 4
arru = [
[43, 43, 47, 41],
[35, 36, 37, 38],
[29, 20, 21, 21],
[13, 14, 15, 16]
]
print ("The largest zigzag sum of the matrix is:", largestZigZag(arru, n))
```

**Output:**

`The largest zigzag sum of the matrix is: 130`

## Tags:

# zig zag sequence hackerrank solution

# largest sum zigzag sequence in a matrix

# given a matrix, the task is to print the boundary elements of the matrix and display their sum