Articles for category: Data Structures and Algorithms

Preorder to Postorder Traversal

Problem Statement Given an array that represents the preorder traversal of a binary search tree, write a program to find the postorder traversal of the BST. Example The preorder of a BST is given below:preorder[] = {10, 5, 8, 25, 47}The postorder for the example will be:postorder[] = {8, 5, 47, 25, 10} Example Explanation ...

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 ...

Check If Two Strings are Anagram

An anagram string is a rearrangement of the letters of a given string to form another word or phrase, using all the original letters exactly once. For example, “listen” and “silent” are string anagrams of each other. In this article, we will be delving into what is anagram string. Problem Statement Given two strings consisting ...

What is Number System?

A number system, or numeral system, is a method for representing numbers using symbols, crucial in mathematics and programming for data representation. It involves using digits to construct numbers, where each digit’s value is determined by its position and the base value of the system. While computers primarily use the binary system of 0s and 1s, ...

Implementation of Stack using Array

Stacks is a key data structure in modern programming that facilitate efficient software development with Last In First Out (LIFO) principle. Stack is created using one-dimensional arrays. This method involves a ‘top’ variable, which tracks the latest element, ensuring that the last added item is the first to be removed. This article delves into implementing ...

Merge Two Sorted Arrays without Extra Space

Problem Statement Given the sorted arrays nums1[]nums1[] and nums2[]nums2[] of sizes n and m in non-decreasing order. Merge without extra space in sorted, non-decreasing order such that nums1 contains the first n elements, and nums2 contains the last m elements. Example Example Explanation The following two arrays are given : Try to merge two sorted arrays without extra space such that the smallest n elements are present in the first array and the ...

Naive String Matching Algorithm

Problem Statement The problem states that we are given a text string (let’s say text) having the length n and a pattern string (let’s say pattern) having the length m. We need to write a function that takes these two strings (pattern, and text) and prints all the occurrences of the pattern string in the text string. Note: ...

Morris Traversal

Problem Statement Given a binary tree, we need to traverse the tree (visit and print the nodes of the need) without using a stack or recursion. Note: Use the Morris traversal to traverse the binary tree without using any extra data structure and recursion. Example Input tree is: Output: The Morris traversal (in order) of the ...

Longest Common Substring

Problem statement: Given two strings string_1 and string_2 of size N and M respectively. Find the length of the longest common substring. The problem statement says that we are given two strings and we need to return the length of the longest common substring from the given strings. For example: Input: Output: Explanation: The longest common substring from string_1 and string_2 of lengths N ...