Articles for category: Data Structures and Algorithms

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

Find Duplicates in Array

Problem Statement Given an array A of size N + 1. Each element in array A is between 1 and N. Write a program to find one of the duplicates in array A. If there are multiple answers, then print any. Example 1 Input:N = 5A = [1, 2, 5, 3, 4, 3]Output3 ExplanationIn this ...

Palindrome Linked List

Problem Statement We are provided with a linked list (singly linked list) and we need to check whether the provided linked list is a palindrome linked list or not. Now, palindrome means any word or sequence that reads the same forwards as backward. For example mom is a palindrome as it reads the same forwards ...

Postorder Traversal without Recursion

Problem Statement Given a tree, we need to traverse the tree in a post-order traversal way and print the nodes of the tree in the post-order traversal order. The traversal should be done without using recursion. In order to proceed with the problem, we need to understand how post-order traversal is done over the tree. ...

Zero Sum Subarrays

Problem Statement Given an integer array where the array has both negative and positive values, print all the subarrays from the given array whose sum is zero. Example Let’s consider the below-given integer array: Input : arr = [0, -1, 8, -5, -3, -7, 4, 2, 1] Output for the given array is: Explanation Subarrays formed ...

Worst Case of QuickSort

QuickSort is a widely used and efficient sorting algorithm in the field of Data Structures and Algorithms (DSA). However, like any algorithm, it has its worst-case scenarios that can significantly impact its performance. The worst-case scenario for QuickSort occurs when the chosen pivot element consistently leads to unbalanced partitions during each recursive step of the ...