Interview Question
Country: India
Interview Type: Written Test
public class RowColum {
static void updateArray(int size){
int n = size, count=1;
int arr[][] = new int[n][n];
for(int i = 0; i < n; i++){
if(i%2==0){
for(int j=0; j<n; j++){
arr[j][i] = count++;
}
}
else{
for(int j = n-1; j >-1; j--){
arr[j][i] = count++;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(arr[i][j] +"\t");
}
System.out.println();
}
}
public static void main(String args[]){
updateArray(3);
}
}
import java.io.*;
public class snakeMatrix{
public static void main(String[] args)
{
int totalRows,totalCols;
int row,col;
System.out.println("Enter total Number of rows or cols :");
totalCols=totalRows=5;//can be changed
int a[][]=new int[totalRows][totalCols];
for(row=0;row<totalRows;row++)
{
int x=10*row;
for(col=0;col<totalCols;col++)
{
a[row][col]=row+col+x;//generating random numbers
}
}
System.out.println("\n"+"\n");
for(row=0;row<totalRows;row++)
{
for(col=0;col<totalCols;col++)
{
//System.out.print(a[row][col]+"\t");
}
System.out.print("\n");
}
System.out.println("\n"+"\n");
for(col=0;col<totalCols;col++)
{
for(row=0;row<totalRows;row++)
{
if( ((col+2)%2) == 0)
System.out.print(a[row][col]);
else
System.out.print(a[totalRows-row-1][col]);
System.out.print("\t");
}
System.out.print("\n");
}
}
}
Transpose then swap 2 elements:
for(i=0; i<3; i++)
for(j=i+1; j<3; j++)
swap(a[i][j], a[j][i]); //transpose
swap(a[0][1], a[2][1]); //extra swap
Nevertheless, for this specific problem size (fixed problem),
It is simply a permutation of the 6 nondiagonal spots.
So you can hard code 7 writes/copies using 1 temp variable if you like.
No loops needed.
ArrayList<Integer> a = new ArrayList<Integer>();
int row=mat.length;
int col=mat[0].length;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
a.add(mat[i][j]);
}
}
int i=0;
int count=0;
for(int j=0;j<col;j++){
System.out.println("==> J:"+j);
if(j%2!=0){
for(i=2;i>=0;i--){
mat1[i][j]=a.get(count);
count++;
}
}
if(j%2==0){
for(i=0;i<=2;i++){
mat1[i][j]=a.get(count);
count++;
}
}
}
public class matSum {
static int[][] mat =
{
{1,2,3},
{4,5,6},
{7,8,9}
};
static int[][] mat1 = new int[3][3];
public static void main (String args[]){
ArrayList<Integer> a = new ArrayList<Integer>();
int row=mat.length;
int col=mat[0].length;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
a.add(mat[i][j]);
}
}
int i=0;
int count=0;
for(int j=0;j<col;j++){
System.out.println("==> J:"+j);
if(j%2!=0){
for(i=2;i>=0;i--){
mat1[i][j]=a.get(count);
count++;
}
}
if(j%2==0){
for(i=0;i<=2;i++){
mat1[i][j]=a.get(count);
count++;
}
}
}
}
}
def swapMatrix(matrix):
row = len(matrix)
column = len(matrix[0])
mat_duplicate = [[0 for i in range(0, row)] for j in range(0, column)]
for i in range(0, row):
for j in range(0, column):
if i % 2 != 0:
mat_duplicate[column - j - 1][i] = matrix[i][j]
else:
mat_duplicate[j][i] = matrix[i][j]
return mat_duplicate
def swapMatrix(matrix):
row = len(matrix)
column = len(matrix[0])
mat_duplicate = [[0 for i in range(0, row)] for j in range(0, column)]
for i in range(0, row):
for j in range(0, column):
if i % 2 != 0:
mat_duplicate[column - j - 1][i] = matrix[i][j]
else:
mat_duplicate[j][i] = matrix[i][j]
return mat_duplicate
- Anonymous October 31, 2013