Close

## Printing a cross with given word

Given a string we have to print the characters of that string into a cross mark. If the string is “12345”, we need to print 1 5 2 4 3 2 4 1 5 This is printing the two diagonals of a square matrix filled the given string. Following is the simple code to print…

## Searching for a word in a grid

Given a two dimensional table of letters, and a word to be searched, design an algorithm to check if it is present in the grid. The word can be formed in any of the 8 possible directions as shown below. Here is how I solved this problem. Write a function which takes the string to…

## Maximum number of 1s in any row of a boolean matrix

Given a matrix consisting of only 0,1s, with each of it’s row sorted (i.e all 0s appear before 1s). The problem is to efficiently find the maximum number of 1s present in any row. For example consider the following matrix. 0 0 1 10 0 0 10 1 1 10 0 0 0 The maximum…

## Setting the matrix elements to zero

Given a matrix of size m * n, write a program to fill zero in ith row and jth column if matrix[i][j] is zero. For example let us consider the following matrix. [    6 0 2   1 3 4   5 9 0 ] It should transform to the following. [    0…

## Searching for an element in the sorted matrix

Given a row wise and column wise sorted two dimensional array or matrix. How to search for an element efficiently? Example of the array can be int[][] matrix = {                   {1,3,5,7,9},                   {10,12,14,16,18},                   {19,21,23,25,27},                   {28,30,32,34,36},                   {37,39,41,43,45}                }; This can be done using the following algorithm.  We start our search with the top right element.  If it…

## Maximum size square sub matrix with all 1s in a binary matrix

Given a matrix of size M x N which contains only 0s and 1s, How do we find the largest square sub-matrix which contains all 1s. For example in the following example, highlighted portion indicates a 3×3 square sub-matrix with all 1s. 1 0 0 1 1 0 0 1 1 1 1 1 0…