Length of Longest Palindrome Substring
Last Updated :
15 Jul, 2025
Given a string S of length N, the task is to find the length of the longest palindromic substring from a given string.
Examples:
Input: S = "abcbab"
Output: 5
Explanation:
string "abcba" is the longest substring that is a palindrome which is of length 5.
Input: S = "abcdaa"
Output: 2
Explanation:
string "aa" is the longest substring that is a palindrome which is of length 2.
Naive Approach: The simplest approach to solve the problem is to generate all possible substrings of the given string and print the length of the longest substring which is a palindrome.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to obtain the length of
// the longest palindromic substring
int longestPalSubstr(string str)
{
// Length of given string
int n = str.size();
// Stores the maximum length
int maxLength = 1, start = 0;
// Iterate over the string
for (int i = 0;
i < str.length(); i++) {
// Iterate over the string
for (int j = i;
j < str.length(); j++) {
int flag = 1;
// Check for palindrome
for (int k = 0;
k < (j - i + 1) / 2; k++)
if (str[i + k]
!= str[j - k])
flag = 0;
// If string [i, j - i + 1]
// is palindromic
if (flag
&& (j - i + 1) > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
int main()
{
// Given string
string str = "forgeeksskeegfor";
// Function Call
cout << longestPalSubstr(str);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to obtain the length of
// the longest palindromic substring
static int longestPalSubstr(String str)
{
// Length of given string
int n = str.length();
// Stores the maximum length
int maxLength = 1, start = 0;
// Iterate over the string
for(int i = 0; i < str.length(); i++)
{
// Iterate over the string
for(int j = i; j < str.length(); j++)
{
int flag = 1;
// Check for palindrome
for(int k = 0;
k < (j - i + 1) / 2; k++)
if (str.charAt(i + k) !=
str.charAt(j - k))
flag = 0;
// If string [i, j - i + 1]
// is palindromic
if (flag != 0 &&
(j - i + 1) > maxLength)
{
start = i;
maxLength = j - i + 1;
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
public static void main (String[] args)
{
// Given string
String str = "forgeeksskeegfor";
// Function call
System.out.print(longestPalSubstr(str));
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function to obtain the length of
# the longest palindromic substring
def longestPalSubstr(str):
# Length of given string
n = len(str)
# Stores the maximum length
maxLength = 1
start = 0
# Iterate over the string
for i in range(len(str)):
# Iterate over the string
for j in range(i, len(str), 1):
flag = 1
# Check for palindrome
for k in range((j - i + 1) // 2):
if (str[i + k] != str[j - k]):
flag = 0
# If string [i, j - i + 1]
# is palindromic
if (flag != 0 and
(j - i + 1) > maxLength):
start = i
maxLength = j - i + 1
# Return length of LPS
return maxLength
# Driver Code
# Given string
str = "forgeeksskeegfor"
# Function call
print(longestPalSubstr(str))
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG{
// Function to obtain the length of
// the longest palindromic substring
static int longestPalSubstr(string str)
{
// Length of given string
int n = str.Length;
// Stores the maximum length
int maxLength = 1, start = 0;
// Iterate over the string
for(int i = 0; i < str.Length; i++)
{
// Iterate over the string
for(int j = i; j < str.Length; j++)
{
int flag = 1;
// Check for palindrome
for(int k = 0;
k < (j - i + 1) / 2; k++)
if (str[i + k] != str[j - k])
flag = 0;
// If string [i, j - i + 1]
// is palindromic
if (flag != 0 &&
(j - i + 1) > maxLength)
{
start = i;
maxLength = j - i + 1;
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
public static void Main ()
{
// Given string
string str = "forgeeksskeegfor";
// Function call
Console.Write(longestPalSubstr(str));
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// JavaScript program for the above approach
// Function to obtain the length of
// the longest palindromic substring
function longestPalSubstr(str)
{
// Length of given string
var n = str.length;
// Stores the maximum length
var maxLength = 1, start = 0;
// Iterate over the string
for (var i = 0;
i < str.length; i++) {
// Iterate over the string
for (var j = i;
j < str.length; j++) {
var flag = 1;
// Check for palindrome
for (var k = 0;
k < (j - i + 1) / 2; k++)
if (str[i + k]
!= str[j - k])
flag = 0;
// If string [i, j - i + 1]
// is palindromic
if (flag
&& (j - i + 1) > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
// Given string
var str = "forgeeksskeegfor";
// Function Call
document.write( longestPalSubstr(str));
</script>
Time Complexity: O(N3), where N is the length of the given string.
Auxiliary Space: O(N)
Dynamic Programming Approach: The above approach can be optimized by storing results of Overlapping Subproblems. The idea is similar to this post. Below are the steps:
- Maintain a boolean table[N][N] that is filled in a bottom-up manner.
- The value of table[i][j] is true if the substring is a palindrome, otherwise false.
- To calculate table[i][j], check the value of table[i + 1][j - 1], if the value is true and str[i] is same as str[j], then update table[i][j] true.
- Otherwise, the value of table[i][j] is update as false.
Below is the illustration for the string "geeks":

Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the length of
// the longest palindromic substring
int longestPalSubstr(string str)
{
// Length of string str
int n = str.size();
// Stores the dp states
bool table[n][n];
// Initialise table[][] as false
memset(table, 0, sizeof(table));
// All substrings of length 1
// are palindromes
int maxLength = 1;
for (int i = 0; i < n; ++i)
table[i][i] = true;
// Check for sub-string of length 2
int start = 0;
for (int i = 0; i < n - 1; ++i) {
// If adjacent character are same
if (str[i] == str[i + 1]) {
// Update table[i][i + 1]
table[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
// Check for lengths greater than 2
// k is length of substring
for (int k = 3; k <= n; ++k) {
// Fix the starting index
for (int i = 0; i < n - k + 1; ++i) {
// Ending index of substring
// of length k
int j = i + k - 1;
// Check for palindromic
// substring str[i, j]
if (table[i + 1][j - 1]
&& str[i] == str[j]) {
// Mark true
table[i][j] = true;
// Update the maximum length
if (k > maxLength) {
start = i;
maxLength = k;
}
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
int main()
{
// Given string str
string str = "forgeeksskeegfor";
// Function Call
cout << longestPalSubstr(str);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the length of
// the longest palindromic subString
static int longestPalSubstr(String str)
{
// Length of String str
int n = str.length();
// Stores the dp states
boolean [][]table = new boolean[n][n];
// All subStrings of length 1
// are palindromes
int maxLength = 1;
for(int i = 0; i < n; ++i)
table[i][i] = true;
// Check for sub-String of length 2
int start = 0;
for(int i = 0; i < n - 1; ++i)
{
// If adjacent character are same
if (str.charAt(i) == str.charAt(i + 1))
{
// Update table[i][i + 1]
table[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
// Check for lengths greater than 2
// k is length of subString
for(int k = 3; k <= n; ++k)
{
// Fix the starting index
for(int i = 0; i < n - k + 1; ++i)
{
// Ending index of subString
// of length k
int j = i + k - 1;
// Check for palindromic
// subString str[i, j]
if (table[i + 1][j - 1] &&
str.charAt(i) == str.charAt(j))
{
// Mark true
table[i][j] = true;
// Update the maximum length
if (k > maxLength)
{
start = i;
maxLength = k;
}
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "forgeeksskeegfor";
// Function Call
System.out.print(longestPalSubstr(str));
}
}
// This code is contributed by Amit Katiyar
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to find the length of
// the longest palindromic subString
static int longestPalSubstr(String str)
{
// Length of String str
int n = str.Length;
// Stores the dp states
bool [,]table = new bool[n, n];
// All subStrings of length 1
// are palindromes
int maxLength = 1;
for(int i = 0; i < n; ++i)
table[i, i] = true;
// Check for sub-String
// of length 2
int start = 0;
for(int i = 0; i < n - 1; ++i)
{
// If adjacent character are same
if (str[i] == str[i + 1])
{
// Update table[i,i + 1]
table[i, i + 1] = true;
start = i;
maxLength = 2;
}
}
// Check for lengths greater than 2
// k is length of subString
for(int k = 3; k <= n; ++k)
{
// Fix the starting index
for(int i = 0; i < n - k + 1; ++i)
{
// Ending index of subString
// of length k
int j = i + k - 1;
// Check for palindromic
// subString str[i, j]
if (table[i + 1, j - 1] &&
str[i] == str[j])
{
// Mark true
table[i, j] = true;
// Update the maximum length
if (k > maxLength)
{
start = i;
maxLength = k;
}
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "forgeeksskeegfor";
// Function Call
Console.Write(longestPalSubstr(str));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program for the above approach
# Function to find the length of
# the longest palindromic subString
def longestPalSubstr(str):
# Length of String str
n = len(str);
# Stores the dp states
table = [[False for i in range(n)] for j in range(n)];
# All subStrings of length 1
# are palindromes
maxLength = 1;
for i in range(n):
table[i][i] = True;
# Check for sub-String of length 2
start = 0;
for i in range(n - 1):
# If adjacent character are same
if (str[i] == str[i + 1]):
# Update table[i][i + 1]
table[i][i + 1] = True;
start = i;
maxLength = 2;
# Check for lengths greater than 2
# k is length of subString
for k in range(3, n + 1):
# Fix the starting index
for i in range(n - k + 1):
# Ending index of subString
# of length k
j = i + k - 1;
# Check for palindromic
# subString str[i, j]
if (table[i + 1][j - 1] and str[i] == str[j]):
# Mark True
table[i][j] = True;
# Update the maximum length
if (k > maxLength):
start = i;
maxLength = k;
# Return length of LPS
return maxLength;
# Driver Code
if __name__ == '__main__':
# Given String str
str = "forgeeksskeegfor";
# Function Call
print(longestPalSubstr(str));
# This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript program for the above approach
// Function to find the length of
// the longest palindromic subString
function longestPalSubstr(str) {
// Length of String str
var n = str.length;
// Stores the dp states
var table = Array(n).fill().map(()=>Array(n).fill(false));
// All subStrings of length 1
// are palindromes
var maxLength = 1;
for (var i = 0; i < n; ++i)
table[i][i] = true;
// Check for sub-String of length 2
var start = 0;
for (i = 0; i < n - 1; ++i) {
// If adjacent character are same
if (str.charAt(i) == str.charAt(i + 1)) {
// Update table[i][i + 1]
table[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
// Check for lengths greater than 2
// k is length of subString
for (k = 3; k <= n; ++k) {
// Fix the starting index
for (i = 0; i < n - k + 1; ++i) {
// Ending index of subString
// of length k
var j = i + k - 1;
// Check for palindromic
// subString str[i, j]
if (table[i + 1][j - 1] && str.charAt(i) == str.charAt(j)) {
// Mark true
table[i][j] = true;
// Update the maximum length
if (k > maxLength) {
start = i;
maxLength = k;
}
}
}
}
// Return length of LPS
return maxLength;
}
// Driver Code
// Given String str
var str = "forgeeksskeegfor";
// Function Call
document.write(longestPalSubstr(str));
// This code is contributed by umadevi9616
</script>
Time Complexity: O(N2), where N is the length of the given string.
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Manacher's Algorithm. By using this algorithm, for each character c, the longest palindromic substring that has c as its center can be found whose length is odd. But the longest palindromic substring can also have an even length which does not have any center. Therefore, some special characters can be added between each character.
For example, if the given string is "abababc" then it will become "$#a#b#a#b#a#b#c#@". Now, notice that in this case, for each character c, the longest palindromic substring with the center c will have an odd length.
Below are the steps:
- Add the special characters in the given string S as explained above and let its length be N.
- Initialize an array d[], center, and r with 0 where d[i] stores the length of the left part of the palindrome where S[i] is the center, r denotes the rightmost visited boundary and center denotes the current index of character which is the center of this rightmost boundary.
- While traversing the string S, for each index i, if i is smaller than r then its answer has previously been calculated and d[i] can be set equals to answer for the mirror of character at i with the center which can be calculated as (2*center - i).
- Now, check if there are some characters after r such that the palindrome becomes ever longer.
- If (i + d[i]) is greater than r, update r = (i + d[i]) and center as i.
- After finding the longest palindrome for every character c as the center, print the maximum value of (2*d[i] + 1)/2 where 0 ? i < N because d[i] only stores the left part of the palindrome.
Below is the implementation for the above approach:
C++14
// C++ program for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function that placed '#' intermediately
// before and after each character
string UpdatedString(string s){
string newString = "#";
// Traverse the string
for(auto ch : s){
newString += ch;
newString += "#";
}
// Return the string
return newString;
}
// Function that finds the length of
// the longest palindromic substring
int Manacher(string s){
// Update the string
s = UpdatedString(s);
// Stores the longest proper prefix
// which is also a suffix
int LPS[s.length()] = {};
int C = 0;
int R = 0;
for (int i = 0 ; i < s.length() ; i++){
int imir = 2 * C - i;
// Find the minimum length of
// the palindrome
if (R > i){
LPS[i] = min(R-i, LPS[imir]);
}
else{
// Find the actual length of
// the palindrome
LPS[i] = 0;
}
// Exception Handling
while( ((i + 1 + LPS[i]) < s.length()) && ((i - 1 - LPS[i]) >= 0) && s[i + 1 + LPS[i]] == s[i - 1 - LPS[i]]){
LPS[i] += 1;
}
// Update C and R
if (i + LPS[i] > R){
C = i;
R = i + LPS[i];
}
}
int r = 0, c = -1;
for(int i = 0 ; i < s.length() ; i++){
r = max(r, LPS[i]);
if(r == LPS[i]){
c = i;
}
}
// Return the length r
return r;
}
// Driver code
int main()
{
// Given string str
string str = "forgeeksskeegfor";
// Function Call
cout << Manacher(str) << endl;
}
// This code is contributed by subhamgoyal2014.
Java
// Java code for the above approach
import java.util.Arrays;
class GFG {
// Function that placed '#' intermediately
// before and after each character
static String UpdatedString(String s) {
String newString = "#";
// Traverse the string
for (char ch : s.toCharArray()) {
newString += ch;
newString += "#";
}
// Return the string
return newString;
}
// Function that finds the length of
// the longest palindromic substring
static int Manacher(String s) {
// Update the string
s = UpdatedString(s);
// Stores the longest proper prefix
// which is also a suffix
int[] LPS = new int[s.length()];
int C = 0;
int R = 0;
for (int i = 0; i < s.length(); i++) {
int imir = 2 * C - i;
// Find the minimum length of
// the palindrome
if (R > i) {
LPS[i] = Math.min(R - i, LPS[imir]);
} else {
// Find the actual length of
// the palindrome
LPS[i] = 0;
}
// Exception Handling
while (((i + 1 + LPS[i]) < s.length()) && ((i - 1 - LPS[i]) >= 0) && (s.charAt(i + 1 + LPS[i]) == s.charAt(i - 1 - LPS[i]))) {
LPS[i] += 1;
}
// Update C and R
if (i + LPS[i] > R) {
C = i;
R = i + LPS[i];
}
}
int r = 0;
for (int i = 0; i < s.length(); i++) {
r = Math.max(r, LPS[i]);
}
// Return the length r
return r;
}
// Driver code
public static void main(String[] args) {
// Given string str
String str = "forgeeksskeegfor";
// Function Call
System.out.println(Manacher(str));
}
}
// This code is contributed by lokeshpotta20.
Python3
# Python program for the above approach
# Function that placed '#' intermediately
# before and after each character
def UpdatedString(string):
newString = ['#']
# Traverse the string
for char in string:
newString += [char, '#']
# Return the string
return ''.join(newString)
# Function that finds the length of
# the longest palindromic substring
def Manacher(string):
# Update the string
string = UpdatedString(string)
# Stores the longest proper prefix
# which is also a suffix
LPS = [0 for _ in range(len(string))]
C = 0
R = 0
for i in range(len(string)):
imir = 2 * C - i
# Find the minimum length of
# the palindrome
if R > i:
LPS[i] = min(R-i, LPS[imir])
else:
# Find the actual length of
# the palindrome
LPS[i] = 0
# Exception Handling
try:
while string[i + 1 + LPS[i]] \
== string[i - 1 - LPS[i]]:
LPS[i] += 1
except:
pass
# Update C and R
if i + LPS[i] > R:
C = i
R = i + LPS[i]
r, c = max(LPS), LPS.index(max(LPS))
# Return the length r
return r
# Driver code
# Given string str
str = "forgeeksskeegfor"
# Function Call
print(Manacher(str))
C#
// C# program to implement above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function that placed '#' intermediately
// before and after each character
static string UpdatedString(string s){
string newString = "#";
// Traverse the string
foreach(char ch in s){
newString += ch;
newString += "#";
}
// Return the string
return newString;
}
// Function that finds the length of
// the longest palindromic substring
static int Manacher(string s){
// Update the string
s = UpdatedString(s);
// Stores the longest proper prefix
// which is also a suffix
int[] LPS = new int[s.Length];
int C = 0;
int R = 0;
for (int i = 0 ; i < s.Length ; i++){
int imir = 2 * C - i;
// Find the minimum length of
// the palindrome
if (R > i){
LPS[i] = Math.Min(R-i, LPS[imir]);
}
else{
// Find the actual length of
// the palindrome
LPS[i] = 0;
}
// Exception Handling
while( ((i + 1 + LPS[i]) < s.Length) &&
((i - 1 - LPS[i]) >= 0) &&
s[i + 1 + LPS[i]] == s[i - 1 - LPS[i]]){
LPS[i] += 1;
}
// Update C and R
if (i + LPS[i] > R){
C = i;
R = i + LPS[i];
}
}
int r = 0;
for(int i = 0 ; i < s.Length ; i++){
r = Math.Max(r, LPS[i]);
}
// Return the length r
return r;
}
// Driver code
public static void Main(string[] args){
// Given string str
string str = "forgeeksskeegfor";
// Function Call
Console.WriteLine(Manacher(str));
}
}
// This code is contributed by entertain2022.
JavaScript
//Javascript code for the above approach
// Function that placed '#' intermediately
// before and after each character
function UpdatedString(string) {
let newString = ['#'];
// Traverse the string
for (let char of string) {
newString.push(char, '#');
}
// Return the string
return newString.join('');
}
// Function that finds the length of
// the longest palindromic substring
function Manacher(string) {
// Update the string
string = UpdatedString(string);
// Stores the longest proper prefix
// which is also a suffix
let LPS = new Array(string.length).fill(0);
let C = 0;
let R = 0;
for (let i = 0; i < string.length; i++) {
let imir = 2 * C - i;
// Find the minimum length of
// the palindrome
if (R > i) {
LPS[i] = Math.min(R-i, LPS[imir]);
} else {
// Find the actual length of
// the palindrome
LPS[i] = 0;
}
// Exception Handling
try {
while (string[i + 1 + LPS[i]] === string[i - 1 - LPS[i]]) {
LPS[i] += 1;
}
} catch (err) {
// pass
}
// Update C and R
if (i + LPS[i] > R) {
C = i;
R = i + LPS[i];
}
}
let r = Math.max(...LPS);
let c = LPS.indexOf(r);
// Return the length r
return r;
}
// Driver code
// Given string str
let str = "forgeeksskeegfor";
// Function Call
console.log(Manacher(str));
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
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