Maximum Sum Increasing Subsequence
Last Updated :
23 Jul, 2025
Given an array arr[] of n positive integers. The task is to find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order.
Examples:
Input: arr[] = [1, 101, 2, 3, 100]
Output: 106
Explanation: The maximum sum of a increasing sequence is obtained from [1, 2, 3, 100].
Input: arr[] = [4, 1, 2, 3]
Output: 6
Explanation: The maximum sum of a increasing sequence is obtained from [1, 2, 3].
[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space
This problem is a variation of the standard Longest Increasing Subsequence (LIS) problem. In this problem, we will consider the sum of subsequence instead of its length.
The idea is to solve the problem recursively by considering every index i in the array as a potential end of an increasing subsequence. For each index i, initialize the maximum sum subsequence ending at i to arr[i]. Then, for every previous index j (0 ≤ j < i), check if arr[j] < arr[i]. If this condition is met, recursively compute the maximum sum subsequence ending at j and update the result for i by taking the maximum of its current value and arr[i] + maxSum(j). Finally, return the maximum value obtained across all indices as the result.
C++
// C++ program to find maximum
// Sum Increasing Subsequence
#include <bits/stdc++.h>
using namespace std;
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
int maxSumRecur(int i, vector<int> &arr) {
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j= i-1; j>=0; j--) {
if (arr[j]<arr[i]) {
ans = max(ans, arr[i]+maxSumRecur(j, arr));
}
}
return ans;
}
int maxSumIS(vector<int> &arr) {
int ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (int i=0; i<arr.size(); i++) {
ans = max(ans, maxSumRecur(i, arr));
}
return ans;
}
int main() {
vector<int> arr = {1, 101, 2, 3, 100};
cout << maxSumIS(arr);
return 0;
}
Java
// Java program to find maximum
// Sum Increasing Subsequence
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr) {
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr));
}
}
return ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr));
}
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 101, 2, 3, 100};
System.out.println(maxSumIS(arr));
}
}
Python
# Python program to find maximum
# Sum Increasing Subsequence
# Recursive function which finds the
# maximum sum increasing Subsequence
# ending at index i.
def maxSumRecur(i, arr):
ans = arr[i]
# Compute values for all
# j in range (0, i-1)
for j in range(i - 1, -1, -1):
if arr[j] < arr[i]:
ans = max(ans, arr[i] + maxSumRecur(j, arr))
return ans
def maxSumIS(arr):
ans = 0
# Compute maximum sum for each
# index and compare it with ans.
for i in range(len(arr)):
ans = max(ans, maxSumRecur(i, arr))
return ans
if __name__ == "__main__":
arr = [1, 101, 2, 3, 100]
print(maxSumIS(arr))
C#
// C# program to find maximum
// Sum Increasing Subsequence
using System;
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr) {
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.Max(ans, arr[i] + maxSumRecur(j, arr));
}
}
return ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.Length; i++) {
ans = Math.Max(ans, maxSumRecur(i, arr));
}
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 101, 2, 3, 100};
Console.WriteLine(maxSumIS(arr));
}
}
JavaScript
// JavaScript program to find maximum
// Sum Increasing Subsequence
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
function maxSumRecur(i, arr) {
let ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (let j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr));
}
}
return ans;
}
function maxSumIS(arr) {
let ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (let i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr));
}
return ans;
}
const arr = [1, 101, 2, 3, 100];
console.log(maxSumIS(arr));
[Better Approach 1] Using Top-Down DP (Memoization) - O(n^2) Time and O(n) Space
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: The maximum sum increasing subsequence ending at index i, i.e., maxSum(i), depends on the optimal solutions of the subproblems maxSum(j) where 0 <= j < i and arr[j]<arr[i]. By finding the maximum value in these optimal substructures, we can efficiently calculate the maximum sum increasing subsequence at index i.
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.
- There is one parameter, i that changes in the recursive solution. So we create a 1D array of size n for memoization.
- We initialize this array as -1 to indicate nothing is computed initially.
- Now we modify our recursive solution to first check if the value is -1, then only make recursive calls. This way, we avoid re-computations of the same subproblems.
C++
// C++ program to find maximum
// Sum Increasing Subsequence
#include <bits/stdc++.h>
using namespace std;
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
int maxSumRecur(int i, vector<int> &arr, vector<int> &memo) {
// If value is computed,
// return it.
if (memo[i]!=-1) return memo[i];
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i-1; j >= 0; j--) {
if (arr[j]<arr[i]) {
ans = max(ans, arr[i]+maxSumRecur(j, arr, memo));
}
}
return memo[i] = ans;
}
int maxSumIS(vector<int> &arr) {
int ans = 0;
vector<int> memo(arr.size(), -1);
// Compute maximum sum for each
// index and compare it with ans.
for (int i=0; i<arr.size(); i++) {
ans = max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
int main() {
vector<int> arr = {1, 101, 2, 3, 100};
cout << maxSumIS(arr);
return 0;
}
Java
// Java program to find maximum
// Sum Increasing Subsequence
import java.util.Arrays;
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr, int[] memo) {
// If value is computed,
// return it.
if (memo[i] != -1) return memo[i];
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr, memo));
}
}
return memo[i] = ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
int[] memo = new int[arr.length];
Arrays.fill(memo, -1);
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 101, 2, 3, 100};
System.out.println(maxSumIS(arr));
}
}
Python
# Python program to find maximum
# Sum Increasing Subsequence
# Recursive function which finds the
# maximum sum increasing Subsequence
# ending at index i.
def maxSumRecur(i, arr, memo):
# If value is computed,
# return it.
if memo[i] != -1:
return memo[i]
ans = arr[i]
# Compute values for all
# j in range (0, i-1)
for j in range(i - 1, -1, -1):
if arr[j] < arr[i]:
ans = max(ans, arr[i] + maxSumRecur(j, arr, memo))
memo[i] = ans
return ans
def maxSumIS(arr):
ans = 0
memo = [-1] * len(arr)
# Compute maximum sum for each
# index and compare it with ans.
for i in range(len(arr)):
ans = max(ans, maxSumRecur(i, arr, memo))
return ans
if __name__ == "__main__":
arr = [1, 101, 2, 3, 100]
print(maxSumIS(arr))
C#
// C# program to find maximum
// Sum Increasing Subsequence
using System;
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr, int[] memo) {
// If value is computed,
// return it.
if (memo[i] != -1) return memo[i];
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.Max(ans, arr[i] + maxSumRecur(j, arr, memo));
}
}
return memo[i] = ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
int[] memo = new int[arr.Length];
Array.Fill(memo, -1);
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.Length; i++) {
ans = Math.Max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 101, 2, 3, 100};
Console.WriteLine(maxSumIS(arr));
}
}
JavaScript
// JavaScript program to find maximum
// Sum Increasing Subsequence
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
function maxSumRecur(i, arr, memo) {
// If value is computed,
// return it.
if (memo[i] !== -1) return memo[i];
let ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (let j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr, memo));
}
}
memo[i] = ans;
return ans;
}
function maxSumIS(arr) {
let ans = 0;
let memo = Array(arr.length).fill(-1);
// Compute maximum sum for each
// index and compare it with ans.
for (let i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
const arr = [1, 101, 2, 3, 100];
console.log(maxSumIS(arr));
[Better Approach 2] Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n) Space
The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner. The idea is to create a 1-D array. Then fill the values using dp[i] = max(arr[i] + dp[j]) for 0 <= j < i and arr[j]<arr[i].
C++
// C++ program to find maximum
// Sum Increasing Subsequence
#include <bits/stdc++.h>
using namespace std;
int maxSumIS(vector<int> &arr) {
int ans = 0;
vector<int> dp(arr.size());
// Compute maximum sum for each
// index and compare it with ans.
for (int i=0; i<arr.size(); i++) {
dp[i] = arr[i];
for (int j=0; j<i; j++) {
if (arr[j]<arr[i]) {
dp[i] = max(dp[i], arr[i]+dp[j]);
}
}
ans = max(ans, dp[i]);
}
return ans;
}
int main() {
vector<int> arr = {1, 101, 2, 3, 100};
cout << maxSumIS(arr);
return 0;
}
Java
// Java program to find maximum
// Sum Increasing Subsequence
import java.util.Arrays;
class GfG {
static int maxSumIS(int[] arr) {
int ans = 0;
int[] dp = new int[arr.length];
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.length; i++) {
dp[i] = arr[i];
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.max(dp[i], arr[i] + dp[j]);
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 101, 2, 3, 100};
System.out.println(maxSumIS(arr));
}
}
Python
# Python program to find maximum
# Sum Increasing Subsequence
def maxSumIS(arr):
ans = 0
dp = [0] * len(arr)
# Compute maximum sum for each
# index and compare it with ans.
for i in range(len(arr)):
dp[i] = arr[i]
for j in range(i):
if arr[j] < arr[i]:
dp[i] = max(dp[i], arr[i] + dp[j])
ans = max(ans, dp[i])
return ans
if __name__ == "__main__":
arr = [1, 101, 2, 3, 100]
print(maxSumIS(arr))
C#
// C# program to find maximum
// Sum Increasing Subsequence
using System;
class GfG {
static int maxSumIS(int[] arr) {
int ans = 0;
int[] dp = new int[arr.Length];
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.Length; i++) {
dp[i] = arr[i];
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.Max(dp[i], arr[i] + dp[j]);
}
}
ans = Math.Max(ans, dp[i]);
}
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 101, 2, 3, 100};
Console.WriteLine(maxSumIS(arr));
}
}
JavaScript
// JavaScript program to find maximum
// Sum Increasing Subsequence
function maxSumIS(arr) {
let ans = 0;
let dp = new Array(arr.length);
// Compute maximum sum for each
// index and compare it with ans.
for (let i = 0; i < arr.length; i++) {
dp[i] = arr[i];
for (let j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.max(dp[i], arr[i] + dp[j]);
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
const arr = [1, 101, 2, 3, 100];
console.log(maxSumIS(arr));
[Expected Approach] Using Dynamic Programming and Greedy - O(n log(n)) time and O(n) space
This approach combines dynamic programming and a greedy strategy to efficiently find the maximum sum of an increasing subsequence in an array. The key idea is based on Longest Increasing Subsequence (or LIS) using Binary Search. Unlike LIS, here we need to compare sums, so we cannot do binary search over the array. We use a Tree Map or Similar Structure to keep sums corresponding to different values. We build the solution incrementally and prune invalid subsequences to ensure that only the most promising candidates are kept in the tree map.
- Start from the End:
- Traverse the array from the last element to the first.
- For each element, consider the best subsequences that can come after it.
- Dynamic Programming with Greedy Strategy:
- Use dynamic programming to build the solution incrementally.
- For each element, calculate the best sum of an increasing subsequence that can start from it.
- Store Maximum Subsequence Sum:
- Use a map (or similar structure) to store the maximum sum of subsequences for each element.
- The key is the element, and the value is the maximum sum possible for subsequences starting from that element.
- Greedy Pruning:
- As you traverse the array, discard subsequences that cannot contribute to the best result.
- Remove any subsequences whose sum is less than the current element's potential sum.
- Maintain Increasing Order:
- Ensure subsequences are strictly increasing by only considering elements greater than the current element for the next subsequence.
- Efficient Updates and Lookups:
- The map allows quick lookups to find the best subsequences for the next element.
- Update the map as you encounter better subsequences that include the current element.
C++
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int maxSumIS(vector<int> &arr)
{
int n = (int)arr.size();
// Map to store the maximum subsequence sum for each element
map<int, int> mp;
int answer = 0;
// Traverse the array from end to start
for (int i = n - 1; i >= 0; i--)
{
int val = arr[i];
auto it = mp.upper_bound(val);
int bestNext = 0;
if (it != mp.end())
{
bestNext = it->second;
}
int temp = val + bestNext;
answer = max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
auto removeIt = mp.lower_bound(val);
if (removeIt != mp.begin())
{
auto jt = removeIt;
--jt;
while (true)
{
// Prune invalid subsequences by checking
// if they cannot contribute to a better result
if (jt->first < val && jt->second <= temp)
{
if (jt == mp.begin())
{
mp.erase(jt);
break;
}
else
{
auto temp = jt;
--jt;
mp.erase(temp);
}
}
else
{
break;
}
}
}
// Insert the current element and its best subsequence sum into the map
auto exist = mp.lower_bound(val);
if (exist == mp.end() || exist->first != val)
{
// If element doesn't exist, insert it
mp.insert({val, temp});
}
else
{
if (exist->second < temp)
{
// If a better subsequence sum exists, update it
exist->second = temp;
}
}
}
return answer;
}
int main()
{
vector<int> arr = {1, 101, 2, 3, 100};
cout << maxSumIS(arr) << "\n";
return 0;
}
Java
import java.util.*;
public class GfG{
public static int maxSumIS(int[] arr)
{
int n = arr.length;
// Map to store the maximum subsequence sum for each
// element
TreeMap<Integer, Integer> mp = new TreeMap<>();
int answer = 0;
// Traverse the array from end to start
for (int i = n - 1; i >= 0; i--) {
int val = arr[i];
Integer bestNext = mp.higherKey(val);
int bestValue
= (bestNext != null) ? mp.get(bestNext) : 0;
int temp = val + bestValue;
answer = Math.max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
Iterator<Map.Entry<Integer, Integer> > removeIt
= mp.tailMap(val, false)
.entrySet()
.iterator();
while (removeIt.hasNext()) {
Map.Entry<Integer, Integer> entry
= removeIt.next();
if (entry.getValue() <= temp) {
removeIt.remove();
}
else {
break;
}
}
// Insert the current element and its best
// subsequence sum into the map
if (!mp.containsKey(val)) {
mp.put(val, temp);
}
else {
if (mp.get(val) < temp) {
mp.put(val, temp);
}
}
}
return answer;
}
public static void main(String[] args)
{
int[] arr = { 1, 101, 2, 3, 100 };
System.out.println(maxSumIS(arr));
}
}
Python
def max_sum_is(arr):
n = len(arr)
# Map to store the maximum subsequence sum for each element
mp = {}
answer = 0
# Traverse the array from end to start
for i in range(n - 1, -1, -1):
val = arr[i]
best_next = max((k for k in mp.keys() if k > val), default=None)
best_value = mp[best_next] if best_next is not None else 0
temp = val + best_value
answer = max(answer, temp)
# Find and remove all subsequences
# that cannot contribute to the best result
keys_to_remove = [k for k in mp if k >= val]
for k in keys_to_remove:
if mp[k] <= temp:
del mp[k]
else:
break
# Insert the current element and its best subsequence sum into the map
if val not in mp:
mp[val] = temp
else:
if mp[val] < temp:
mp[val] = temp
return answer
arr = [1, 101, 2, 3, 100]
print(max_sum_is(arr))
C#
using System;
using System.Collections.Generic;
class GfG{
public static int MaxSumIS(int[] arr)
{
int n = arr.Length;
// Map to store the maximum subsequence sum for each
// element
SortedDictionary<int, int> mp
= new SortedDictionary<int, int>();
int answer = 0;
// Traverse the array from end to start
for (int i = n - 1; i >= 0; i--) {
int val = arr[i];
int bestNext = 0;
foreach(var key in mp.Keys)
{
if (key > val) {
bestNext = mp[key];
break;
}
}
int temp = val + bestNext;
answer = Math.Max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
var keysToRemove = new List<int>();
foreach(var key in mp.Keys)
{
if (key >= val)
break;
if (mp[key] <= temp) {
keysToRemove.Add(key);
}
else {
break;
}
}
foreach(var key in keysToRemove)
{
mp.Remove(key);
}
// Insert the current element and its best
// subsequence sum into the map
if (!mp.ContainsKey(val)) {
mp[val] = temp;
}
else {
if (mp[val] < temp) {
mp[val] = temp;
}
}
}
return answer;
}
public static void Main()
{
int[] arr = { 1, 101, 2, 3, 100 };
Console.WriteLine(MaxSumIS(arr));
}
}
JavaScript
function maxSumIS(arr)
{
let n = arr.length;
// Map to store the maximum subsequence sum for each
// element
let mp = new Map();
let answer = 0;
// Traverse the array from end to start
for (let i = n - 1; i >= 0; i--) {
let val = arr[i];
let bestNext = 0;
for (let [key, value] of mp) {
if (key > val) {
bestNext = value;
break;
}
}
let temp = val + bestNext;
answer = Math.max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
for (let [key, value] of mp) {
if (key >= val)
break;
if (value <= temp) {
mp.delete(key);
}
else {
break;
}
}
// Insert the current element and its best
// subsequence sum into the map
if (!mp.has(val)) {
mp.set(val, temp);
}
else {
if (mp.get(val) < temp) {
mp.set(val, temp);
}
}
}
return answer;
}
let arr = [ 1, 101, 2, 3, 100 ];
console.log(maxSumIS(arr));
Maximum Sum Increasing Subsequence
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem