NVIDIA Interview Question
Software Engineer / Developers<pre lang="" line="1" title="CodeMonkey6461" class="run-this">public class RotateBy180Degree {
public static void rotateIt(int[][] arr) {
int temp;
for (int row1 = 0, row2 = arr.length - 1; row1 <= row2; ++row1, --row2) {
for (int col1 = 0, col2 = arr[0].length - 1; col1 < arr[0].length; ++col1, --col2) {
if (row1 == row2 && col1 >= col2) {
break;
}
temp = arr[row1][col1];
arr[row1][col1] = arr[row2][col2];
arr[row2][col2] = temp;
}
}
}
public static void main(String[] args) {
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
int[][] arr2 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
for (int[] row : arr) {
System.out.println(Arrays.toString(row));
}
RotateBy180Degree.rotateIt(arr);
System.out.println();
for (int[] row : arr) {
System.out.println(Arrays.toString(row));
}
System.out.println();
for (int[] row : arr2) {
System.out.println(Arrays.toString(row));
}
RotateBy180Degree.rotateIt(arr2);
System.out.println();
for (int[] row : arr2) {
System.out.println(Arrays.toString(row));
}
}
}</pre>
This is basically to find the transpose of matrix. We can certainly not use the same array as the destination array as given in solution above because it can or can not be a square matrix. We create a new matrix with dimensions n*m where n is the number of columns and m is the number of rows in original matrix.
Hi,
I think Transpose of a matrix is not turning the matrix by 180 degrees. Its rotating the matrix by 90 degrees Hence we dont need a new array. So, if a matrix is
1 2 3 4 5
6 7 8 9 10
It becomes,
10 9 8 7 6
5 4 3 2 1
void rotate_180(int m[M][N])
{
for (int i=0; i<M/2; i++)
for (int j=0; j<N; j++)
{
int t = m[i][j];
m[i][j] = m[M-1-i][N-1-j];
m[M-1-i][N-1-j] = t;
}
}
@anonymous
this algo looks buggy. It will work correctly when number of rows in even. But if rows are odd, it will not return the correct answer.
e.g.
for input:
{'x','y','z'},
{'p','q','r'},
{'m','n','o'},
the output your algo will give is:
{'o','n','m'},
{'p','q','r'},
{'z','y','x'},
the second row is incorrect, it should be {'r','q','p'},
This works correctly: (written for character array instead of ints)
void rotateBy180(char[r,c])
{
int totalCount = (int)(r * c);
totalCount /= 2;
for (int i = 0; i < r/2; i++)
{
for (int j = 0; j < c; j++)
{
if (totalCount == 0)
break;
char temp = matrix[i, j];
matrix[i, j] = matrix[(int)r - 1 - i, (int)c - 1 - j];
matrix[(int)r - 1 - i, (int)c - 1 - j] = temp;
totalCount--;
}
}
}
Transpose of matrix is as below:-
#include<stdio.h>
int a[2][3]={1,2,3,
4,5,6};
int main()
{
int i,j;
printf("Print the matrix elements are :- \n");
int b[3][2];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",a[i][j]);
b[j][i]=a[i][j];
}
printf("\n");
}
printf("Transpose of matrix is as :- \n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",b[i][j]);
}
printf("\n");
}
return 0;
}
Ans :-
Print the matrix elements are :-
1 2 3
4 5 6
Transpose of matrix is as :-
1 4
2 5
3 6
int matrix[row][col], temp[col];
//run it for half the rows, it will take care of odd/even cases
for(r=0;r<row/2;r++)
{
for(c=0;c<(col-1);c++)
{
//copy reverse of last row into an array
temp[c]=matrix[row-r][col-c];
//copy the reverse of rth row into the (row-r)th row
matrix[row-r][col-c]=matrix[r][c];
//copy temp into the rth row from top
matrix[r][c]=temp[c];
}
}
package com.app.matrix;
import java.util.Arrays;
public class RotateMatrixBy180Degree {
private static void rotateMatrix(int[][] mat) {
int nRows = mat.length;
int nCols = mat[0].length;
// reverse row
int i = 0;
int j = nRows - 1;
int k = 0;
while (i < j) {
int[] tmp = mat[j];
mat[j] = mat[i];
mat[i] = tmp;
i++;
j--;
}
// reverse column
i = 0;
while(i < nRows) {
int[] row = mat[i];
k = 0;
j = nCols - 1;
while(k < j) {
int tmp = row[k];
row[k] = row[j];
row[j] = tmp;
k++;
j--;
}
i++;
}
}
public static void main(String[] args) {
int[][] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
rotateMatrix(mat);
System.out.println(Arrays.deepToString(mat));
}
}
- Vir Pratap Uttam May 04, 2015