Sunday, November 17, 2024
Google search engine
HomeLanguagesPython Program for Print matrix in zag-zag fashion

Python Program for Print matrix in zag-zag fashion

Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. 
 

matrix_zag-zag

Example: 
 

Input: 
1 2 3
4 5 6
7 8 9
Output: 
1 2 4 7 5 3 6 8 9

 

Method 1:
Approach of Python3 code 
This approach is simple. While travelling the matrix in the usual fashion, on basis of parity of the sum of the indices of the element, add that particular element to the list either at the beginning or at the end if sum of i and j is either even or odd respectively. Print the solution list as it is. 
 

Python3




# Program to print matrix in Zig-zag pattern
 
matrix = [
    [1, 2, 3, ],
    [4, 5, 6],
    [7, 8, 9],
]
rows = 3
columns = 3
 
solution = [[] for i in range(rows+columns-1)]
 
for i in range(rows):
    for j in range(columns):
        sum = i+j
        if(sum % 2 == 0):
 
            # add at beginning
            solution[sum].insert(0, matrix[i][j])
        else:
 
            # add at end of the list
            solution[sum].append(matrix[i][j])
 
 
# print the solution as it as
for i in solution:
    for j in i:
        print(j, end=" ")


Output

1 2 4 7 5 3 6 8 9 

Time complexity: O(n*m) for a given matrix of order n*m

Auxiliary Space: O(n*m)

Method 2: Using While loop
 

Python3




# Program to print matrix in Zig-zag pattern
 
 
def findOrder(matrix):
    rows = 3
    columns = 3
    result = [0]*(rows*columns)
    result[0] = matrix[0][0]
    k = 1
    i = j = 0
    while(k < rows*columns):
        while i >= 1 and j < rows-1:
            i -= 1
            j += 1
            result[k] = matrix[i][j]
            k += 1
        if j < rows-1:
            j += 1
            result[k] = matrix[i][j]
            k += 1
        elif i < columns-1:
            i += 1
            result[k] = matrix[i][j]
            k += 1
        while i < columns-1 and j >= 1:
            i += 1
            j -= 1
            result[k] = matrix[i][j]
            k += 1
        if i < columns-1:
            i += 1
            result[k] = matrix[i][j]
            k += 1
        elif j < rows-1:
            j += 1
            result[k] = matrix[i][j]
            k += 1
    return result
 
 
matrix = [
    [1, 2, 3, ],
    [4, 5, 6],
    [7, 8, 9],
]
rows = 3
columns = 3
result = findOrder(matrix)
for num in result:
    print(num, end=' ')


Output

1 2 4 7 5 3 6 8 9 

Time Complexity: O(rows*columns),The time complexity of the algorithm is O(rows*columns) since we are iterating through the matrix of size rows*columns. 
Space Complexity: O(rows*columns),The space complexity of the algorithm is O(rows*columns) since we are creating an array of size rows*columns to store the result.

Please refer complete article on Print matrix in zag-zag fashion for more details!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments