Question : Given an infinite array of integers which is sorted. How would you search for an integer in this array? Here is the algo (in java):
public static int searchInf(int A\[\],int high,int x){ // Assume we are searching for x if (A\[1\] > x){ return -1; } if(A\[high\] == x){ return high; } else{ int low = high; int higher = (int) Math.pow(2,high); if (A\[high\]>x){ binarySearch(A,low,higher); } else{ searchInf(A,higher,x); } }// end else return -1; }// end searchInf method So if we find some element greater than element x, perform normal binary search, else call the search inf function.
[Read More]
Minimum number of coins to get particular amount, given multiple denomination
Question: You are given some denominations of coins in an array (int denom[])and infinite supply of all of them. Given an amount (int amount OR S), find the minimum number of coins required to get the exact amount.
Problem statement
“Given a list of N coins, their values (V1, V2, … , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it’s not possible to select coins in such a way that they sum up to S.
[Read More]
Get maximum sum from coins in a line
Question: There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins. Assume that you go first, describe an algorithm to compute the maximum amount of money you can win.
Answer: If we assume that n is even then there is simple strategy to ensure that you win.
[Read More]
Find optimal number of jumps to reach last element
Question: Given an array, start from the first element and reach the last by jumping. The jump length can be at most the value at the current position in the array. Optimum result is when you reach the goal in minimum number of jumps.
Example:
Given array A = {2,3,1,1,4}
Possible ways to reach the end (index list)
0,2,3,4 (jump 2 to index 2, and then jump 1 to index 3 and then jump 1 to index 4) 0,1,4 (jump 1 to index 1, and then jump 3 to index 4) Since second solution has only 2 jumps it is the optimum result.
[Read More]
Max possible sum of non-consecutive elements
Below code works for -ve numbers : public static … vikramdpatil - May 4, 2014Below code works for -ve numbers :
public static int findMaxSum(int arr[]) {
if (arr.length == 0) return 0;
if (arr.length == 1) return arr[0];
int a = arr[0];
int b = Math.max(arr[0], arr[1]);
int c = b;
for(int i=2; i<arr.length; i++) {
c = Math.max(arr[i], Math.max(b, arr[i] + a));
a = b; // a now becomes second last sum
[Read More]
Max possible sum of non-consecutive elements
Question: There is an integer array consisting positive numbers only. Find maximum possible sum of elements such that there are no 2 consecutive elements present in the sum.
Example: If given array is (6, 4, 2, 8, 1), the answer will be 14 (8+6). This is the maximum sum we can obtain without taking two consecutive elements.
Answer: To solve these type of question, first thing is to find a recurring relation.
[Read More]
Maximum size square sub-matrix with all 1s
Question: Given a matrix consisting only 0s and 1s, find the maximum size square sub-matrix with all 1s.
Example: Consider the below matrix.
0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 The maximum square sub-matrix with all ‘1’ bits is from (2,1) to (4,3)
1 1 1 1 1 1 1 1 1 Answer:
[Read More]
Find largest sub-matrix with all 1s (not necessarily square)
In last post, we saw a dynamic programming approach to for finding maximum size square sub-matrix with all 1s. In this post, we will discuss how to find largest all 1s sub-matrix in a binary matrix. The resultant sub-matrix is not necessarily a square sub-matrix.
Example:
> > > > 1 1 0 0 1 0 > > > > > 0 1 1 1 1 1 > > > > > 1 1 1 1 1 0 > > > > > 0 0 1 1 0 0 > > ```Largest all 1s sub-matrix is from (1,1) to (2,4).
[Read More]
Maximum Area Rectangle in Histogram
Question: Find the maximum rectangle (in terms of area) under a histogram in linear time. I mean the area of largest rectangle that fits entirely in the Histogram.
(Please refer figures before code section for clarity. If I include bar i completely, those figure will tell how much maximum area rectangle I can get.)
Consider the histogram below:
Max area of the rectangle:
Max area = 4 * 3 = 12
[Read More]
Iterative or Non recursive Inorder traversal on tree
This process when implemented iteratively also requires a stack and a boolean to prevent the execution from traversing any portion of a tree twice. The general process of in-order traversal follows the following steps:
Create an empty stack S. Initialize current node as root Push the current node to S and set current = current->left until current is NULL If current is NULL and stack is not empty then
[Read More]