Category Archives: Dynamic Programming

Choice of Area

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 evaluation

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 subsequences

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 Subsequences

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 »