Given n dices each with m faces, numbered from 1 to m, the task is to find the number of ways to get sum x. x is the summation of values on each face when all the dice are thrown.
Examples:
Input: m = 6, n = 3, x = 12
Output: 25
Explanation: There are 25 total ways to get the Sum 12 using 3 dices with faces from 1 to 6.
Input: m = 2, n = 3, x = 6
Output: 1
Explanation: There is only 1 way to get the Sum 6 using 3 dices with faces from 1 to 2. All the dices will have to land on 2.
Using Recursion - O(m^n) Time and O(m) Space
The idea is to explore all possible ways to achieve the target sum. For each die, the approach tries every possible face value (from 1 to m), and recursively checks the number of ways to achieve the remaining sum with the remaining dice. At each recursive step, the function reduces the number of dice by one and subtracts the current die's value from the target sum. Base cases handle scenarios where all dice have been used or the target sum cannot be achieved, returning 1 for a valid combination and 0 for invalid scenarios.
Mathematically the recurrence relation will look like the following:
- noOfWays(m, n, x) = sum (noOfWays(m, n-1, x-j)) for j in range[1, m].
Base Cases:
- noOfWays(m, n, x) = 1, if n = 0 and x = 0.
- noOfWays(m, n, x) = 0, if n = 0 or x < 0.
C++
// C++ program to implement
// dice throw problem using recursion
#include <bits/stdc++.h>
using namespace std;
int noOfWays(int m, int n, int x) {
// Base case: Valid combination
if (n == 0 && x == 0)
return 1;
// Base case: Invalid combination
if (n == 0 || x <= 0)
return 0;
int ans = 0;
// Check for all values of m.
for (int i = 1; i <= m; i++) {
ans += noOfWays(m, n - 1, x - i);
}
return ans;
}
int main() {
int m = 6, n = 3, x = 8;
cout << noOfWays(m, n, x);
}
Java
// Java program to implement
// dice throw problem using recursion
import java.util.*;
class GfG {
static int noOfWays(int m, int n, int x) {
// Base case: Valid combination
if (n == 0 && x == 0) return 1;
// Base case: Invalid combination
if (n == 0 || x <= 0) return 0;
int ans = 0;
// Check for all values of m.
for (int i = 1; i <= m; i++) {
ans += noOfWays(m, n - 1, x - i);
}
return ans;
}
public static void main(String[] args) {
int m = 6, n = 3, x = 8;
System.out.println(noOfWays(m, n, x));
}
}
Python
# Python program to implement
# dice throw problem using recursion
def noOfWays(m, n, x):
# Base case: Valid combination
if n == 0 and x == 0:
return 1
# Base case: Invalid combination
if n == 0 or x <= 0:
return 0
ans = 0
# Check for all values of m.
for i in range(1, m + 1):
ans += noOfWays(m, n - 1, x - i)
return ans
if __name__ == "__main__":
m = 6
n = 3
x = 8
print(noOfWays(m, n, x))
C#
// C# program to implement
// dice throw problem using recursion
using System;
class GfG {
static int noOfWays(int m, int n, int x) {
// Base case: Valid combination
if (n == 0 && x == 0) return 1;
// Base case: Invalid combination
if (n == 0 || x <= 0) return 0;
int ans = 0;
// Check for all values of m.
for (int i = 1; i <= m; i++) {
ans += noOfWays(m, n - 1, x - i);
}
return ans;
}
static void Main(string[] args) {
int m = 6, n = 3, x = 8;
Console.WriteLine(noOfWays(m, n, x));
}
}
JavaScript
// JavaScript program to implement
// dice throw problem using recursion
function noOfWays(m, n, x) {
// Base case: Valid combination
if (n === 0 && x === 0) return 1;
// Base case: Invalid combination
if (n === 0 || x <= 0) return 0;
let ans = 0;
// Check for all values of m.
for (let i = 1; i <= m; i++) {
ans += noOfWays(m, n - 1, x - i);
}
return ans;
}
let m = 6, n = 3, x = 8;
console.log(noOfWays(m, n, x));
Using Top-Down DP (Memoization) - O(n*x*m) Time and O(n*x) Space
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: Number of ways to make sum at dice n, i.e., noOfWays(m, n, x), depends on the solutions of the subproblems noOfWays(m, n-1, x-j) for j in range [1, m]. By combining these optimal substructures, we can efficiently calculate the number of ways to make target sum at dice n.
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.
- There are only are two parameters: n and x that changes in the recursive solution. So we create a 2D matrix of size (n+1)*(x+1) for memoization.
- We initialize this matrix 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 implement
// dice throw problem using memoization
#include <bits/stdc++.h>
using namespace std;
int countRecur(int m, int n, int x, vector<vector<int>> &memo) {
// Base case: Valid combination
if (n==0 && x==0) return 1;
// Base case: Invalid combination
if (n==0 || x<=0) return 0;
// If value is memoized
if (memo[n][x] != -1) return memo[n][x];
int ans = 0;
// Check for all values of m.
for (int i=1; i<=m; i++) {
ans += countRecur(m, n-1, x-i, memo);
}
return memo[n][x] = ans;
}
int noOfWays(int m, int n, int x) {
vector<vector<int>> memo(n+1, vector<int>(x+1, -1));
return countRecur(m, n, x, memo);
}
int main() {
int m = 6, n = 3, x = 8;
cout << noOfWays(m, n, x);
}
Java
// Java program to implement
// dice throw problem using memoization
import java.util.Arrays;
class GfG {
static int countRecur(int m, int n, int x, int[][] memo) {
// Base case: Valid combination
if (n == 0 && x == 0) return 1;
// Base case: Invalid combination
if (n == 0 || x <= 0) return 0;
// If value is memoized
if (memo[n][x] != -1) return memo[n][x];
int ans = 0;
// Check for all values of m.
for (int i = 1; i <= m; i++) {
ans += countRecur(m, n - 1, x - i, memo);
}
return memo[n][x] = ans;
}
static int noOfWays(int m, int n, int x) {
int[][] memo = new int[n + 1][x + 1];
for (int[] row : memo) Arrays.fill(row, -1);
return countRecur(m, n, x, memo);
}
public static void main(String[] args) {
int m = 6, n = 3, x = 8;
System.out.println(noOfWays(m, n, x));
}
}
Python
# Python program to implement
# dice throw problem using memoization
def countRecur(m, n, x, memo):
# Base case: Valid combination
if n == 0 and x == 0:
return 1
# Base case: Invalid combination
if n == 0 or x <= 0:
return 0
# If value is memoized
if memo[n][x] != -1:
return memo[n][x]
ans = 0
# Check for all values of m.
for i in range(1, m + 1):
ans += countRecur(m, n - 1, x - i, memo)
memo[n][x] = ans
return ans
def noOfWays(m, n, x):
memo = [[-1 for _ in range(x + 1)] for _ in range(n + 1)]
return countRecur(m, n, x, memo)
if __name__ == "__main__":
m = 6
n = 3
x = 8
print(noOfWays(m, n, x))
C#
// C# program to implement
// dice throw problem using memoization
using System;
class GfG {
static int countRecur(int m, int n, int x, int[,] memo) {
// Base case: Valid combination
if (n == 0 && x == 0) return 1;
// Base case: Invalid combination
if (n == 0 || x <= 0) return 0;
// If value is memoized
if (memo[n, x] != -1) return memo[n, x];
int ans = 0;
// Check for all values of m.
for (int i = 1; i <= m; i++) {
ans += countRecur(m, n - 1, x - i, memo);
}
return memo[n, x] = ans;
}
static int noOfWays(int m, int n, int x) {
int[,] memo = new int[n + 1, x + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= x; j++) {
memo[i, j] = -1;
}
}
return countRecur(m, n, x, memo);
}
static void Main(string[] args) {
int m = 6, n = 3, x = 8;
Console.WriteLine(noOfWays(m, n, x));
}
}
JavaScript
// JavaScript program to implement
// dice throw problem using memoization
function countRecur(m, n, x, memo) {
// Base case: Valid combination
if (n === 0 && x === 0) return 1;
// Base case: Invalid combination
if (n === 0 || x <= 0) return 0;
// If value is memoized
if (memo[n][x] !== -1) return memo[n][x];
let ans = 0;
// Check for all values of m.
for (let i = 1; i <= m; i++) {
ans += countRecur(m, n - 1, x - i, memo);
}
memo[n][x] = ans;
return ans;
}
function noOfWays(m, n, x) {
const memo = Array.from({ length: n + 1 }, () => Array(x + 1).fill(-1));
return countRecur(m, n, x, memo);
}
const m = 6, n = 3, x = 8;
console.log(noOfWays(m, n, x));
Using Bottom-Up DP (Tabulation) - O(n*x*m) Time and O(n*x) Space
The idea is to fill the DP table based on previous values. For each dice i and sum j, we try all the values k from 1 to m. The table is filled in an iterative manner from i = 1 to i = n and for each sum j from 1 to x.
The dynamic programming relation is as follows:
- dp[i][j] = sum(dp[i-1][j-k]) where k is in range [1, m] and j-k >= 0.
C++
// C++ program to implement
// dice throw problem using tabulation
#include <bits/stdc++.h>
using namespace std;
int noOfWays(int m, int n, int x) {
// Create a 2D dp array with (n+1) rows and (x+1) columns
// dp[i][j] will store the number of ways to get a sum
// of 'j' using 'i' dice
vector<vector<int>> dp(n + 1, vector<int>(x + 1, 0));
// Base case: There is 1 way to get
// a sum of 0 with 0 dice
dp[0][0] = 1;
// Loop through each dice (i) from 1 to n
for (int i = 1; i <= n; i++) {
// Loop through each sum (j) from 1 to x
for (int j = 1; j <= x; j++) {
// Loop through all possible dice values (k) from 1 to m
// and if the sum j - k is valid (non-negative),
// add the number of ways from dp[i-1][j-k]
for (int k = 1; k <= m && j - k >= 0; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
// The result will be in dp[n][x], which contains
// the number of ways to get sum 'x' using 'n' dice
return dp[n][x];
}
int main() {
int m = 6, n = 3, x = 8;
cout << noOfWays(m, n, x);
}
Java
// Java program to implement
// dice throw problem using tabulation
class GfG {
static int noOfWays(int m, int n, int x) {
// Create a 2D dp array with (n+1) rows and (x+1)
// columns dp[i][j] will store the number of ways to
// get a sum of 'j' using 'i' dice
int[][] dp = new int[n + 1][x + 1];
// Base case: There is 1 way to get a sum of 0 with
// 0 dice
dp[0][0] = 1;
// Loop through each dice (i) from 1 to n
for (int i = 1; i <= n; i++) {
// Loop through each sum (j) from 1 to x
for (int j = 1; j <= x; j++) {
// Loop through all possible dice values (k)
// from 1 to m and if the sum j - k is valid
// (non-negative), add the number of ways
// from dp[i-1][j-k]
for (int k = 1; k <= m && j - k >= 0; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
// The result will be in dp[n][x], which contains
// the number of ways to get sum 'x' using 'n' dice
return dp[n][x];
}
public static void main(String[] args) {
int m = 6, n = 3, x = 8;
System.out.println(noOfWays(m, n, x));
}
}
Python
# Python program to implement
# dice throw problem using tabulation
def noOfWays(m, n, x):
# Create a 2D dp array with (n+1) rows and (x+1) columns
# dp[i][j] will store the number of ways to get a
# sum of 'j' using 'i' dice
dp = [[0 for _ in range(x + 1)] for _ in range(n + 1)]
# Base case: There is 1 way to get a sum
# of 0 with 0 dice
dp[0][0] = 1
# Loop through each dice (i) from 1 to n
for i in range(1, n + 1):
# Loop through each sum (j) from 1 to x
for j in range(1, x + 1):
# Loop through all possible dice values (k) from 1 to m
# and if the sum j - k is valid (non-negative),
# add the number of ways from dp[i-1][j-k]
for k in range(1, m + 1):
if j - k >= 0:
dp[i][j] += dp[i - 1][j - k]
# The result will be in dp[n][x], which contains
# the number of ways to get sum 'x' using 'n' dice
return dp[n][x]
if __name__ == "__main__":
m = 6
n = 3
x = 8
print(noOfWays(m, n, x))
C#
// C# program to implement
// dice throw problem using tabulation
using System;
class GfG {
static int noOfWays(int m, int n, int x) {
// Create a 2D dp array with (n+1) rows and (x+1)
// columns dp[i, j] will store the number of ways to
// get a sum of 'j' using 'i' dice
int[, ] dp = new int[n + 1, x + 1];
// Base case: There is 1 way to get a sum of 0 with
// 0 dice
dp[0, 0] = 1;
// Loop through each dice (i) from 1 to n
for (int i = 1; i <= n; i++) {
// Loop through each sum (j) from 1 to x
for (int j = 1; j <= x; j++) {
// Loop through all possible dice values (k)
// from 1 to m and if the sum j - k is valid
// (non-negative), add the number of ways
// from dp[i-1, j-k]
for (int k = 1; k <= m && j - k >= 0; k++) {
dp[i, j] += dp[i - 1, j - k];
}
}
}
// The result will be in dp[n, x], which contains
// the number of ways to get sum 'x' using 'n' dice
return dp[n, x];
}
static void Main(string[] args) {
int m = 6, n = 3,x = 8;
Console.WriteLine(noOfWays(m, n, x));
}
}
JavaScript
// JavaScript program to implement
// dice throw problem using tabulation
function noOfWays(m, n, x) {
const dp = Array.from({length : n + 1},
() => Array(x + 1).fill(0));
// Base case: There's 1 way to achieve a sum of 0 using
// 0 dice
dp[0][0] = 1;
// Loop through each dice (i) from 1 to n (number of
// dice)
for (let i = 1; i <= n; i++) {
// Loop through each sum (j) from 1 to x (target
// sum)
for (let j = 1; j <= x; j++) {
// Loop through all possible outcomes of the
// dice (k) from 1 to m (faces of the dice) If
// the sum j - k is valid (non-negative),
// increment the number of ways to achieve sum j
for (let k = 1; k <= m && j - k >= 0; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
// Return the number of ways to achieve the sum x using
// n dice
return dp[n][x];
}
const m = 6, n = 3, x = 8;
console.log(noOfWays(m, n, x));
Using Space Optimized DP - O(n*x*m) Time and O(x) Space
In previous approach of dynamic programming we have derive the relation between states as given below:
- dp[i][j] = sum(dp[i-1][j-k]) where k is in range [1, m] and j-k >= 0.
If we observe that for calculating current dp[i][j] state we only need previous row. There is no need to store all the previous states just one previous state is used to compute result.
C++
// C++ program to implement
// dice throw problem using space optimised
#include <bits/stdc++.h>
using namespace std;
int noOfWays(int m, int n, int x) {
// Initialize a 1D dp array with size (x + 1), all values set to 0.
// dp[j] will store the number of ways to get a sum of 'j' using 'i' dice.
vector<int> dp(x + 1, 0);
// Base case: There is 1 way to get
// sum 0 (using no dice)
dp[0] = 1;
// Iterate through each dice (i) from 1 to n (number of dice)
for (int i = 1; i <= n; i++) {
// Iterate backwards through all possible sums (j) from x to 1
// to ensure that the results from previous dice
// counts are not overwritten
for (int j = x; j >= 1; j--) {
// Reset dp[j] before calculating its
// new value for the current dice
dp[j] = 0;
// Loop through all possible outcomes of the dice (k)
// from 1 to m (faces of the dice)
// If j - k is a valid sum (non-negative), update dp[j]
for (int k = 1; k <= m && j - k >= 0; k++) {
dp[j] += dp[j - k];
}
}
// After each dice iteration, set dp[0] to 0
// since there are no ways to achieve sum 0
dp[0] = 0;
}
// Return the number of ways to get
// sum x using n dice
return dp[x];
}
int main() {
int m = 6, n = 3, x = 8;
cout << noOfWays(m, n, x);
}
Java
// Java program to implement
// dice throw problem using space optimised
class GfG {
// Function to find the number of ways to get a sum 'x'
// with 'n' dice
static int noOfWays(int m, int n, int x) {
// Initialize a 1D dp array of size (x + 1), all
// values initially 0 dp[j] will store the number of
// ways to get a sum of 'j' using 'i' dice
int[] dp = new int[x + 1];
// Base case: There is 1 way to get sum 0 (using no
// dice)
dp[0] = 1;
// Iterate through each dice (i) from 1 to n (total
// number of dice)
for (int i = 1; i <= n; i++) {
// Iterate backwards through all possible sums
// (j) from x to 1 to avoid overwriting the
// results from the previous dice count
for (int j = x; j >= 1; j--) {
// Reset dp[j] before calculating its new
// value for the current dice
dp[j] = 0;
// Loop through all possible outcomes of the
// dice (k) from 1 to m (faces of the dice)
// If j - k is a valid sum (non-negative),
// update dp[j]
for (int k = 1; k <= m && j - k >= 0; k++) {
dp[j] += dp[j - k];
}
}
// After each dice iteration, set dp[0] to 0
// since there are no ways to achieve sum 0
dp[0] = 0;
}
// Return the number of ways to get sum 'x' using
// 'n' dice
return dp[x];
}
public static void main(String[] args) {
int m = 6, n = 3, x = 8;
System.out.println(noOfWays(m, n, x));
}
}
Python
# Python program to implement
# dice throw problem using space optimised
def noOfWays(m, n, x):
# Initialize a 1D dp array where dp[j] will store
# the number of ways to get a sum of 'j' using 'i' dice
dp = [0] * (x + 1)
# Base case: There is 1 way to get a sum
# of 0 (using no dice)
dp[0] = 1
# Iterate through each dice (i) from 1 to n (total number of dice)
for i in range(1, n + 1):
# Iterate backwards through all possible sums (j) from x to 1
# to avoid overwriting the results from the previous dice count
for j in range(x, 0, -1):
# Reset dp[j] before calculating its new value
# for the current dice
dp[j] = 0
# Loop through all possible outcomes of the dice
# (k) from 1 to m (faces of the dice)
# If j - k is a valid sum (non-negative), update dp[j]
for k in range(1, m + 1):
if j - k >= 0:
dp[j] += dp[j - k]
# After each dice iteration, set dp[0] to 0
# since there are no ways to achieve sum 0
dp[0] = 0
# Return the number of ways to get
# sum 'x' using 'n' dice
return dp[x]
if __name__ == "__main__":
m = 6
n = 3
x = 8
print(noOfWays(m, n, x))
C#
// C# program to implement
// dice throw problem using space optimized
using System;
class GfG {
// Function to calculate the number of ways to get a sum
// 'x' using 'n' dice
static int noOfWays(int m, int n, int x) {
// Create a 1D dp array to store the number of ways
// to achieve each sum from 0 to x
int[] dp = new int[x + 1];
// Base case: there is exactly 1 way to get a sum of
// 0 (using no dice)
dp[0] = 1;
// Iterate over the number of dice
for (int i = 1; i <= n; i++) {
// Iterate backwards through all possible sums
// (from x down to 1) to prevent overwriting the
// dp values
for (int j = x; j >= 1; j--) {
// Reset dp[j] before calculating its new
// value for the current dice
dp[j] = 0;
// Loop through all possible dice outcomes
// (1 to m faces of the dice) and add the
// number of ways to achieve sum (j - k) to
// dp[j]
for (int k = 1; k <= m && j - k >= 0; k++) {
dp[j] += dp[j - k];
}
}
// After processing a dice, we set dp[0] to 0
// because there are no ways to achieve sum 0
// after using any dice
dp[0] = 0;
}
// Return the number of ways to achieve the sum 'x'
// using 'n' dice
return dp[x];
}
static void Main(string[] args) {
int m = 6, n = 3, x = 8;
Console.WriteLine(noOfWays(m, n, x));
}
}
JavaScript
// JavaScript program to implement
// dice throw problem using space optimized
function noOfWays(m, n, x) {
// Initialize dp array to store the number of ways to
// achieve each sum from 0 to x All values initially set
// to 0
const dp = Array(x + 1).fill(0);
// Base case: there is exactly 1 way to achieve sum 0
// (using no dice)
dp[0] = 1;
// Iterate over the number of dice
for (let i = 1; i <= n; i++) {
// Iterate backwards through all possible sums from
// x to 1 to avoid overwriting dp values
for (let j = x; j >= 1; j--) {
// Reset dp[j] to 0 before calculating its new
// value
dp[j] = 0;
// Loop through all possible dice outcomes (1 to
// m faces of the dice) Add the number of ways
// to achieve sum (j - k) to dp[j]
for (let k = 1; k <= m && j - k >= 0; k++) {
dp[j] += dp[j - k];
}
}
// After processing each dice, set dp[0] to 0 since
// it's not valid to have sum 0 with any dice
dp[0] = 0;
}
// Return the number of ways to achieve the sum x using
// n dice
return dp[x];
}
const m = 6, n = 3, x = 8;
console.log(noOfWays(m, n, x));
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