Category Archives: Dynamic Programming

Maximum subarray sum in O(n) using prefix sum

Given an Array of Positive and Negative Integers, find out the Maximum Subarray Sum in that Array. Examples: Input1 : arr = {-2, -3, 4,… Read More »

Counting numbers of n digits that are montone

Call decimal number a monotone if: . Write a program which takes positive number n on input and returns number of decimal numbers of length… Read More »

Longest palindrome subsequence with O(n) space

Given a sequence, find the length of the longest palindromic subsequence in it. More examples Input : abbaab Output : 4 Input : geeksforgeeks Output… Read More »

Check for possible path in 2D matrix

Given a 2D array(m x n), check if there is any path from top left to bottom right. In the matrix, -1 is considered as… Read More »

Jacobsthal and Jacobsthal-Lucas numbers

The Jacobsthal sequence is an additive sequence similar to the Fibonacci sequence, defined by the recurrence relation Jn = Jn-1 + Jn-2, with initial terms… Read More »

K maximum sums of non-overlapping contiguous sub-arrays

Given an Array of Integers and an Integer value k, find out k non-overlapping sub-arrays which have k maximum sums. Examples: Input : arr1[] =… Read More »

Largest rectangular sub-matrix having sum divisible by k

Given a n x n matrix of integers. The problem is to find the largest area rectangular sub-matrix having sum divisible by the given value… Read More »

Minimum number of deletions to make a string palindrome | Set 2

Given a string A, compute the minimum number of characters you need to delete to make resulting string a palindrome. Examples: Input : baca Output… Read More »

Count ways to reach the nth stair using step 1, 2 or 3

A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement… Read More »

Newman-Conway Sequence

Newman-Conway Sequence is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7… In mathematical… Read More »

Maximum product of an increasing subsequence

Given an array of numbers, find the maximum product formed by multiplying numbers of an increasing subsequence of that array. Note: A single number is… Read More »

Number of ways to form a heap with n distinct integers

Given n, how many distinct Max Heap can be made from n distinct integers? Examples: Input : n = 3 Output : Assume the integers… Read More »

Hosoya’s Triangle

The Fibonnaci triangle or Hosoya’s triangle is a triangular arrangement of numbers based on Fibonacci numbers. Each number is the sum of two numbers above… Read More »

Count number of increasing subsequences of size k

Given an array arr[] containing n integers. The problem is to count number of increasing subsequences in the array of size k. Examples: Input :… Read More »

Different ways to sum n using numbers greater than or equal to m

Given two natural number n and m. The task is to find the number of ways in which the numbers that are greater than or… Read More »