Water overflowing from glass arranged in form of triangle

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. […]

Rectangles in a matrix

Given a character matrix in which some of the cells have alphabets written on them while other cells are empty (empty cells contains character ‘-‘ in them). For example consider the below matrix: A – – – B – – – C All characters are unique. Set character on all empty cells in such a […]

Sum of rectangle region in a given matrix

Given a matrix of integers of order M*N and coordinates of a rectangular region in the matrix, write code to find the sum of all the elements in that rectangular region. For example, if the input matrix is And co-ordinated given are (3,4) and (6, 8), then they represent the below rectangular region whose Sum […]

Build binary tree from ancestor matrics

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 […]

Print both diagonals of a matrix

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.

Print elements of a matrix in diagonal order

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:

Islands in a two dimensional array

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 […]

Transpose of non-symmetric matrix

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 […]

Search in a matrix sorted on rows & columns

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 […]