# Tag Archives: Dynamic Programming

## Path with maximum average valueNovember 17, 2016

Given a square matrix of size N*N, where each cell is associated with a specific cost. A path is defined as a specific sequence of cells which starts from top left cell move only right or down and ends on bottom right cell. We want to find a path with maximum average over all existing… Read More »

## Print all longest common sub-sequences in lexicographical orderNovember 13, 2016

You are given two strings.Now you have to print all longest common sub-sequences in lexicographical order? Examples: Input : str1 = “abcabcaa”, str2 = “acbacba” Output: ababa abaca abcba acaba acaca acbaa acbca This problem is an extension of longest common subsequence. We first find length of LCS and store all LCS in 2D table… Read More »

## Maximum weight path ending at any element of last row in a matrix

Given a matrix of integers where every element represents weight of the cell. Find the path having the maximum weight in matrix [N X N]. Path Traversal Rules are: It should begin from top left element. The path can end at any element of last row. We can move to following two cells from a… Read More »

## Number of permutation with K inversionsNovember 10, 2016

Given an array, an inversion is defined as a pair a[i], a[j] such that a[i] > a[j] and i < j. We are given two numbers N and k, we need to tell how many permutation of first N number have exactly K inversion. Examples: Input : N = 3, K = 1 Output :… Read More »

## Game Theory : Choice of AreaNovember 7, 2016

Consider a game, in which you have two types of powers, A and B and there are 3 types of Areas X, Y and Z. Every second you have to switch between these areas, each area has specific properties by which your power A and power B increase or decrease. We need to keep choosing… Read More »

## All ways to add parenthesis for evaluationOctober 14, 2016

Given a string that represents an expression constituting numbers and binary operator +, – and * only. We need to parenthesize the expression in all possible way and return all evaluated values. Input : expr = “3-2-1” Output : {0, 2} ((3-2)-1) = 0 (3-(2-1)) = 2 Input : expr = “5*4-3*2” Output : {-10,… Read More »

## Count all increasing subsequencesOctober 7, 2016

We are given an array of digits (values lie in range from 0 to 9). The task is to count all the sub sequences possible in array such that in each subsequence every digit is greater than its previous digits in the subsequence. Examples: Input : arr[] = {1, 2, 3, 4} Output: 15 There… Read More »

## Count distinct occurrences as a subsequenceOctober 3, 2016

Given a two strings S and T, find count of distinct occurrences of T in S as a subsequence. Examples: Input : S = banana, T = ban Output : 3 T appears in S as below three subsequences. [ban], [ba n], [b an] Input : S = geeksforgeeks, T = ge Output : 6… Read More »

## Longest repeating and non-overlapping substringSeptember 28, 2016

Given a string str, find the longest repeating non-overlapping substring in it. In other words find 2 identical substrings of maximum length which do not overlap. If there exists more than one such substring return any of them. Examples: Input : str = “geeksforgeeks” Output : geeks Input : str = “aab” Output : a… Read More »

## Find minimum sum such that one of every three consecutive elements is takenSeptember 25, 2016

Given an array of n non-negative numbers, the task is to find the minimum sum of elements (picked from the array) such that at least one element is picked out of every 3 consecutive elements in the array. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {1, 2,… Read More »

## Count Distinct SubsequencesSeptember 18, 2016

Given a string, find count of distinct subsequences of it. Examples: Input : str = “gfg” Output : 7 The seven distinct subsequences are “”, “g”, “f”, “gf”, “fg”, “gg” and “gfg” Input : str = “ggg” Output : 4 The six distinct subsequences are “”, “g”, “gg” and “ggg” The problem of counting distinct… Read More »