package com.interview.multiarray;

/**
 * Date 07/31/2016
 * @author Tushar Roy
 *
 * You are given an n x n 2D matrix representing an image.
 * Rotate the image by 90 degrees (clockwise).
 *
 * https://leetcode.com/problems/rotate-image/
 */
public class RotateImage {

	public void rotate(int[][] matrix) {
		int length = matrix.length-1;
		int j=0;
		while(j < matrix.length/2){
			for(int i=j; i < length-j; i++){
				int temp = matrix[j][i];
				matrix[j][i] = matrix[length-i][j];
				matrix[length-i][j] = matrix[length-j][length-i];
				matrix[length-j][length-i] = matrix[i][length-j];
				matrix[i][length-j] = temp;
			}
			j++;
		}
	}

	private void print(int arr[][]){
		for(int i=0; i < arr.length; i++){
			for(int j=0; j < arr.length; j++){
				System.out.print(arr[i][j] + " ");
			}
			System.out.print("\n");
		}
	}

	public static void main(String args[]){

		int matrix[][] = {{1,2,3,4,20},{5,6,7,8,30},{9,10,11,12,40},{13,14,15,16,50},{21,22,23,24,25}};
		RotateImage ti = new RotateImage();
		ti.rotate(matrix);
		ti.print(matrix);
	}
}

@@ -264,17 +264,20 @@
 mage %7B%0A%0A
-%09
+    
 public v
@@ -305,18 +305,24 @@
 trix) %7B%0A
-%09%09
+        
 int leng
@@ -343,18 +343,24 @@
 ngth-1;%0A
-%09%09
+        
 int j=0;
@@ -360,18 +360,24 @@
 nt j=0;%0A
-%09%09
+        
 while(j 
@@ -396,19 +396,28 @@
 gth/2)%7B%0A
-%09%09%09
+            
 for(int 
@@ -441,20 +441,32 @@
 ; i++)%7B%0A
-%09%09%09%09
+                
 int temp
@@ -478,28 +478,40 @@
 trix%5Bj%5D%5Bi%5D;%0A
-%09%09%09%09
+                
 matrix%5Bj%5D%5Bi%5D
@@ -530,28 +530,40 @@
 ngth-i%5D%5Bj%5D;%0A
-%09%09%09%09
+                
 matrix%5Blengt
@@ -600,20 +600,32 @@
 gth-i%5D;%0A
-%09%09%09%09
+                
 matrix%5Bl
@@ -666,20 +666,32 @@
 gth-j%5D;%0A
-%09%09%09%09
+                
 matrix%5Bi
@@ -714,30 +714,60 @@
 mp;%0A
-%09%09%09%7D%0A%09%09%09j++;%0A%09%09%7D%0A%09%7D%0A%0A%09
+            %7D%0A            j++;%0A        %7D%0A    %7D%0A%0A    
 priv
@@ -795,18 +795,24 @@
 r%5B%5D%5B%5D)%7B%0A
-%09%09
+        
 for(int 
@@ -838,19 +838,28 @@
 ; i++)%7B%0A
-%09%09%09
+            
 for(int 
@@ -889,12 +889,24 @@
 +)%7B%0A
-%09%09%09%09
+                
 Syst
@@ -940,16 +940,34 @@
 %22);%0A
-%09%09%09%7D%0A%09%09%09
+            %7D%0A            
 Syst
@@ -990,17 +990,29 @@
 %22);%0A
-%09%09%7D%0A%09%7D%0A%0A%09
+        %7D%0A    %7D%0A%0A    
 publ
@@ -1048,18 +1048,24 @@
 gs%5B%5D)%7B%0A%0A
-%09%09
+        
 int matr
@@ -1152,18 +1152,24 @@
 4,25%7D%7D;%0A
-%09%09
+        
 RotateIm
@@ -1196,18 +1196,24 @@
 mage();%0A
-%09%09
+        
 ti.rotat
@@ -1227,10 +1227,16 @@
 x);%0A
-%09%09
+        
 ti.p
@@ -1253,9 +1253,12 @@
 x);%0A
-%09
+    
 %7D%0A%7D%0A

Convert tab to space