Articles for category: Data Structures and Algorithms

Stack Operations

A stack is an abstract data type (ADT) which is used to store data in a linear fashion. A stack only has a single end (which is stack’s top) through which we can insert or delete data from it. There are many different stack operations which can be applied on a stack. We will discuss ...

Activity Selection Problem

We are given N activities with their start time and finish time. Our task is to find the maximum number of non-conflicting activities that can be performed within a given time assuming that only a single activity can be performed at a time. Scope In this article we are looking upon Activity selection problem. In ...

Maximum Subarray Sum

Problem Statement We are given an array A of size N which consists of both positive and negative integers. We have to write a program to find the Maximum Sum of Subarray. Note: A subarray is a contiguous part of an Array. Example Input: [2,-3,6,-5,4,2] Output: 7 Explanation: The subarray [6,-5,4,2] is the max sum ...

Median of Two Sorted Arrays

Median of Two Sorted Arrays of Same Size Before getting into the problem statement of finding the median of two sorted arrays, let us first get a brief introduction about the arrays. An array is a linear collection of values stored at contiguous memory locations. Array stores homogeneous values(similar data type values). To learn more ...

Longest Palindromic Subsequence

Problem Statement Given a string ‘S’, your objective is to identify the length of the longest palindromic subsequence. In simpler terms, you’re tasked with finding the longest sequence of characters in ‘S’ that can be read the same forwards and backwards, but not necessarily in consecutive order. Example Approach-1: Using Recursive Solution Dry Run: Code ...

Merge K Sorted Linked Lists

How to merge K-sorted linked lists Given K sorted linked lists, merge them in a sorted manner and return the head of the linked list. Explanation: All elements from the sorted lists are merged in a sorted manner and the head of the sorted list is returned. Constraints : Approach 1(Simple): KaTeX parse error: $ ...

Minimum Number of Jumps to Reach End

You are given with the array of positive integers and asked to find the minimum number of steps or jumps required to reach the end. Takeaways Excellent problem to learn time and space complexity optimization in dynamic programming problem. Problem Statement The problem statement that is given to us, is — You are given an array ...

Missing Number in Array

You are given with the array of length n-1 and elements ranging from 1 to n, you have to figure out the missing number. Scope This article tells how to find the missing number in the array In this article we will learn finding missing number using mathematical approch In this article we will learn ...

Detect Cycle in Undirected Graph

Given an undirected graph containing ‘n’ nodes and ‘e’ edges. Determine whether or not the graph contains a cycle. Takeaways Worst-case time complexity of Depth first search for detecting the cycle in undirected graph is O(n) where n is the number of nodes. Example of problem. Consider the following image. It shows an undirected graph with a cycle. Now have a look at ...