# Category Archives: Dynamic Programming

## Find maximum length Snake sequenceJune 2, 2016

Given a grid of numbers, find maximum length Snake sequence and print it. If multiple snake sequences exists with the maximum length, print any one… Read More »

## Minimum time to finish tasks without skipping two consecutiveMay 30, 2016

Given time taken by n tasks. Find the minimum time needed to finish the tasks such that skipping of tasks is allowed, but can not… Read More »

## Maximum profit by buying and selling a share at most k timesMay 29, 2016

In share trading, a buyer buys shares and sells on future date. Given stock price of n days, the trader is allowed to make at… Read More »

## A Space Optimized Solution of LCSApril 16, 2016

Given two strings, find the length of longest subsequence present in both of them. Examples: LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of… Read More »

## Ways to arrange Balls such that adjacent balls are of different typesMarch 21, 2016

There are ‘p’ balls of type P, ‘q’ balls of type Q and ‘r’ balls of type R. Using the balls we want to create… Read More »

## Count number of ways to fill a “n x 4” grid using “1 x 4” tilesMarch 9, 2016

Given a number n, count number of ways to fill a n x 4 grid using 1 x 4 tiles.

## Weighted Job Scheduling in O(n Log n) timeMarch 5, 2016

Given N jobs where every job is represented by following three elements of it. Start Time Finish Time Profit or Value Associated Find the maximum… Read More »

## Count number of subsets having a particular XOR valueFebruary 8, 2016

Given an array arr[] of n numbers and a number K, find the number of subsets of arr[] having XOR of elements as K Examples:… Read More »

## Permutation Coefficient

Permutation refers to the process of arranging all the members of a given set to form a sequence. The number of permutations on a set… Read More »

## Longest Zig-Zag SubsequenceFebruary 3, 2016

The longest Zig-Zag subsequence problem is to find length of the longest subsequence of given sequence such that all elements of this are alternating. If… Read More »

## Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)December 10, 2015

Given three numbers n, r and p, compute value of nCr mod p. Example: Input: n = 10, r = 2, p = 13 Output:… Read More »

## Partition a set into two subsets such that the difference of subset sums is minimumDecember 8, 2015

Given a set of integers, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums… Read More »

## Count Derangements (Permutation such that no element appears in its original position)November 18, 2015

A Derangement is a permutation of n elements, such that no element appears in its original position. For example, a derangement of {0, 1, 2,… Read More »

## Count number of paths with at-most k turnsNovember 16, 2015

Given a “m x n” matrix, count number of paths to reach bottom right from top left with maximum k turns allowed. What is a… Read More »

## Count number of ways to partition a set into k subsetsNovember 9, 2015

Given two numbers n and k where n represents number of elements in a set, find number of ways to partition the set into k… Read More »