Problem :
There is an m x n grid. One can only move either down or right at any point in time. One is trying to reach the bottom-right corner of the grid.
How many possible unique paths are there?
(project euler problem 15)
The similar question has been asked in Cracking the coding interview: Here we have to count the unique paths, but there we have to find the unique paths.
[Read More]
How many possible ways the child can run up the ladder
A child is running up a ladder with n steps, and can hop either 1 step or 2 steps at a time. Calculate how many possible ways the child can run up the ladder.
This problem is similar to Fibonacci Problem. Each step n can be reached from either two steps below (n-2) or one step below (n-1), thus the number of possibilities to reach that step is the sum of the possibilities to reach those other two steps.
[Read More]
Find Nth largest element in the rotated sorted array
Please give review to this post http://rawjava.blo…
Unknown - Apr 1, 2015
Please give review to this post
http://rawjava.blogspot.com/2015/04/java-program-to-find-maximum-number.html
Good question and equally good answer..Enjoyed your post :)
Find Nth largest element in the rotated sorted array
**Question : **Find Nth largest element in the rotated sorted array
So for example we have sorted array as -
2,3,6,12, 15, 18.
Now suppose the array is rotated k times ( we don’t know k), such that array becomes
15, 18,2,3,6,12
Solution
So, to find the Nth largest element in normal sorted array is a[N]. But in this case it is rotated k, which we don’t know.
[Read More]
Find the rotation count in rotated sorted array
**Question : **Find the minimum element in the rotated sorted array.
So for example we have sorted array as -
2,3,6,12, 15, 18.
Now suppose the array is rotated k times ( we don’t know k), such that array becomes
15, 18,2,3,6,12
Solution
This can be solved in linear time and logarithmic time. If we notice the above array, we see the array has been rotated 2 times, which is also the index of smallest element in the array.
[Read More]
Tower of Hanoi Problem
There are 3 pegs source ,auxillary and target. n disks of different sizes are given which can slide onto any peg . In the beginning all of the disks are in the source peg in the order of size with largest disk at the bottom and smallest disk at the top. We have to move all the disks from source peg to target peg such that in the end the target peg will have all the disks in the same order of size.
[Read More]
Strassen's Subcubic Matrix Multiplication Algorithm
A Review on Matrix Multiplication
Given two 2 nxn matrices X, Y and their product matrix Z,
X.Y = Z
we know that Zij = (ith row of X) . (jth column of Y). So, we take row from X, and column from Y.
zij = ∑xik . ykj where k is 1 < k < n.
For eg. : Let
X = a b and Y = e f
[Read More]
Find all subsets of a given set OR Find power set of a given set
What is the complexity of this code?
Shams - Mar 4, 2012
What is the complexity of this code?
Sorry for the late response. The time complexity of method 2 will be O(n.2^n). I know it may not help now as I am over an year late, but still replying.
Find all subsets of a given set OR Find power set of a given set
Problem
Write a method that returns all subsets of a set.
Example
If we’re given a set of integers such that S = {1, 2, 3}, how can we find all the subsets of that set? For example, given S, the set of all subsets i.e. P(S) we get are {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, and {1, 2, 3}.
Here is {} is empty set denoted by Φ.
[Read More]
print the sum( inclusive of the root ) of sub-tree rooted at each node in in-order in Binary Tree
/\*
Two passes with constant space
\*/
int printSum(Tree t, int toPrint)
{
if(t == NULL)
return 0;
int lsum = printSum(t->left, toPrint);
int rsum = printSum(t->right, 0);
int sum = lsum + t->data + rsum;
if(toPrint)
printf("%d ", sum);
printSum(t->right, toPrint);
return sum;
}
/\*
One pass with n space
\*/
int printSum2(Tree t, int a\[\], int \*pos)
{
if(t == NULL)
return 0;
int lsum = printSum2(t->left, a, pos);
(\*pos)++;
int p = \*pos;
int rsum = printSum2(t->right,a, pos);
return a\[p\] = lsum + t->data + rsum;
}