Articles for category: Data Structures and Algorithms

Diagonal Traversal of Binary Tree

Problem Statement Given a binary tree $A$ containing $n$ nodes, consider diagonals (lines with a slope of $-1$) passing between the nodes and print all the diagonal elements in the binary tree belonging to the same line. Note: The diagonals with a slope of $-45\text{ degree}$ angle, will be like a $\backslash$. This question has ...

Josephus problem

Problem Statement There is n number of people standing in a circle. Numbering starts from 1 to n and is in the fixed clockwise direction. In each step, we are supposed to eliminate a person which is at the $k^{th}$ position from the current position such that only one man remains at the end who ...

Inversion Count in an Array

The Inversion count in an array indicates the number of element pairs (i, j) where i < j and A[i] > A[j], serving as a measure of the array’s unsortedness. Understanding Inversion count in an Array is essential for analyzing sorting algorithms’ efficiency and optimizing them, making count inversion a key concept in algorithm design ...

Boyer Moore Algorithm

Problem Statement We are provided with an input pattern and a text (or string), and we need to search and print all the occurrences of the pattern in the string. Note: Use Boyer Moore’s algorithm for searching the pattern in the string. Refer to the Example and Explanation sections for more details and the Approach section to understand the working of the Boyer Moore algorithm. ...

Level Order Traversal in Spiral Form

Problem Statement Given a root node of a binary tree, print its level order traversal in spiral order. Example InputLet’s assume we have the following binary tree – Note that only the address of the root node is provided as the input. Output Example Explanation The simple level order traversal of the tree is – ...

Insertion in linked list

Modify a Linked List by inserting a new node at the front, after a specified node, and at the end. These operations offer flexibility in updating the structure based on specific needs, enabling dynamic adjustments to the Linked List. Insertion at the Beginning of the Linked List Insertion at the Beginning of the Linked List ...

Binary Coded Decimal Addition

BCD addition transforms the simple sum of two numbers, A and B, into a binary-coded marvel, concatenating each digit’s binary form from their sum into a precise, digital-friendly format. What is BCD? BCD, or Binary Coded Decimal, is a unique encoding system where every decimal digit is represented by its 4-bit binary counterpart. This method ...

Check for BST

Problem Statement Given a binary tree, we have to determine if the tree is a binary search tree or not.A tree is said to be a valid binary search tree when, Example consider the following examplesexample-1 output: example-2 output: Example Explanation In example 1,Condition 1 is not satisfied. All the nodes in the left subtree ...

Pattern Searching with Rabin-Karp Algorithm

The Rabin-Karp algorithm uses a hash function to search for and match patterns in text. Instead of going through every character in the initial phase as the Naive String Matching Algorithm does, it filters out the characters that don’t match before comparing. What is the Rabin-Karp Algorithm? The M-character subsequences of text to be compared and the ...