# Longest palindromic substring length of a given string (recursion and dynamic programming approach)

A substring is a string formed based on a continuous subset of characters of another big series. Let’s see the following example. As we do in the longest palindromic subsequence length, we have a similar formula here with a small condition. The condition here is the length of substring whichever formed should be equal toRead More »

# Count of all subsets with given sum in an unsorted integer array

Given an array of unsorted integers of size ‘N,’ we aim to find all subsets count with sum is equivalent to a given sum. A subset array is an array in which all the items on it exists in another bigger array. Here the array is called subset to the larger array. To find theRead More »

# Topological sort of an acyclic directed graph

Topological sorting forms with the ordering of all its vertices topologically. A topological ordering of any edge “U–>V” in an acyclic directed graph starts with “U” then “V”. For example we can think of any processes dependency in which task ‘U’ needs to finish first to execute process ‘V’. We cannot do topological sorting onRead More »

# Best complexity edit distance algorithm with an example

Edit distance The minimum number of operations on a string (array of characters) to convert into another input string is called the edit distance between the two strings. The operations can be Insert a character, Remove a character, or Replace a character with another. We can solve this problem using recursion. Now let’s take aRead More »

# K-th largest or smallest element in an unsorted array of integers

Given an input of ‘N’ unsorted integers in which we need to find the given K-th largest or smallest integer. This is one of the most commonly asked interview questions asking in recent days. As there are multiple ways to solve this problem, in this article I am discussing two optimized solutions for this problem.Read More »

# Find the maximum subarray sum in an unsorted integer array

Given an array of unsorted integers with both negative and positive values. We aim to find a subarray in which the sum of all integers is maximum than any other subarray of the given array. Let’s understand this with the below example image. The above input array has integers of size 7 and its maximumRead More »

# Find the median of an unsorted stream of integers at any given time

An unsorted integer stream is coming, we need to always keep the median of all the stream elements. The median can be defined as follows. To find the median of any given array, Sort the array Find integer(s) in the middle of the sorted array The average of middle these integers is median ( IfRead More »

# Reverse alternate group of K nodes in a singly linked list

Consider we have a singly linked list of “N” nodes, in which we aim to reverse alternate “K” nodes. Let’s see the below pictorial representation of a singly linked list. Here, our input linked list of size 7 nodes is 10->20->30->40->50->60->70->NULL and we want to reverse every alternative 3 (K) nodes group, pictorial representation ofRead More »

# Find root to leaf path whose sum is maximum in a binary tree

Given a binary tree with all positive integer nodes, we need to find the path from the root to a leaf whose sum is maximum in all root to leaf paths of the binary tree. The above diagram shows a simple binary tree with the maximum sum path [24, 16, 12]. This algorithm needs anRead More »

# Print binary tree nodes in vertical order (vertical order traversal)

A vertical order traversal is one of the binary tree traversals, where all elements are vertically at the same level, displays as an array of elements. If someone looks at a 3D model of the binary tree from bottom to top way, all the rear nodes which are hidden by front nodes, come under theRead More »

# Find verticals sum of binary tree

Given a binary tree with integer nodes in it, the aim is to find verticals sum. Let’s discuss this with the below example. As shown in the image above, each vertical level has an index, like {-2, -1, 0, 1, 2} in the above picture. The result should be the sum of all the nodesRead More »

# Lowest common ancestor

Given a binary tree and two integer nodes on it, the aim is to find the lowest common ancestor of the nodes in a given binary tree. A lowest common ancestor is the lowest common parent node of the respective nodes. Let’s check this with a couple of examples. We can solve this problem recursively,Read More »

# Find mirror of a binary tree

Given a binary tree, we aim to find its mirror tree. A Mirror tree is a binary tree where it’s left and right subtrees are swaps its positions. A recursive definition of the mirror tree is a binary tree T(P, L, R) is T(P, T(R), T(L)). Here both L(left) and R(right) subtrees swapped their positionRead More »

# Find the diameter of a given binary tree

The diameter of a binary tree is the longest distance between two leaf nodes. These leaf nodes are nodes from the root node or leaf nodes of any subtrees’ of the binary tree. Let’s see below two example binary trees diameter At any specific node it calculates as the sum of heights of left childRead More »

# Convert binary tree to its sum tree in-place

A binary tree with integer nodes is given, we aim to convert this binary tree to its sum tree in-place. A Sum Tree is a binary tree in which the value of the current node is a sum of data of all its descendent nodes data. Let’s see below the binary tree and its sumRead More »