Microsoft Interview Question
Applications DevelopersCountry: United States
Interview Type: Phone Interview
as all we know there exists two diagonals in a matrix....
for 1st diagonal
....
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
printf("%d ",matrix[i][j]);
}
}
for 2nd diagonal....
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i+j==n)
printf("%d ",matrix[i][j]);
}
}
Printing diagonals of a matrix is quite easy, but you need to understand the relation between matrix and diagonals. If you want perfect diagonals from matrix then your matrix must be square, I mean your matrix should be NxN. For example 2×2, 4×4 or 9×9 etc. Because in rectangular matrix (2×3 or 5×7) it is not possible to find perfect diagonals.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Now if you see above matrix carefully there are two diagonals in this one is 1, 6, 11, 16 and other is 4, 7, 10, 13.
I found this implementation, which is already done, you can check algo on below website link
somanyword.com/2013/12/print-diagonals-of-a-given-matrix/
public static void main(String []args){
System.out.println("Hello World");
int [][] array = {{00,01,02,03,04},
{10,11,12,13,14},
{20,21,22,23,24},
{30,31,32,33,34},
{40,41,42,43,44}};
for(int i =0; i < array.length; i++){
System.out.print(array[i][i]+" ");
}
System.out.println();
for (int j = 0 ; j < array.length; j++ ){
System.out.print(array[j][(array.length-1)-j]+" ");
}
}
This will work only for square matrix....
// test2.cpp : main project file.
#include "stdafx.h"
#include <iostream>
using namespace System;
#define M 3
#define N 3
bool testDiagonal(int n, int m, int input [][N])
{
if (m != n) return false;
for (int i=0; i<m; i++)
{
printf ("Diagonal: %d\n", input[i][i]);
}
}
int main(array<System::String ^> ^args)
{
int input[M][N] = {{1,2,3}, {4,5,6}, {7,8,9}};
testDiagonal(M, N, input);
return 0;
}
// test2.cpp : main project file.
#include "stdafx.h"
#include <iostream>
using namespace System;
#define M 3
#define N 3
bool testDiagonal(int n, int m, int input [][N])
{
if (m != n) return false;
for (int i=0; i<m; i++)
{
printf ("Diagonal: %d\n", input[i][i]);
}
}
int main(array<System::String ^> ^args)
{
int input[M][N] = {{1,2,3}, {4,5,6}, {7,8,9}};
testDiagonal(M, N, input);
return 0;
}
public class PrintMatrixDiagonally {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] mat = { {1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20}
};
printMatrixDiagonally(mat);
}
private static void printMatrixDiagonally(int[][] mat) {
//Down side
int col = 0;
for(int row = 0; row < mat.length; row++){
diagonally(mat, row, col);
System.out.print("\n");
}
//Right side
int row = mat.length - 1;
for(col = 1; col < mat[row].length; col++){
diagonally(mat, row, col);
System.out.print("\n");
}
}
//using recursion printing matrix diagonally
private static void diagonally(int[][] mat, int row, int col) {
//Bundary check
if(row < 0 || col < 0 || row >= mat.length || col >= mat[0].length){
return;
}
System.out.print(mat[row][col]+" ");
diagonally(mat, --row, ++col);
}
}
Solution for main and incidental diagonals
public class Diagonals {
public static void main(String[] args) {
int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
new Diagonals().printDiagonals(matrix);
}
public void printDiagonals(int[][] matrix) {
StringBuilder mainDiagonal = new StringBuilder();
StringBuilder incidentalDiagonal = new StringBuilder();
for (int i = 0; i < matrix.length; i++) {
if (matrix[i].length != matrix.length) {
System.out.print("Not square matrix");
return;
}
mainDiagonal.append(matrix[i][i]);
mainDiagonal.append(' ');
incidentalDiagonal.append(matrix[i][matrix.length - i - 1]);
incidentalDiagonal.append(' ');
}
System.out.println("main Diagonal: " + mainDiagonal);
System.out.println("incidental Diagonal: " + incidentalDiagonal);
}
}
- Anonymous December 14, 2013