Rotate NxN array

my code is working on other ide but not here.

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner sc=new Scanner(System.in);
	int m=sc.nextInt();
	int n=sc.nextInt();
	int[][] arr=new int[m][n];
	int i,j;
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			arr[i][j]=sc.nextInt();

		}
	}
	for(j=n-1;j>=0;j--)
	{
		for(i=0;i<m;i++)
		{
			System.out.print(arr[i][j]+" ");
		}
		System.out.println();
	}
}

}

@aaayushi534_2b8dbe33159237a1 You have been told that array will be of size nn so you only need to take n input not m also. So take n input and take further nn inputs for array and change m to n everywhere and remove line int m=sc.nextInt();
Corrected code looks like this :
(Also remember you are getting write answer but you not only need to print the array you have to rotate the array in place.)

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	int[][] arr=new int[n][n];
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			arr[i][j]=sc.nextInt();

		}
	}
	for(j=n-1;j>=0;j--)
	{
		for(i=0;i<n;i++)
		{
			System.out.print(arr[i][j]+" ");
		}
		System.out.println();
	}
}
}

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.

1 Like