Count number of ways to partition a set into k subsets
Last Updated :
23 Jul, 2025
Given two numbers n and k where n represents a number of elements in a set, find a number of ways to partition the set into k subsets.
Example:
Input: n = 3, k = 2
Output: 3
Explanation: Let the set be [1, 2, 3], we can partition it into 3 subsets in the following ways [[1,2], [3]], [[1], [2,3]], [[1,3], [2]].
Input: n = 3, k = 1
Output: 1
Explanation: There is only one way [[1, 2, 3]].
Using Recursion - O(2^n) Time and O(n) Space
We can recursively calculate the number of ways to partition a set of n elements by considering each element and either placing it in an existing subset or creating a new subset. For each element, we calculate the number of ways to partition the remaining n-1 elements into k subsets and then sum these values for all possible k. This gives us the following recurrence relation for Stirling numbers of the second kind:
- The previous n – 1 elements are divided into k partitions, i.e S(n-1, k) ways. Put this nth element into one of the previous k partitions. So, count = k * S(n-1, k)
- The previous n – 1 elements are divided into k – 1 partitions, i.e S(n-1, k-1) ways. Put the nth element into a new partition ( single element partition). So, count = S(n-1, k-1)
Base case: Return 0 for invalid cases (n == 0, k == 0, or k > n) and 1 for trivial cases (k == 1 or k == n).
Total count = S(n, k) = k * S( n - 1, k) + S(n - 1, k - 1)
C++
// A C++ program to count number of partitions
// of a set with n elements into k subsets
#include<iostream>
using namespace std;
// Returns count of different partitions of n
// elements in k subsets
int countP(int n, int k) {
// Base cases
if (n == 0 || k == 0 || k > n)
return 0;
if (k == 1 || k == n)
return 1;
// Recursive formula
// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k*countP(n-1, k) + countP(n-1, k-1);
}
int main() {
int n = 5, k = 2;
cout << countP(n, k);
return 0;
}
Java
// A Java program to count number of partitions
// of a set with n elements into k subsets
class GfG {
// Returns count of different partitions of n
// elements in k subsets
static int countP(int n, int k) {
// Base cases
if (n == 0 || k == 0 || k > n)
return 0;
if (k == 1 || k == n)
return 1;
// Recursive formula
// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k * countP(n - 1, k) + countP(n - 1, k - 1);
}
public static void main(String[] args) {
int n = 5, k = 2;
System.out.println(countP(n, k));
}
}
Python
# A Python program to count number of partitions
# of a set with n elements into k subsets
# Returns count of different partitions of n
# elements in k subsets
def countP(n, k):
# Base cases
if n == 0 or k == 0 or k > n:
return 0
if k == 1 or k == n:
return 1
# Recursive formula
# S(n+1, k) = k*S(n, k) + S(n, k-1)
return k * countP(n - 1, k) + countP(n - 1, k - 1)
n = 5
k = 2
print(countP(n, k))
C#
// A C# program to count number of partitions
// of a set with n elements into k subsets
using System;
class GfG {
// Memoization table to store subproblem results
static int[,] memo;
// Returns count of different partitions of
// n elements in k subsets
static int countP(int n, int k) {
// Base cases
if (n == 0 || k == 0 || k > n) {
return 0;
}
if (k == 1 || k == n) {
return 1;
}
// If the value is already computed,
// return it
if (memo[n, k] != -1) {
return memo[n, k];
}
memo[n, k] = k * countP(n - 1, k) + countP(n - 1, k - 1);
return memo[n, k];
}
static void Main(string[] args) {
// Set the values for n and k
int n = 5, k = 2;
// Initialize memoization table with -1
// (indicating not computed yet)
// Adjusted memoization size to [n+1, n+1]
memo = new int[n + 1, n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
memo[i, j] = -1;
}
}
// Call the function and
// print the result
Console.WriteLine(countP(n, k));
}
}
JavaScript
// A JavaScript program to count number of partitions
// of a set with n elements into k subsets
// Returns count of different partitions of n
// elements in k subsets
function countP(n, k) {
// Base cases
if (n === 0 || k === 0 || k > n) {
return 0;
}
if (k === 1 || k === n) {
return 1;
}
// Recursive formula
// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k * countP(n - 1, k) + countP(n - 1, k - 1);
}
let n = 5, k = 2;
console.log(countP(n, k));
Using Top-Down DP (Memoization) - O(n * k) Time and O(n * k) 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 number of ways to partition a set of n elements into k subsets depends on two smaller subproblems:
- The number of ways to partition the first n-1 elements into k subsets, and
- The number of ways to partition the first n-1 elements into k-1 subsets and then add the new element as its own subset. By combining these optimal solutions, we can efficiently calculate the total number of ways to partition the set.
2. Overlapping Subproblems: In the recursive approach, certain subproblems are recalculated multiple times. For example, when computing S(n, k), the subproblems S(n-1, k) and S(n-1, k-1) are recomputed multiple times. This redundancy leads to overlapping subproblems, which can be avoided using memoization or tabulation. Below is recursion tree of countP(10, 7). The subproblem countP(8,6) or CP(8,6) is called multiple times.

