Category Archives: Matrix

Find difference between sums of two diagonals

Given a matrix of n X n. The task is to calculate the absolute difference between the sums of its diagonal. Examples: Input : mat[][] = 11 2 4 4 5 6 10 8 -12 Output : 15 Sum of primary diagonal = 11 + 5 + (-12) = 4. Sum of primary diagonal =… Read More »

Find number of endless points

Given a binary N x N matrix, we need to find the total number of matrix positions from which there is an endless path. Any position (i, j) is said to have an endless path if and only if all of the next positions in its row(i) and its column(j) should have value 1. If… Read More »

Program for Sudoku Generator

Background: Following are the rules of Suduku for a player. In all 9 sub matrices 3×3 the elements should be 1-9, without repetition. In all rows there should be elements between 1-9 , without repetition. In all columns there should be elements between 1-9 , without repetition. The task is to generate a 9 x… Read More »

Program for Conway’s Game Of Life

Initially, there is a grid with some cells which may be alive or dead. Our task to generate the next generation of cells based on the following rules: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three live neighbors lives on… Read More »

Maximum sum of hour glass in matrix

Given a 2D matrix, the task is that we find maximum sum of a hour glass. An hour glass is made of 7 cells in following form. A B C D E F G Examples: Input : 1 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0… Read More »

Number of decisions to reach destination

Given a grid which consists of 4 types of characters : ‘B’ ‘.’ ‘S’ and ‘D’. We need to reach D starting from S, at each step we can go to neighboring cells i.e. up, down, left and right. Cells having character ‘B’ are blocked i.e. at any step we can’t move to cell having… Read More »

Maximum and Minimum in a square matrix.

Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive… Read More »