Skip to main content

Column Wise Traversal

 Column Wise Traversal

Send Feedback

Given a matrix, ‘A’ of size ‘N’ * ‘M’, you must traverse the matrix column-wise.

You must return an integer array of size ‘N’ * ‘M’ denoting the column-wise traversal of the matrix.

For example:

Input :
A = [ [1, 2, 3], [4, 5, 6] ]

Output :
1 4 2 5 3 6

Explanation: 
For the given matrix, the first column is [1, 4], the second is [2, 5] and the third is [3, 6].
For column-wise traversal, you must traverse the first column, the second column, and then the third.
Input Format:
First-line contains 'T,' denoting the number of Test cases.
For each Test case:
The first line contains two integers, ‘N' and ‘M’.
The following ‘N’ lines have ‘M’ integers each, denoting the matrix ‘A’.
Output Format:-
You must return the array with elements in order of column-wise traversal.
Note:-
You don’t need to print anything. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N * M <= 10^5
1 <= A[ i ][ j ] <= 10^9
Time Limit: 1 sec
Sample Input 1 :
2
2 2 
4 3
2 1
5 1
1 
2 
3 
4 
5
Sample Output 1 :
4 2 3 1
1 2 3 4 5 
Explanation Of Sample Input 1 :
For test case one:
Input :
A = [ [4, 3], [2, 1] ]
Output :
4 2 3 1
Explanation: For the given matrix, the first column is [4, 2], and the second is [3, 1].
For column-wise traversal, you need to traverse the first column and then the second column.

For test case two:
Input :
A = [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ] ]
Output :
1 2 3 4 5
Explanation: For the given matrix, the first column is [1, 2, 3, 4, 5].
For column-wise traversal, you need to traverse the first column.
Sample Input 2 :
2
1 1 
4
1 5
1 2 3 4 5
Sample Output 2 :
4
1 2 3 4 5 
code for this
public class Solution {
    public static int[] printColWise(int[][] a) {
        int numRows = a.length;
        int numCols = a[0].length;
        int[] result = new int[numRows * numCols];
        int index = 0;

        for (int j = 0; j < numCols; j++) {
            for (int i = 0; i < numRows; i++) {
                result[index++] = a[i][j];
            }
        }

        return result;
    }

    public static void main(String[] args) {
        // Sample input
        int[][] matrix1 = {{4, 3}, {2, 1}};
        int[][] matrix2 = {{1}, {2}, {3}, {4}, {5}};

        // Call the printColWise function for each test case
        int[] result1 = printColWise(matrix1);
        int[] result2 = printColWise(matrix2);

        // Print the results
        for (int num : result1) {
            System.out.print(num + " ");
        }
        System.out.println();

        for (int num : result2) {
            System.out.print(num + " ");
        }
    }
}

Comments

Popular posts from this blog

Add Image To Blog Page

  Add Image To Blog Page Send Feedback Your blog now looks better and seems to be readable. After knowing how to add an image, you will now do the same in your blog. So let's add all the images required to your blog. Images to be added are provided at the end of this problem. Remember to add appropriate alternate text as well. Some text also need to be added to the blog, so add it as well. For where to insert them, look at the expected look of the page and add them to their appropriate positions, as shown below- The required images are - code for this question  <!DOCTYPE html> <html>     <head>          <img src = "https://ninjasfiles.s3.amazonaws.com/0000000000001394.png">         <p>blog codingninjas.in</p>         <p>blog Coding Ninja Offical Blog</p>         <img src = "https://ninjasfiles.s3.amazonaws.com/0000000000001395.png"...

Code: Form Input Types

  Code: Form Input Types Send Feedback For now, we have created a simple form. But there is a problem that the inputs can take any value other than what they are meant to be. So, we need to change the type of the input element for proper functioning. Your task is to change the type of input according to the meaning of each of them. The meaning of each input element is provided below - 1. Comment - to provide a comment for the blog 2. Name - the name of the person who wants to comment on the blog. 3. Email - to provide the email id of the person. 4. Website - to provide the link to a website that belongs to the above said person. The expected output is - code for this  <!DOCTYPE html> <html>     <head>         <title>Best Coding Practices</title>     </head>     <body>         <div>             <header>       ...

Add View Section to Blog

  Add View Section to Blog Send Feedback Let's add a view section ( using a div tag ) to our blog, which represents the number of visits to our blog. Although it would be static for now, we can change it to dynamic later. So, we will use some text formatting tags to add the views section. The instructions are provided below. Note: The text formatting tags are always inline. Instructions to follow - 1. Add a separate view section (using a div tag ) inside the article in the container (div tag )of the first image of the article. 2. Now, add 2 span's with content as: 'Views:' and '1,137'. Make these span's bold using the appropriate text formatting tag. Expected Blog page is - <!DOCTYPE html> <html>     <head>         <title>Best Coding Practices</title>     </head>     <body>         <div>             <header>       ...