import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[][] arr = inputarray();
rowprint(arr);
}
public static int[][] inputarray(){
int M = sc.nextInt();
int N = sc.nextInt();
int[][] arr = new int[M][N];
for(int i = 0;i<M;i++) {
for(int j = 0;j<N;j++) {
arr[i][j] = sc.nextInt();
}
}
return arr;
}
public static void rowprint(int[][] arr) {
for(int i = 0;i<arr.length;i++) {
if(i%2==0) {
for(int j = 0;j<arr.length;j++) {
System.out.print(arr[i][j]+", β);
}
}
else {
for(int j = arr.length-1;j>=0;jβ) {
System.out.print(arr[i][j]+β, ");
}
}
}
System.out.print(βENDβ);
}
}
Whats wrong in this code??
Code not accepted
Hi @prateekad99,
It will give wrong answer for the input:
2 3
1 2 3
4 5 6
You need to add more conditions to your code. Right now you are only printing for n x n matrices.
It will give error ( Index out of bounds) for the input:
3 2
1 2
3 4
5 6
I hope Iβve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.