# Matrix

You are given the Ancestor matrix of a Binary tree, write an Algorithm to construct the corresponding tree. For example, the below tree: Will have the following ancestor Matrix The order of rows in the above matrix is not defined. I have kept it in the ascending order because the data in our nodes is […]

Given a matrix of order N*N, write code to print both the diagonals of that matrix. For example: the matrix and its 2 diagonals are given below: In the above diagram, I have colored the elements in first diagonal as red and elements in 2nd diagonal as green.

Given a square matrix of order N*N, write code to print all the elements in the order of their diagonal. For example, in the below matrix, the elements should be printed in the marked (in red) order, and the final output should be as shown below:

Each element in the array is connected to eight other elements (toward top, down, left and right). For example, the zero below is connected to all the ones 1 1 1 1 0 1 1 1 1 Given a two-dimensional array of 0’s and 1’s. Find the number of islands where an island is a […]

Glasses are arranged in the form of triangle (on top of each other) as shown below: 1 2 3 4 5 6 7 8 9 10 ……………….. …………………. Liquid is poured into 1st glass (Glass no. 1). When it is full, then extra liquid will flow into the glasses 2 and 3 in equal quantities. […]

Given a M*N order matrix, which is stored in an array in a row-major order. eg. if the matrix is of order 2*4 with below elements a b c d e f g h Then it is stored in a one dimensional array as below: a b c d e f g h Write a program […]

Given a matrix whose each row and column is sorted as shown below 10 20 30 40 15 25 35 45 27 29 37 48 32 33 39 50 Write an algorithm that will search for the matrix for a value (say 37) and print the index of position where it finds the value. If […]

Given a Matrix, write a function that will print the elements in spiral order. For example, if the Matrix is as below: Then the output should be: 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10

Given a Matrix of positive integers and 2 cells A & B. You have to reach from A to B covering cells such that the sum of elements in path is minimum. You can travel in forward, downward and diagonally. For example: If the given Matrix is as below: 1 2 3 4 8 2 […]