Given a square matrix mat[][] of size n × n, print all its elements in a snake pattern. The first row is printed from left to right, the second row from right to left, and this pattern continues for all rows.
Examples :
Input: mat[][] = [ [10, 20, 30, 40], [15, 25, 35, 45], [27, 29, 37, 48], [32, 33, 39, 50]]; Output: [10,20,30,40,45,35,25,15,27,29,37,48,50,39,33,32] Explanation: The first row is printed from left to right, the second row from right to left, and the same pattern continues for the remaining rows.
Input: mat[][] = [[1, 2], [3, 4]] Output: [1, 2, 4, 3] Explanation: The first row is printed from left to right as 1 2, while the second row is printed from right to left as 4 3.
Traverse Rows Alternately - O(n^2) Time and O(1) Auxiliary Space
The idea is to traverse the matrix row by row, changing the direction after each row. For even-indexed rows, print elements from left to right, and for odd-indexed rows, print elements from right to left.
Row 0 is even, so print from left to right: 1 2 3.
Row 1 is odd, so print from right to left: 6 5 4.
Row 2 is even, so print from left to right: 7 8 9.
Therefore, the elements are printed as:[1, 2, 3, 6, 5, 4, 7, 8, 9]
C++
#include<iostream>#include<vector>usingnamespacestd;vector<int>snakePattern(vector<vector<int>>matrix){intn=matrix.size();vector<int>res;// Traverse over the rows.for(inti=0;i<n;i++){// If current row is even, add elements// from left to right.if(i%2==0){for(intj=0;j<n;j++)res.push_back(matrix[i][j]);}// If current row is odd, add elements// from right to left.else{for(intj=n-1;j>=0;j--)res.push_back(matrix[i][j]);}}returnres;}intmain(){vector<vector<int>>matrix={{1,2,3},{4,5,6},{7,8,9}};vector<int>res=snakePattern(matrix);cout<<"[";for(inti=0;i<res.size();i++){cout<<res[i];if(i!=res.size()-1)cout<<", ";}cout<<"]";return0;}
C
#include<stdio.h>#include<stdlib.h>int*snakePattern(intn,intmatrix[n][n]){int*res=(int*)malloc(n*n*sizeof(int));intk=0;// Traverse over the rows.for(inti=0;i<n;i++){// If current row is even, add elements// from left to right.if(i%2==0){for(intj=0;j<n;j++)res[k++]=matrix[i][j];}// If current row is odd, add elements// from right to left.else{for(intj=n-1;j>=0;j--)res[k++]=matrix[i][j];}}returnres;}intmain(){intn=3;intmatrix[3][3]={{1,2,3},{4,5,6},{7,8,9}};int*res=snakePattern(n,matrix);printf("[");for(inti=0;i<n*n;i++){printf("%d",res[i]);if(i!=n*n-1)printf(", ");}printf("]");free(res);return0;}
Java
importjava.util.ArrayList;classGFG{staticArrayList<Integer>snakePattern(int[][]matrix){intn=matrix.length;ArrayList<Integer>res=newArrayList<>();// Traverse over the rows.for(inti=0;i<n;i++){// If current row is even, add elements// from left to right.if(i%2==0){for(intj=0;j<n;j++)res.add(matrix[i][j]);}// If current row is odd, add elements// from right to left.else{for(intj=n-1;j>=0;j--)res.add(matrix[i][j]);}}returnres;}publicstaticvoidmain(String[]args){int[][]matrix={{1,2,3},{4,5,6},{7,8,9}};ArrayList<Integer>res=snakePattern(matrix);System.out.println(res);}}
Python
defsnakePattern(matrix):n=len(matrix)res=[]# Traverse over the rows.foriinrange(n):# If current row is even, add elements# from left to right.ifi%2==0:forjinrange(n):res.append(matrix[i][j])# If current row is odd, add elements# from right to left.else:forjinrange(n-1,-1,-1):res.append(matrix[i][j])returnresif__name__=="__main__":matrix=[[1,2,3],[4,5,6],[7,8,9]]res=snakePattern(matrix)print(res)
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticList<int>snakePattern(int[][]matrix){intn=matrix.Length;List<int>res=newList<int>();// Traverse over the rows.for(inti=0;i<n;i++){// If current row is even, add elements// from left to right.if(i%2==0){for(intj=0;j<n;j++)res.Add(matrix[i][j]);}// If current row is odd, add elements// from right to left.else{for(intj=n-1;j>=0;j--)res.Add(matrix[i][j]);}}returnres;}staticvoidMain(){int[][]matrix={newint[]{1,2,3},newint[]{4,5,6},newint[]{7,8,9}};List<int>res=snakePattern(matrix);Console.WriteLine("["+string.Join(", ",res)+"]");}}
JavaScript
functionsnakePattern(matrix){letn=matrix.length;letres=[];// Traverse over the rows.for(leti=0;i<n;i++){// If current row is even, add elements// from left to right.if(i%2==0){for(letj=0;j<n;j++)res.push(matrix[i][j]);}// If current row is odd, add elements// from right to left.else{for(letj=n-1;j>=0;j--)res.push(matrix[i][j]);}}returnres;}// Driver codeletmatrix=[[1,2,3],[4,5,6],[7,8,9]];letres=snakePattern(matrix);console.log("["+res.join(", ")+"]");