Spiral Print Clockwise [codingblocks]

Take as input a 2-d array.Print the 2-D array in sprial form clockwise.


Input Format
Two integers M(row) and N(colomn) and further M * N integers(2-d array numbers).

Constraints
Both M and N are between 1 to 10.

Output Format
All M * N integers separated by commas with 'END' written in the end(as shown in example).

Sample Input
4 4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

Sample Output
11, 12, 13, 14, 24, 34, 44, 43, 42, 41, 31, 21, 22, 23, 33, 32, END

Explanation
For spiral level clockwise traversal, Go for first row-> last column ->last row -> first column and then do the same traversal for the remaining matrix.



Java Code:



/*
Amit Kumar
07-12-2020
IDE Code: https://ide.geeksforgeeks.org/CeeHFpOMwa
*/

package array;
import java.util.Scanner;

public class Array_SpiralPrint {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int row = scanner.nextInt();
        int col = scanner.nextInt();
        int[][] matrix2d = new int[row][col];

        for (int i=0; i<row; i++) {
            for (int j=0; j<col; j++) {
                matrix2d[i][j] = scanner.nextInt();
            }
        }

        printMatrix2D(matrix2d);
    }

    private static void printMatrix2D(int[][] matrix2d) {
        int left, right, top, bottom, count;
        left = 0;
        right = matrix2d[0].length - 1;
        top = 0;
        bottom = matrix2d.length - 1;
        count = (right + 1) * (bottom + 1);

        while (left <= right && top <= bottom) {

            //Print left to right ROW
            if (count > 0) {
                for(int i=left; i<=right; i++) {
                    System.out.print(matrix2d[top][i] + ", ");
                    count--;
                }
                top++;
            }

            //Print right top to bottom COLUMN
            if (count > 0) {
                for (int i=top; i<=bottom; i++) {
                    System.out.print(matrix2d[i][right] + ", ");
                    count--;
                }
                right--;
            }

            //Print right bottom to left ROW
            if (count > 0) {
                for (int i=right; i>=left; i--) {
                    System.out.print(matrix2d[bottom][i] + ", ");
                    count--;
                }
                bottom--;
            }

            //Print left bottom to top COLUMN
            if (count > 0) {
                for (int i=bottom; i>=top; i--) {
                    System.out.print(matrix2d[i][left] + ", ");
                    count--;
                }
                left++;
            }
        }
        System.out.print("END");
    }
}

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )