Articles for category: Data Structures and Algorithms

Remove Duplicates from Array

Problem Statement Given an array of integers, we have to remove duplicates from array such that each element in the array appears only once (all the values in the array should become unique). Example Input : [1,2,2,3,4,4,4,5,5] Output : [1,2,3,4,5] Explanation As we can see the frequency of all the elements Element Frequency 1 1 2 2 3 1 ...

Sliding Window Algorithm

Sliding Window Algorithm is a technique for reducing the complexity of algorithms. It is used such that the need for reusing the loops gets reduced and hence the program gets optimized. In this technique, we reuse the result of the previous step to compute the result of the next step. What is Sliding Window Algorithm? It ...

kth Smallest Element

Problem Statement We are given an integer array nums and an integer k. We have to return the kth smallest element in the array where $k <=$ size of nums. Also, note that we have to return the $kth$ smallest element not the kth distinct element. Example Example Explanation If we sort the above array, ...

Sieve of Eratosthenes

The Sieve of Eratosthenes is a classic method for efficiently finding prime numbers up to a given limit, crucial in cryptography for securing data like credit card numbers. This ancient algorithm sieves out non-prime numbers, leaving behind the primes. By understanding this algorithm, we unlock a powerful tool for various mathematical applications, from fraction conversion ...

Circular Linked List

Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or a doubly circular linked list. Before reading this article, you should have some understanding of the following Data Structures topics: ...

Binary Exponentiation

Binary Exponentiation is a technique of computing a number raised to some quantity, which can be as small as $0$ or as large as $10^{18}$ in a fast and efficient manner. It utilises the property of exponentiation and the fact that any number can be represented as sum of powers of two depending on its ...

String in Data Structure

A string is generally considered as a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. Scope Takeaways Library Functions Introduction A String is seen anywhere and everywhere. Right from when you login onto your device, you ...

Z Algorithm for Pattern Searching

Z algorithm is an algorithm for searching a given pattern in a string. It is an efficient algorithm as it has linear time complexity. It has a time complexity of O(m+n), where m is the length of the string and n is the length of the pattern to be searched. Introduction to the Z Algorithm ...

Dijkstra’s Algorithm

Dijkstra Algorithm is a graph algorithm for finding the shortest path from a source node to all other nodes in a graph(single source shortest path). It is a type of greedy algorithm. It only works on weighted graphs with positive weights. It has a time complexity of $O(V^2)$ using the adjacency matrix representation of graph. ...

Dynamic Programming

The dynamic programming algorithm tries to find the shortest way to a solution when solving a problem. It does this by going from the top down or the bottom up. The top-down method solves equations by breaking them into smaller ones and reusing the answers when needed. The bottom-up approach solves equations by breaking them ...