Wave Print Column Wise [codingblocks]

 Take as input a two-d array. Wave print it column-wise.


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 seperated by commas with 'END' wriiten 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, 21, 31, 41, 42, 32, 22, 12, 13, 23, 33, 43, 44, 34, 24, 14, END


Java Code:



/*
Amit Kumar
04-12-2020
IDE Code: https://ide.geeksforgeeks.org/FKyP9Y8thu
*/

package array;
import java.util.Scanner;

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

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

        wavePrintColomnWise(twoDarray, row, col);
    }

    private static void wavePrintColomnWise(int[][] twoDarray, int ROW, int COL) {
        for (int i=0; i<COL; i++) {
            for (int j=0; j<ROW; j++) {
                if (i % 2 == 0) {
                    System.out.print(twoDarray[j][i] + ", ");
                } else {
                    System.out.print(twoDarray[ROW-1-j][i] + ", ");
                }
            }
        }
        System.out.println("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 )