Articles for category: Data Structures and Algorithms

Hamiltonian Cycle

What is Hamiltonian Cycle? Hamiltonian Cycle or Circuit is a path in an undirected graph that visits all the vertices in the graph exactly once and terminates back at the starting node. Problem Statement Given an undirected graph, our task is to determine whether the graph contains a Hamiltonian cycle or not. Example Explanation In the above example, ...

Digit DP

Digit Dp as the name suggests, is a technique of dynamic programming wherein we use the digits of the numbers to reach the final solution. This technique solves those problems which concern the digits between two specified integers. To find the solution, we try building all the integers between the two given integers and compute ...

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

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