## EMC Interview Question

Consultants**Team:**VDirector

**Country:**United States

**Interview Type:**Phone Interview

we would need the sum total of the size of the array which will help us in finding our the number of times we need to run the loop

```
int i=0,j=0,t=0;
boolean flag = false;
for (int c=0; c <= 9 ; c++){ //9 is addition of i and j of 2D array
i = 0; j = t;
//System.out.pritln(c%5);
for (int k = 0; k <= t; k++){
System.out.print(a[i++][j--]);
}
System.out.print("\n");
if (t < 4 && flag == false)
t++;
else
{
t--;
flag = true;
}
}
```

But I guess your program does not meet the demanded output.So please refactor them more to print the demanded output.

```
public static void printMatrix(int[][] array)
{
if(array == null)
{
return ;
}
int rowCount = array.length;
int columnCount = array[1].length;
for(int i=0 ;i<rowCount ; i++)
{
int k =0;
int v = i;
while(v!= -1 && k <= i)
{
System.out.print(array[k][v]);
v--;
k++;
}
System.out.println();
}
for(int i = 1 ; i< columnCount ; i++)
{
int k = i;
int v = rowCount-1 ;
while(k<rowCount)
{
System.out.print(array[k][v]);
k++;
v--;
}
System.out.println();
}
}
```

before I posted the question I figured out the solution already.Wanted to see the response.

```
package com.sf.algorithmsStrcutures.binarySearch;
public class printBiMatrix {
static int a[][] = { {1 ,2, 3, 4},
{5, 4, 3, 5},
{6, 5, 9, 8},
{9, 8, 7, 6}
};
static int factorial(int n){
int factorial = 1 ;
int max = n;
for(int index = max;index>0;index-- ){
factorial = factorial * index;
}
return factorial;
}
/**
* @param args
*/
public static void main(String[] args) {
int size = a.length;
//int numberOfChances = factorial(size);
for(int index=0;index < 2*size;index++){
for(int i=0;i < size;i++){
for(int j=0;j< size;j++){
if((i + j) == index){
System.out.print(a[i][j]);
}
}
}
System.out.println();
}
}
}
//Increase the bimatrix array into any size like 5*5 or 6*6 or 7*7 or 8*8
```

I'm trying to understand why people aren't surrounding their code with braces (as described immediately above the comment box) to preserve line breaks and offer code highlighting.

Did you not see these instructions? Did you not care? Did you not understand them?

I'd like to get people using this more widely, and I don't understand why they're not.

Try this code:

```
class PrintArray
{
public static void main(String[] args)
{
int[][] arr= {
{1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2}
};
for (int i = 0 ; i < arr.length ; i++)
{
int temp = i;
for ( int j=0; j <= i; j++ )
{
System.out.print(arr[j][temp--]);
}
System.out.print("\n");
}
for (int i = 1 ; i < arr.length ; i++)
{
int temp = i;
for (int j = arr[i].length - 1; j >= i; j--)
{
System.out.print(arr[temp++][j]);
}
System.out.print("\n");
}
}
}
```

```
public void print(int[][] array) {
int size = array.length;
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
System.out.println(array[j][i-j]);
}
System.out.prinln("\n");
}
for (int i = 1; i < size; i++) {
for (int j = i; j < size; j++) {
System.out.println(array[j][size - j]);
}
System.out.println("\n");
}
}
```

```
private static void PrintDiagonal(int[][] input)
{
int totalLines = input.Length * 2 - 1;
for (int line = 0; line < totalLines; line++)
{
Console.WriteLine();
int row = line >= input.Length ? input.Length - 1 : line;
for (int column = line - row; row < input.Length && row >= 0 && column >= 0 && column < input.Length; row--, column++)
{
Console.Write(" " + input[column][row]);
}
}
}
```