- The recursive solution involves two parameters: n (the number of elements) and k (the number of subsets).
- We create a 2D memoization table of size (n + 1) x (k + 1) to store results for all combinations of n and k.
- We initialize the table with -1 to indicate uncomputed subproblems, we check if the value at memo[n][k] is -1. If it is, we proceed to compute the result. otherwise, we return the stored result.
C++
// C++ code of finding the bellNumber
// using Memoization
#include <iostream>
#include <vector>
using namespace std;
// Function to compute Stirling numbers of
// the second kind S(n, k) with memoization
int stirling(int n, int k, vector<vector<int>>& memo) {
// Base cases
if (n == 0 && k == 0) return 1;
if (k == 0 || n == 0) return 0;
if (n == k) return 1;
if (k == 1) return 1;
// Check if result is already computed
if (memo[n][k] != -1) return memo[n][k];
// Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +
stirling(n - 1, k - 1, memo);
return memo[n][k];
}
// Function to calculate the total number of
// ways to partition a set of `n` elements
int countP(int n, int k) {
// Initialize memoization table with -1
vector<vector<int>> memo(n + 1, vector<int>(n + 1, -1));
return stirling(n, k, memo);
}
int main() {
int n = 5, k = 2;
int result = countP(n, k);
cout << result << endl;
return 0;
}
Java
// Java code to find the total subsets
// using Memoization
class GfG {
// Function to compute Stirling numbers of
// the second kind S(n, k) with memoization
static int stirling(int n, int k, int[][] memo) {
// Base cases
if (n == 0 && k == 0) return 1;
if (k == 0 || n == 0) return 0;
if (n == k) return 1;
if (k == 1) return 1;
// Check if result is already computed
if (memo[n][k] != -1) return memo[n][k];
// Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +
stirling(n - 1, k - 1, memo);
return memo[n][k];
}
// Function to calculate the total number of
// ways to partition a set of `n` elements
static int countP(int n, int k) {
// Initialize memoization table with -1
int[][] memo = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
memo[i][j] = -1;
}
}
return stirling(n, k, memo);
}
public static void main(String[] args) {
int n = 5, k = 2;
int result = countP(n, k);
System.out.println(result);
}
}
Python
# Python code to find the total subsets
# using Memoization
# Function to compute Stirling numbers of
# the second kind S(n, k) with memoization
def stirling(n, k, memo):
# Base cases
if n == 0 and k == 0:
return 1
if k == 0 or n == 0:
return 0
if n == k:
return 1
if k == 1:
return 1
# Check if result is already computed
if memo[n][k] != -1:
return memo[n][k]
# Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +\
stirling(n - 1, k - 1, memo)
return memo[n][k]
# Function to calculate the total number of
# ways to partition a set of `n` elements
def countP(n, k):
# Initialize memoization table with -1
memo = [[-1 for _ in range(n + 1)] for _ in range(n + 1)]
return stirling(n, k, memo)
if __name__ == "__main__":
n = 5
k = 2
result = countP(n, k)
print(result)
C#
// C# code to find the total subsets
// using Memoization
using System;
class GfG {
// Function to compute Stirling numbers of the
// second kind S(n, k) with memoization
static int Stirling(int n, int k, int[,] memo) {
// Base cases
if (n == 0 && k == 0) return 1;
if (k == 0 || n == 0) return 0;
if (n == k) return 1;
if (k == 1) return 1;
// Check if result is already computed
if (memo[n, k] != -1) return memo[n, k];
// Recursive formula
memo[n, k] = k * Stirling(n - 1, k, memo)
+ Stirling(n - 1, k - 1, memo);
return memo[n, k];
}
// Function to calculate the total number of ways
// to partition a set of `n` elements into `k` subsets
static int CountP(int n, int k) {
// Initialize memoization table with -1
int[,] memo = new int[n + 1, n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
memo[i, j] = -1;
}
}
return Stirling(n, k, memo);
}
static void Main() {
int n = 5, k = 2;
int result = CountP(n, k);
Console.WriteLine(result);
}
}
JavaScript
// JavaScript code to find the total subsets
// using Memoization
// Function to compute Stirling numbers of
// the second kind S(n, k) with memoization
function stirling(n, k, memo) {
// Base cases
if (n === 0 && k === 0) return 1;
if (k === 0 || n === 0) return 0;
if (n === k) return 1;
if (k === 1) return 1;
// Check if result is already computed
if (memo[n][k] !== -1) return memo[n][k];
// Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +
stirling(n - 1, k - 1, memo);
return memo[n][k];
}
// Function to calculate the total number of
// ways to partition a set of `n` elements
function countP(n, k) {
// Initialize memoization table with -1
const memo = Array.from({ length: n + 1 }, () => Array(n + 1).fill(-1));
return stirling(n, k, memo);
}
const n = 5, k = 2;
const result = countP(n, k);
console.log(result);
Using Bottom-Up DP (Tabulation) - O(n * k) Time and O(n * k) Space
The approach is similar to the previous one, but instead of solving the problem recursively, we can calculate the solution using a bottom-up dynamic programming approach. We maintain a 2D table dp[][]where dp[i][j] represents the number of ways to partition a set of i elements into j subsets.
Base Case:
- For i = 0 and 0 <= j < n, dp[0][j] = 0 (no elements to partition into subsets).
- For j = 0 and 0 <= i < n, dp[i][0] = 0(cannot partition i elements into 0 subsets).
- For i = j, dp[i][j] = 1(one way to partition i elements into i subsets).
Recursive Case:
- For i > 1 and j > 1,
- dp[i][j] = j * dp[i-1][j] + dp[i-1][j - 1]
- This formula arises from considering two options: either placing the i-th element in one of the existing subsets (which is j * dp[i-1][j]) or placing it in a new subset (dp[i-1][j-1]).
C++
// A Dynamic Programming based C++ program to count
// number of partitions of a set with n elements
#include <iostream>
using namespace std;
// Returns count of different partitions
// of n elements in k subsets
int countP(int n, int k) {
// Table to store results of subproblems
int dp[n + 1][k + 1];
// Initialize base cases
for (int i = 0; i <= n; i++)
// No way to partition n elements
// into 0 subsets if n > 0
dp[i][0] = 0;
for (int i = 0; i <= k; i++)
// 1 way to partition 0 elements
// into 0 subsets
dp[0][i] = (i == 0) ? 1 : 0;
// Fill rest of the entries in
// dp[][] in bottom up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || i == j)
dp[i][j] = 1;
else
dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
}
}
return dp[n][k];
}
int main() {
int n = 5, k = 2;
cout << countP(n, k);
return 0;
}
Java
// A Dynamic Programming based Java program to count
// number of partitions of a set with n elements
class GfG {
// Returns count of different partitions
// of n elements in k subsets
static int countP(int n, int k) {
// Table to store results
// of subproblems
int[][] dp = new int[n + 1][k + 1];
// Initialize base cases
for (int i = 0; i <= n; i++) {
// No way to partition n elements
// into 0 subsets if n > 0
dp[i][0] = 0;
}
// 1 way to partition 0 elements
// into 0 subsets
dp[0][0] = 1;
// Fill rest of the entries in dp[][]
// in bottom-up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (j == 1 || i == j)
// One way to partition i elements
// into i subsets or 1 subset
dp[i][j] = 1;
else
dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
}
}
return dp[n][k];
}
public static void main(String[] args) {
int n = 5, k = 2;
System.out.println(countP(n, k));
}
}
Python
# A Dynamic Programming based Python program to count
# number of partitions of a set with n elements
# Returns count of different partitions
# of n elements in k subsets
def countP(n, k):
# Table to store results of subproblems
dp = [[0 for _ in range(k + 1)] for _ in range(n + 1)]
# Initialize base cases
for i in range(n + 1):
# No way to partition n elements
# into 0 subsets if n > 0
dp[i][0] = 0
# 1 way to partition 0 elements into 0 subsets
dp[0][0] = 1
# Fill the rest of the entries
# in dp[][] in bottom-up manner
for i in range(1, n + 1):
for j in range(1, k + 1):
if j == 1 or i == j:
# One way to partition i elements
# into i subsets or 1 subset
dp[i][j] = 1
else:
dp[i][j] = j * dp[i - 1][j] + \
dp[i - 1][j - 1]
return dp[n][k]
if __name__ == "__main__":
n = 5
k = 2
print(countP(n, k))
C#
// A Dynamic Programming based C# program to count
// number of partitions of a set with n elements
using System;
class GfG {
// Returns count of different partitions
// of n elements in k subsets
static int CountP(int n, int k) {
// Table to store results of
// subproblems
int[,] dp = new int[n + 1, k + 1];
// Initialize base cases
for (int i = 0; i <= n; i++) {
// No way to partition n elements
// into 0 subsets if n > 0
dp[i, 0] = 0;
}
for (int i = 0; i <= k; i++) {
// 1 way to partition 0 elements
// into 0 subsets
dp[0, i] = (i == 0) ? 1 : 0;
}
// Fill rest of the entries in
// dp[][] in bottom-up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (j == 1 || i == j) {
// One way to partition i elements
// into i subsets or 1 subset
dp[i, j] = 1;
} else {
dp[i, j] = j * dp[i - 1, j] + dp[i - 1, j - 1];
}
}
}
return dp[n, k];
}
static void Main() {
int n = 5, k = 2;
Console.WriteLine(CountP(n, k));
}
}
JavaScript
// A Dynamic Programming based JavaScript program to count
// number of partitions of a set with n elements
// Returns count of different partitions
// of n elements in k subsets
function countP(n, k) {
// Table to store results of subproblems
let dp = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
// Initialize base cases
for (let i = 0; i <= n; i++) {
// No way to partition n elements
// into 0 subsets if n > 0
dp[i][0] = 0;
}
for (let i = 0; i <= k; i++) {
// 1 way to partition 0 elements
// into 0 subsets
dp[0][i] = (i === 0) ? 1 : 0;
}
// Fill rest of the entries in
// dp[][] in bottom-up manner
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= i; j++) {
if (j === 1 || i === j) {
dp[i][j] = 1;
} else {
dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
}
}
}
return dp[n][k];
}
const n = 5, k = 2;
console.log(countP(n, k));
Using Space Optimized DP - O(n * k) Time and O(n) Space
In previous approach the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
- Create a 1D vector dp of size n+1.
- Set a base case dp[0] = 1 .
- Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
- Initialize variable prev and temp used to store the previous values from current computations.
- After every iteration assign the value of temp to prev for further iteration.
- At last return and print the final answer stored in dp[n].
C++
// C++ program to count different
// partitions of n elements into k subsets
#include<bits/stdc++.h>
using namespace std;
// Returns count of different partitions of n
// elements in k subsets
int countP(int n, int k) {
// Vector to store results of subproblems
vector<int> dp(n+1, 0);
// Base cases
dp[0] = 1;
for (int j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
int prev = dp[0];
for (int i = 1; i <= n; i++) {
// Store current value of dp[i]
int temp = dp[i];
if (j == 1 || i == j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
int main() {
cout << countP(5, 2);
return 0;
}
Java
// Java program to count different
// partitions of n elements into k subsets
class GfG {
// Returns count of different
// partitions of n elements in k subsets
static int countP(int n, int k) {
// Vector to store results of subproblems
int[] dp = new int[n+1];
// Base cases
dp[0] = 1;
for (int j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
int prev = dp[0];
for (int i = 1; i <= n; i++) {
// Store current value of dp[i]
int temp = dp[i];
if (j == 1 || i == j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
public static void main(String[] args) {
System.out.println(countP(5, 2));
}
}
Python
# Python program to count different
# partitions of n elements into k subsets
# Returns count of different partitions
# of n elements in k subsets
def countP(n, k):
# List to store results of subproblems
dp = [0] * (n + 1)
# Base cases
dp[0] = 1
for j in range(1, k + 1):
# Store previous value of dp[i-1]
prev = dp[0]
for i in range(1, n + 1):
# Store current value of dp[i]
temp = dp[i]
if j == 1 or i == j:
# One way to partition if j == 1 or i == j
dp[i] = 1
else:
dp[i] = j * dp[i - 1] + prev
# Update prev for next iteration
prev = temp
# Return the final answer
# (number of ways to partition n elements)
return dp[n]
if __name__ == "__main__":
print(countP(5, 2))
C#
// C# program to count different
// partitions of n elements into k subsets
class GfG {
// Returns count of different
// partitions of n elements in k subsets
static int countP(int n, int k) {
// Array to store results of subproblems
int[] dp = new int[n + 1];
// Base cases
dp[0] = 1;
for (int j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
int prev = dp[0];
for (int i = 1; i <= n; i++) {
// Store current value of dp[i]
int temp = dp[i];
if (j == 1 || i == j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
static void Main(string[] args) {
System.Console.WriteLine(countP(5, 2));
}
}
JavaScript
// JavaScript program to count different
// partitions of n elements into k subsets
// Returns count of different
// partitions of n elements in k subsets
function countP(n, k) {
// Array to store results of subproblems
let dp = new Array(n + 1).fill(0);
// Base cases
dp[0] = 1;
for (let j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
let prev = dp[0];
for (let i = 1; i <= n; i++) {
// Store current value of dp[i]
let temp = dp[i];
if (j === 1 || i === j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
console.log(countP(5, 2));
Related article:
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