Friday, 14 December 2018

Dynamic programming problem

Given a binary matrix, find out the maximum size square sub-matrix with all 1s.
For example, consider the below binary matrix.
maximum-size-square-sub-matrix-with-all-1s
Please suggest solution to this in comment section.

Interview problem #2 ( whether the word can be found in the matrix)

Given a 2D matrix of characters and a target word, write a function that returns whether the word can be found in the matrix by going left-to-right, or up-to-down.
For example, given the following matrix:
[['F', 'A', 'C', 'I'],
 ['O', 'B', 'Q', 'P'],
 ['A', 'N', 'O', 'B'],
 ['M', 'A', 'S', 'S']]
and the target word 'FOAM', you should return true, since it's the leftmost column. Similarly, given the target word 'MASS', you should return true, since it's the last row.

Suggest solution to this in comment section.

Solution to problem #1

#include<iostream>
using namespace std;

int count(int m, int n) {
if (m == 1 || n == 1) {
return 1;
}

else {
return count(m, n - 1) + count(m - 1, n);
}


}

int main() {
int m,n;
cin >>m>> n;
cout << count(m, n)<<endl;

}

I did this question using recursion we can also use DP(dynamic programming) in this. The time complexity of above solution is exponential.


Explanation of recursion is that we can come to (m,n-1) and take a step down or I can come to (m-1,n) and can take a right and come to (m,n). 

Thursday, 13 December 2018

Interview problem #1 (possible path in a matrix)

Count all possible paths from top left to bottom right of a mXn matrix and the constraint is that you can only move right and bottom.


Test Cases:

1) m=2,n=2
output=2

2) m=3,n=3
output=6

3) m=5,n=5
output=70

please suggest solution to this problem.

A new series

Hello guys,
I am starting this new blog which is related to coding. First let me introduce myself. I am Fazal Ahmad third year undergraduate student currently pursuing major in Electrical Engineering at IIT Bombay. I am somewhat between beginner and intermediate in coding but trying to improve my coding skills. I am starting this blog maybe because I want to learn and by this I can also share my knowledge.
I am starting a new coding series in which I will post daily a coding question (mostly asked in interviews). I will try to solve and put solution (with explanation) on the blog but guys I want you also to contribute to solution (with explanation). Please give some suggestions so that I can improve my blog. 

Dynamic programming problem

Given a binary matrix, find out the maximum size square sub-matrix with all 1s. For example, consider the below binary matrix. Please...