private static void printDiagonal(int[][] a, int m) {

int i,j;

int var_i,var_j;

for(j = 0; j < m; j++){

var_i = 0;

var_j = j;

do{

System.out.print(a[var_i][var_j]);

var_i++;

var_j--;

}while(var_i <= j);

System.out.println();

}

for(i = 1; i < m;i++){

var_i = i;

var_j = m-1;

do{

System.out.print(a[var_i][var_j]);

var_i++;

var_j--;

}while( var_j >= i);

System.out.println();

}

}

#define R 5

#define C 5

void printArr(int arr[R][C])

{

for(int i=0;i<C;i++)

{

int temp=0;

for(int j=i;j>=0;j--)

{

printf("%d",arr[temp][j]);

temp++;

}

printf("\n");

}

for(int i=1;i<C;i++)

{

int temp=i;

for(int j=R-1;j>=i;j--)

{

printf("%d",arr[temp][j]);

temp++;

}

printf("\n");

}

}

int main()

{

int arr[5][5]={{1 ,2, 3, 4,6},

{5, 4, 3, 5,7},

{6, 5, 9, 8,9},

{9, 8, 7, 6,1},

{1, 8, 3, 6,2}};

printArr(arr);

getchar();

return 0;

}

#include <stdio.h>

int ary[5][5] = { {1 ,2, 3, 4,6},

{5, 4, 3, 5,7},

{6, 5, 9, 8,9},

{9, 8, 7, 6,1},

{1, 8, 3, 6,2} };

int main() {

int x, y , z , w , k;

int change_y = 0;

int change_y_end = 0;

for(x = 0 ; x < 5; x++ ) {

for(y = change_y; y < 5 ; y++ ) {

z = x;

w = y;

for( k = y; k >= change_y_end; k-- ) {

printf("%d ", ary[z++][k]);

}

printf("\n");

}

if(y == 5){

change_y = 4;

}

change_y_end++;

}

return 0;

}

```
int main()
{
int arr[5][5] ={ {1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2}
};
for(int i = 0; i < 5 ; i ++)
{
int temp = i;
for(int j = 0; j <= i ; j++)
cout<<arr[j][temp--];
cout<<"\n";
}
for(int i = 1; i < 5 ; i ++)
{
int temp = i;
for(int j = 4; j >= i ; j--)
cout<<arr[temp++][j];
cout<<"\n";
}
}
```

I think this is the simplest way...

int main()

{

int arr[5][5] ={ {1 ,2, 3, 4,6},

{5, 4, 3, 5,7},

{6, 5, 9, 8,9},

{9, 8, 7, 6,1},

{1, 8, 3, 6,2}

};

int temp=0;

While(temp<9)

{

for(i=0;i<5;i++)

for(j=0;j<5;j++)

{

if(i+j=temp)

Print(arr[i][j]);

if(i+j=8)

temp++;

}

print("/n");

}

}

This code is more general. It simply along each left-hand diagonals and print the values.

```
void printDiagonalString(vector<vector<int> > &v){
int size = (int)v.size();
int len = size * 2 - 1;
int i, j;
for(int k = 0; k < len; k++){
if(k < size){
i = 0;
j = k;
}else{
i = k - size + 1;
j = size - 1;
}
while(i < size && j >= 0){
cout<<v[i++][j--];
}
cout<<endl;
}
}
```

this is very simple algorithem

```
void PrintMatrixSpecial(int [][] a)
{
int i =0,x,y;
for (i =0 ;i < size ;i++)
{
for (x=i ; x>=0;x--)
{
y = i-x;
printf("%d",a[x][y]);
}
printf("\n");
}
}
```

int level = 0;

- Mohammed February 28, 2013int x = -1;

int y = 0;

int tempX = 0;

int tempY = 0;

for (int k = 0; k < (n * 2) - 1; k++)

{

if (k >= n)

{

y++;

level--;

}

else

{

x++;

level++;

}

tempX = x;

tempY = y;

while (tempX != -1 && tempY != x+1)

{

Console.Write(A[tempX, tempY] + " ");

tempX--;

tempY++;

}

Console.WriteLine();