Print Like a Wave
Send Feedback
Code Genius is an all-in-one platform for bloggers, YouTubers, job seekers, and tech enthusiasts. It offers resources on content creation, job search, cooling pads, latest tech info, smartphones, headphones, laptops, electronics, and online earning.
Print Like a Wave
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
Second line onwards, the next 'N' lines or rows represent the ith row values.
Each of the ith row constitutes 'M' column values separated by a single space.
For each test case, print the elements of the two-dimensional array/list in the sine wave order in a single line, separated by a single space.
Output for every test case will be printed in a seperate line.
1 <= t <= 10^2
0 <= N <= 10^3
0 <= M <= 10^3
Time Limit: 1sec
1
3 4
1 2 3 4
5 6 7 8
9 10 11 12
1 5 9 10 6 2 3 7 11 12 8 4
2
5 3
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
3 3
10 20 30
40 50 60
70 80 90
1 4 7 10 13 14 11 8 5 2 3 6 9 12 15
10 40 70 80 50 20 30 60 90
code for this
public class Solution {
public static void wavePrint(int mat[][]){ //Your code goes here if (mat.length==0) System.exit(0); for (int j = 0; j < mat[0].length ; j++) { for (int i = 0; i < mat.length; i++) { System.out.print(mat[i][j]+" "); } j++; if (j==mat[0].length) System.exit(0); for (int i = mat.length-1; i >= 0; i--) { System.out.print(mat[i][j]+" "); } } }
}
Comments
Post a Comment