Nth term of a recurrence relation generated by two given arrays
Last Updated :
15 Jul, 2025
Given an integer N and two arrays F[] and C[] of size K that represent the first K terms and coefficient of first K terms of the below recurrence relation respectively.
FN = C1*FN - 1 + C2*FN - 2 + C3*FN - 3 +....+ CK*FN - K.
The task is to find the Nth term of the recurrence relation. Since the number can be very large take modulo to 109 + 7.
Examples:
Input: N = 10, K = 2, F[] = {0, 1}, C[] = {1, 1}
Output: 55
Explanation:
FN= FN - 1 + FN - 2 with F0 = 0, F1 = 1
The above recurrence relation forms the Fibonacci sequence with two initial values.
The remaining terms of the series can be calculated as the sum of previous K terms with corresponding multiplication with coefficient stored in C[].
Therefore, F10 = 55.
Input: N = 5, K = 3, F[] = {1, 2, 3}, C[] = {1, 1, 1}
Output: 20
Explanation:
The sequence of the above recurrence relation is 1, 2, 3, 6, 11, 20, 37, 68, ....
Every next term is the sum of the previous (K = 3) terms with base condition F0 = 1, F1 = 2 and F2 = 3
Therefore, F5 = 20.
Naive Approach: The idea is to generate the sequence using the given recurrence relation by calculating each term with the help of the previous K terms. Print the Nth Term after the sequence is formed.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
// Function to calculate Nth term of
// general recurrence relations
void NthTerm(int F[], int C[], int K,
int n)
{
// Stores the generated sequence
int ans[n + 1] = { 0 };
for (int i = 0; i < K; i++)
ans[i] = F[i];
for (int i = K; i <= n; i++) {
for (int j = i - K; j < i; j++) {
// Current term is sum of
// previous k terms
ans[i] += ans[j];
ans[i] %= mod;
}
}
// Print the nth term
cout << ans[n] << endl;
}
// Driver Code
int main()
{
// Given Array F[] and C[]
int F[] = { 0, 1 };
int C[] = { 1, 1 };
// Given N and K
int K = 2;
int N = 10;
// Function Call
NthTerm(F, C, K, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG{
static double mod = 1e9 + 7;
// Function to calculate Nth term of
// general recurrence relations
static void NthTerm(int F[], int C[], int K,
int n)
{
// Stores the generated sequence
int ans[] = new int[n + 1 ];
for(int i = 0; i < K; i++)
ans[i] = F[i];
for(int i = K; i <= n; i++)
{
for(int j = i - K; j < i; j++)
{
// Current term is sum of
// previous k terms
ans[i] += ans[j];
ans[i] %= mod;
}
}
// Print the nth term
System.out.println(ans[n]);
}
// Driver Code
public static void main (String[] args)
{
// Given Array F[] and C[]
int F[] = { 0, 1 };
int C[] = { 1, 1 };
// Given N and K
int K = 2;
int N = 10;
// Function call
NthTerm(F, C, K, N);
}
}
// This code is contributed by jana_sayantan
Python3
# Python3 program for the above approach
mod = 1e9 + 7
# Function to calculate Nth term of
# general recurrence relations
def NthTerm(F, C, K, n):
# Stores the generated sequence
ans = [0] * (n + 1)
i = 0
while i < K:
ans[i] = F[i]
i += 1
i = K
while i <= n:
j = i - K
while j < i:
# Current term is sum of
# previous k terms
ans[i] += ans[j]
ans[i] %= mod
j += 1
i += 1
# Print the nth term
print(int(ans[n]))
# Driver code
if __name__ == '__main__':
# Given Array F[] and C[]
F = [ 0, 1 ]
C = [ 1, 1 ]
# Given N and K
K = 2
N = 10
# Function call
NthTerm(F, C, K, N)
# This code is contributed by jana_sayantan
C#
// C# program for
// the above approach
using System;
class GFG{
static double mod = 1e9 + 7;
// Function to calculate Nth term of
// general recurrence relations
static void NthTerm(int [] F, int [] C,
int K, int n)
{
// Stores the generated sequence
int []ans = new int[n + 1];
for(int i = 0; i < K; i++)
ans[i] = F[i];
for(int i = K; i <= n; i++)
{
for(int j = i - K; j < i; j++)
{
// Current term is sum of
// previous k terms
ans[i] += ans[j];
ans[i] %= (int)mod;
}
}
// Print the nth term
Console.WriteLine(ans[n]);
}
// Driver Code
public static void Main (String[] args)
{
// Given Array F[] and C[]
int [] F= {0, 1};
int [] C= {1, 1};
// Given N and K
int K = 2;
int N = 10;
// Function call
NthTerm(F, C, K, N);
}
}
// This code is contributed by jana_sayantan
JavaScript
<script>
// JavaScript program for the above approach
let mod = 1e9 + 7;
// Function to calculate Nth term of
// general recurrence relations
function NthTerm(F, C, K, n)
{
// Stores the generated sequence
let ans = new Uint8Array(n + 1);
for (let i = 0; i < K; i++)
ans[i] = F[i];
for (let i = K; i <= n; i++) {
for (let j = i - K; j < i; j++) {
// Current term is sum of
// previous k terms
ans[i] += ans[j];
ans[i] %= mod;
}
}
// Print the nth term
document.write(ans[n] + "<br>");
}
// Driver Code
// Given Array F[] and C[]
let F = [ 0, 1 ];
let C = [ 1, 1 ];
// Given N and K
let K = 2;
let N = 10;
// Function Call
NthTerm(F, C, K, N);
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The Nth term of the recurrence relation can be found by using Matrix Exponentiation. Below are the steps:
- Let's consider the initial states as:
F = [f0, f1, f2.......................................fk-1]
- Define a matrix of size K2 as:
T =
[0, 0, 0, ............., Ck]
[1, 0, 0, ............., Ck-1]
[0, 1, 0, ............., Ck-2]
[.............................]
[.............................]
[0, 0, 0, ............, 0, C2]
[0, 0, 0, ............, 0, C2]
[0, 0, 0, ............, 1, C1]
- Calculate the Nth power of matrix T[][] using binary exponentiation.
- Now, multiplying F[] with Nth power of T[][] gives:
FxTN = [FN, FN + 1, FN + 2, .........................., FN + K]
- The first term of the resultant matrix F x TN is the required result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
// Declare T[][] as global matrix
int T[2000][2000];
// Result matrix
int result[2000][2000];
// Function to multiply two matrices
void mul_2(int K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
int temp[K + 1][K + 1];
memset(temp, 0, sizeof temp);
// Iterate over range [0, K]
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= K; j++) {
for (int k = 1; k <= K; k++) {
// Update temp[i][j]
temp[i][j]
= (temp[i][j]
+ (T[i][k] * T[k][j])
% mod)
% mod;
}
}
}
// Update the final matrix
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= K; j++) {
T[i][j] = temp[i][j];
}
}
}
// Function to multiply two matrices
void mul_1(int K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
int temp[K + 1][K + 1];
memset(temp, 0, sizeof temp);
// Iterate over range [0, K]
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= K; j++) {
for (int k = 1; k <= K; k++) {
// Update temp[i][j]
temp[i][j]
= (temp[i][j]
+ (result[i][k] * T[k][j])
% mod)
% mod;
}
}
}
// Update the final matrix
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= K; j++) {
result[i][j] = temp[i][j];
}
}
}
// Function to calculate matrix^n
// using binary exponentaion
void matrix_pow(int K, int n)
{
// Initialize result matrix
// and unity matrix
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= K; j++) {
if (i == j)
result[i][j] = 1;
}
}
while (n > 0) {
if (n % 2 == 1)
mul_1(K);
mul_2(K);
n /= 2;
}
}
// Function to calculate nth term
// of general recurrence
int NthTerm(int F[], int C[], int K,
int n)
{
// Fill T[][] with appropriate value
for (int i = 1; i <= K; i++)
T[i][K] = C[K - i];
for (int i = 1; i <= K; i++)
T[i + 1][i] = 1;
// Function Call to calculate T^n
matrix_pow(K, n);
int answer = 0;
// Calculate nth term as first
// element of F*(T^n)
for (int i = 1; i <= K; i++) {
answer += F[i - 1] * result[i][1];
}
// Print the result
cout << answer << endl;
return 0;
}
// Driver Code
int main()
{
// Given Initial terms
int F[] = { 1, 2, 3 };
// Given coefficients
int C[] = { 1, 1, 1 };
// Given K
int K = 3;
// Given N
int N = 10;
// Function Call
NthTerm(F, C, K, N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
static int mod = (int) (1e9 + 7);
// Declare T[][] as global matrix
static int [][]T = new int[2000][2000];
// Result matrix
static int [][]result = new int[2000][2000];
// Function to multiply two matrices
static void mul_2(int K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
int [][]temp = new int[K + 1][K + 1];
// Iterate over range [0, K]
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
for (int k = 1; k <= K; k++)
{
// Update temp[i][j]
temp[i][j] = (temp[i][j] +
(T[i][k] * T[k][j]) %
mod) % mod;
}
}
}
// Update the final matrix
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
T[i][j] = temp[i][j];
}
}
}
// Function to multiply two matrices
static void mul_1(int K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
int [][]temp = new int[K + 1][K + 1];
// Iterate over range [0, K]
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
for (int k = 1; k <= K; k++)
{
// Update temp[i][j]
temp[i][j] = (temp[i][j] +
(result[i][k] * T[k][j]) %
mod) % mod;
}
}
}
// Update the final matrix
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
result[i][j] = temp[i][j];
}
}
}
// Function to calculate matrix^n
// using binary exponentaion
static void matrix_pow(int K, int n)
{
// Initialize result matrix
// and unity matrix
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
if (i == j)
result[i][j] = 1;
}
}
while (n > 0)
{
if (n % 2 == 1)
mul_1(K);
mul_2(K);
n /= 2;
}
}
// Function to calculate nth term
// of general recurrence
static int NthTerm(int F[], int C[],
int K, int n)
{
// Fill T[][] with appropriate value
for (int i = 1; i <= K; i++)
T[i][K] = C[K - i];
for (int i = 1; i <= K; i++)
T[i + 1][i] = 1;
// Function Call to calculate T^n
matrix_pow(K, n);
int answer = 0;
// Calculate nth term as first
// element of F * (T ^ n)
for (int i = 1; i <= K; i++)
{
answer += F[i - 1] * result[i][1];
}
// Print the result
System.out.print(answer + "\n");
return 0;
}
// Driver Code
public static void main(String[] args)
{
// Given Initial terms
int F[] = {1, 2, 3};
// Given coefficients
int C[] = {1, 1, 1};
// Given K
int K = 3;
// Given N
int N = 10;
// Function Call
NthTerm(F, C, K, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for
# the above approach
mod = 1e9 + 7
# Declare T[][] as global matrix
T = [[0 for x in range (2000)]
for y in range (2000)]
# Result matrix
result = [[0 for x in range (2000)]
for y in range (2000)]
# Function to multiply two matrices
def mul_2(K):
# Create an auxiliary matrix to
# store elements of the
# multiplication matrix
temp = [[0 for x in range (K + 1)]
for y in range (K + 1)]
# Iterate over range [0, K]
for i in range (1, K + 1):
for j in range (1, K + 1):
for k in range (1, K + 1):
# Update temp[i][j]
temp[i][j] = ((temp[i][j] +
(T[i][k] * T[k][j]) %
mod) % mod)
# Update the final matrix
for i in range (1, K + 1):
for j in range (1, K + 1):
T[i][j] = temp[i][j]
# Function to multiply two matrices
def mul_1(K):
# Create an auxiliary matrix to
# store elements of the
# multiplication matrix
temp = [[0 for x in range (K + 1)]
for y in range (K + 1)]
# Iterate over range [0, K]
for i in range (1, K + 1):
for j in range (1, K + 1):
for k in range (1, K + 1):
# Update temp[i][j]
temp[i][j] = ((temp[i][j] +
(result[i][k] * T[k][j]) %
mod) % mod)
# Update the final matrix
for i in range (1, K + 1):
for j in range (1, K + 1):
result[i][j] = temp[i][j]
# Function to calculate matrix^n
# using binary exponentaion
def matrix_pow(K, n):
# Initialize result matrix
# and unity matrix
for i in range (1, K + 1):
for j in range (1, K + 1):
if (i == j):
result[i][j] = 1
while (n > 0):
if (n % 2 == 1):
mul_1(K)
mul_2(K)
n //= 2
# Function to calculate nth term
# of general recurrence
def NthTerm(F, C, K, n):
# Fill T[][] with appropriate value
for i in range (1, K + 1):
T[i][K] = C[K - i]
for i in range (1, K + 1):
T[i + 1][i] = 1
# Function Call to calculate T^n
matrix_pow(K, n)
answer = 0
# Calculate nth term as first
# element of F*(T^n)
for i in range (1, K + 1):
answer += F[i - 1] * result[i][1]
# Print the result
print(int(answer))
# Driver Code
if __name__ == "__main__":
# Given Initial terms
F = [1, 2, 3]
# Given coefficients
C = [1, 1, 1]
# Given K
K = 3
# Given N
N = 10
# Function Call
NthTerm(F, C, K, N)
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
class GFG{
static int mod = (int) (1e9 + 7);
// Declare T[,] as global matrix
static int [,]T = new int[2000, 2000];
// Result matrix
static int [,]result = new int[2000, 2000];
// Function to multiply two matrices
static void mul_2(int K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
int [,]temp = new int[K + 1,
K + 1];
// Iterate over range [0, K]
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
for (int k = 1; k <= K; k++)
{
// Update temp[i,j]
temp[i, j] = (temp[i, j] +
(T[i, k] * T[k, j]) %
mod) % mod;
}
}
}
// Update the readonly matrix
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
T[i, j] = temp[i, j];
}
}
}
// Function to multiply two matrices
static void mul_1(int K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
int [,]temp = new int[K + 1,
K + 1];
// Iterate over range [0, K]
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
for (int k = 1; k <= K; k++)
{
// Update temp[i,j]
temp[i,j] = (temp[i, j] +
(result[i, k] * T[k, j]) %
mod) % mod;
}
}
}
// Update the readonly matrix
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
result[i, j] = temp[i, j];
}
}
}
// Function to calculate matrix^n
// using binary exponentaion
static void matrix_pow(int K, int n)
{
// Initialize result matrix
// and unity matrix
for (int i = 1; i <= K; i++)
{
for (int j = 1; j <= K; j++)
{
if (i == j)
result[i, j] = 1;
}
}
while (n > 0)
{
if (n % 2 == 1)
mul_1(K);
mul_2(K);
n /= 2;
}
}
// Function to calculate nth term
// of general recurrence
static int NthTerm(int []F, int []C,
int K, int n)
{
// Fill T[,] with appropriate value
for (int i = 1; i <= K; i++)
T[i, K] = C[K - i];
for (int i = 1; i <= K; i++)
T[i + 1, i] = 1;
// Function Call to calculate T^n
matrix_pow(K, n);
int answer = 0;
// Calculate nth term as first
// element of F * (T ^ n)
for (int i = 1; i <= K; i++)
{
answer += F[i - 1] * result[i, 1];
}
// Print the result
Console.Write(answer + "\n");
return 0;
}
// Driver Code
public static void Main(String[] args)
{
// Given Initial terms
int []F = {1, 2, 3};
// Given coefficients
int []C = {1, 1, 1};
// Given K
int K = 3;
// Given N
int N = 10;
// Function Call
NthTerm(F, C, K, N);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program for the above approach
let mod = (1e9 + 7);
// Declare T[][] as global matrix
let T = new Array(2000);
// Result matrix
let result = new Array(2000);
for (let i = 0; i < 2000; i++)
{
T[i] = new Array(2000);
result[i] = new Array(2000);
for (let j = 0; j < 2000; j++)
{
T[i][j] = 0;
result[i][j] = 0;
}
}
// Function to multiply two matrices
function mul_2(K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
let temp = new Array(K + 1);
for (let i = 0; i <= K; i++)
{
temp[i] = new Array(K + 1);
for (let j = 0; j <= K; j++)
{
temp[i][j] = 0;
}
}
// Iterate over range [0, K]
for (let i = 1; i <= K; i++)
{
for (let j = 1; j <= K; j++)
{
for (let k = 1; k <= K; k++)
{
// Update temp[i][j]
temp[i][j] = (temp[i][j] +
(T[i][k] * T[k][j]) %
mod) % mod;
}
}
}
// Update the final matrix
for (let i = 1; i <= K; i++)
{
for (let j = 1; j <= K; j++)
{
T[i][j] = temp[i][j];
}
}
}
// Function to multiply two matrices
function mul_1(K)
{
// Create an auxiliary matrix to
// store elements of the
// multiplication matrix
let temp = new Array(K + 1);
for (let i = 0; i <= K; i++)
{
temp[i] = new Array(K + 1);
for (let j = 0; j <= K; j++)
{
temp[i][j] = 0;
}
}
// Iterate over range [0, K]
for (let i = 1; i <= K; i++)
{
for (let j = 1; j <= K; j++)
{
for (let k = 1; k <= K; k++)
{
// Update temp[i][j]
temp[i][j] = (temp[i][j] +
(result[i][k] * T[k][j]) %
mod) % mod;
}
}
}
// Update the final matrix
for (let i = 1; i <= K; i++)
{
for (let j = 1; j <= K; j++)
{
result[i][j] = temp[i][j];
}
}
}
// Function to calculate matrix^n
// using binary exponentaion
function matrix_pow(K, n)
{
// Initialize result matrix
// and unity matrix
for (let i = 1; i <= K; i++)
{
for (let j = 1; j <= K; j++)
{
if (i == j)
result[i][j] = 1;
}
}
while (n > 0)
{
if (n % 2 == 1)
mul_1(K);
mul_2(K);
n = parseInt(n / 2, 10);
}
}
// Function to calculate nth term
// of general recurrence
function NthTerm(F, C, K, n)
{
// Fill T[][] with appropriate value
for (let i = 1; i <= K; i++)
T[i][K] = C[K - i];
for (let i = 1; i <= K; i++)
T[i + 1][i] = 1;
// Function Call to calculate T^n
matrix_pow(K, n);
let answer = 0;
// Calculate nth term as first
// element of F * (T ^ n)
for (let i = 1; i <= K; i++)
{
answer += F[i - 1] * result[i][1];
}
// Print the result
document.write(answer + "</br>");
return 0;
}
// Given Initial terms
let F = [1, 2, 3];
// Given coefficients
let C = [1, 1, 1];
// Given K
let K = 3;
// Given N
let N = 10;
// Function Call
NthTerm(F, C, K, N);
// This code is contributed by mukesh07.
</script>
Time Complexity: O(K3log(N))
Auxiliary Space: O(K*K)
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