Articles for category: Data Structures and Algorithms

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

Detect Loop in Linked List

Problem Statement Given a linked list, check whether the linked list is having a loop or not(detect loop in linked list). A cycle exists in a linked list if it contains a node that may be accessed again by following the next pointer. Example Input: Output: True Explanation: linked list is having 6 nodes and the next ...

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

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

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