## Coin Change: Minimum number of coins

Problem: You are given n types of coin denominations of values v(1)<v(2)<…<v(n) (all integers). Assume v(1)=1, so you can always make change for any amount of money C. Give an algorithm which makes change for an amount of money C with as few coins as possible. #include <stdio.h> #include <stdlib.h> int main() { int i,n,den[20],temp[20],min,min_idx, … Read more

## How to count integers between large A and B with a certain property?

In programming contests, the following pattern occurs in a lot of tasks: Given numbers A and B that are huge (maybe 20 decimal digits or more), determine the number of integers X with A ≤ X ≤ B that have a certain property P SPOJ has lots of tasks like these for practice. Where examples … Read more

## What’s the difference between recursion, memoization & dynamic programming? [duplicate]

This question already has answers here: Closed 9 years ago. Possible Duplicate: Dynamic programming and memoization: top-down vs bottom-up approaches I have gone through a lot of articles on this but can’t seem to make sense of it. At times recursion and dynamic programming looks the same and at others memoization & dynamic programming look … Read more

## Find the number of occurrences of a subsequence in a string

For example, let the string be the first 10 digits of pi, 3141592653, and the subsequence be 123. Note that the sequence occurs twice: 3141592653 1 2 3 1 2 3 This was an interview question that I couldn’t answer and I can’t think of an efficient algorithm and it’s bugging me. I feel like … Read more

## Getting the submatrix with maximum sum?

Input: A 2-dimensional array NxN – Matrix – with positive and negative elements. Output: A submatrix of any size such that its summation is the maximum among all possible submatrices. Requirement: Algorithm complexity to be of O(N^3) History: With the help of the Algorithmist, Larry and a modification of Kadane’s Algorithm, i managed to solve … Read more

## What is the minimum cost to connect all the islands?

There is a grid of size N x M. Some cells are islands denoted by ‘0’ and the others are water. Each water cell has a number on it denoting the cost of a bridge made on that cell. You have to find the minimum cost for which all the islands can be connected. A … Read more

## Find common substring between two strings

I’d like to compare 2 strings and keep the matched, splitting off where the comparison fails. So if I have 2 strings – string1 = apples string2 = appleses answer = apples Another example, as the string could have more than one word. string1 = apple pie available string2 = apple pies answer = apple … Read more

## A simple example for someone who wants to understand Dynamic Programming [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 6 years ago. Improve this question I am looking for a manageably understandable example for someone who wants to learn Dynamic Programming. There are nice … Read more

## Why is the knapsack problem pseudo-polynomial?

I know that Knapsack is NP-complete while it can be solved by DP. They say that the DP solution is pseudo-polynomial, since it is exponential in the “length of input” (i.e. the numbers of bits required to encode the input). Unfortunately I did not get it. Can anybody explain that pseudo-polynomial thing to me slowly … Read more

## Throwing cats out of windows

Imagine you’re in a tall building with a cat. The cat can survive a fall out of a low story window, but will die if thrown from a high floor. How can you figure out the longest drop that the cat can survive, using the least number of attempts? Obviously, if you only have one … Read more