# Tag Archives: Dynamic Programming

## 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, 3} is {2, 3, 1, 0}. Given a number n, find total number of Derangements of a set of n elements. Examples: Input: n = 2 Output: 1 For two… 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 turn? A movement is considered turn, if we were moving along row and now move along column. OR we were moving along column and now move along row. There are… Read More »

## Number of paths with exactly k coinsOctober 23, 2015

Given a matrix where every cell has some number of coins. Count number of ways to reach bottom right from top left with exactly k coins. We can move to (i+1, j) and (i, j+1) from a cell (i, j).

## Find minimum possible size of array with given rules for removing elementsOctober 15, 2015

Given an array of numbers and a constant k, minimize size of array with following rules for removing elements. Exactly three elements can be removed at one go. The removed three elements must be adjacent in array, i.e., arr[i], arr[i+1], arr[i+2]. And the second element must be k greater than first and third element must… Read More »

## Find number of solutions of a linear equation of n variablesOctober 3, 2015

Given a linear equation of n variables, find number of non-negative integer solutions of it. For example,let the given equation be “x + 2y = 5″, solutions of this equation are “x = 1, y = 2″, “x = 5, y = 0″ and “x = 1. It may be assumed that all coefficients in… Read More »

## Find the longest path in a matrix with given constraintsOctober 2, 2015

Given a n*n matrix where numbers all numbers are distinct and are distributed from range 1 to n2, find the maximum length path (starting from any cell) such that all cells along the path are increasing order with a difference of 1. We can move in 4 directions from a given cell (i, j), i.e.,… Read More »

## Count number of ways to cover a distanceSeptember 28, 2015

Given a distance ‘dist, count total number of ways to cover the distance with 1, 2 and 3 steps. Examples: Input: n = 3 Output: 4 Below are the four ways 1 step + 1 step + 1 step 1 step + 2 step 2 step + 1 step 3 step Input: n = 4… Read More »

## Count even length binary sequences with same sum of first and second half bitsSeptember 23, 2015

Given a number n, find count of all binary sequences of length 2n such that sum of first n bits is same as sum of last n bits.

## Longest Repeating SubsequenceSeptember 18, 2015

Given a string, find length of the longest repeating subseequence such that the two subsequence don’t have same string character at same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string.

## Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)September 16, 2015

Consider the below problems statement. There 100 different types of caps each having a unique id from 1 to 100. Also, there ‘n’ persons each having a collection of variable number of caps.

## Maximum weight transformation of a given stringSeptember 15, 2015

Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation.

## Count total number of N digit numbers such that the difference between sum of even and odd digits is 1September 4, 2015

Given a number n, we need to count total number of n digit numbers such that the sum of even digits is 1 more than the sum of odd digits. Here even and odd means positions of digits are like array indexes, for exampl, the leftmost (or leading) digit is considered as even digit, next… Read More »

## Count of n digit numbers whose sum of digits equals to given sumSeptember 2, 2015

Given two integers ‘n’ and ‘sum’, find count of all n digit numbers with sum of digits as ‘sum’. Leading 0’s are not counted as digits. 1

## Minimum Initial Points to Reach Destination

Given a grid with each cell consisting of positive, negative or no points i.e, zero points. We can move across a cell only if we have positive points ( > 0 ).

## Total number of non-decreasing numbers with n digitsAugust 30, 2015

A number is non-decreasing if every digit (except the first one) is greater than or equal to previous digit. For example, 223, 4455567, 899, are non-decreasing numbers.

## Find length of the longest consecutive path from a given starting characterAugust 20, 2015

Given a matrix of characters. Find length of the longest path from a given character, such that all characters in the path are consecutive to each other, i.e., every character in path is next to previous in alphabetical order.

## Tiling ProblemAugust 14, 2015

Given a “2 x n” board and tiles of size “2 x 1″, count the number of ways to tile the given board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically i.e., as 2 x 1 tile.

## Minimum number of squares whose sum equals to given number n

A number can always be represented as a sum of squares of other numbers. Note that 1 is a square and we can always break a number as (1*1 + 1*1 + 1*1 + …). Given a number n, find the minimum number of squares that sum to X.

## Find minimum number of coins that make a given valueAugust 13, 2015

Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?

## Collect maximum points in a grid using two traversalsAugust 11, 2015

Given a matrix where every cell represents points. How to collect maximum points using two traversals under following conditions?