code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

Matrixinsnakepattern

import java.util.Arrays;

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class MatrixInSnakePattern {

  static int R = 4, C = 4;

  static void printSnake(int mat[][]) {
    for (int i = 0; i < 4; i++) {
      if (i % 2 != 0) {
        for (int j = 3; j >= 0; j--) {
          System.out.println(mat[i][j]);
        }
      } else {
        for (int j = 0; j < 4; j++) {
          System.out.println(mat[i][j]);
        }
      }
    }
  }

  public static void main(String[] args) {
    int arr[][] = { { 1, 2, 3, 4 },
        { 5, 6, 7, 8 },
        { 9, 10, 11, 12 },
        { 13, 14, 15, 16 } };
    printSnake(arr);
  }
}

Created 2021-12-26T02:36:36+00:00 · Edit