Articles for category: Data Structures and Algorithms

Reverse an Array

Problem Statement Given an array arr[] of length N, print the reverse of it. Example Consider the following example: After reversing this array,we have the new array as Example Explanation The initial array [1,2,3,4,5] when reversed gives [5,4,3,2,1]. Constraints $1 <= N <= 10^5$ There N is the length of the array. Approach 1- Recursive ...

Rearrange Array Alternately

Problem Statement We are provided with a sorted array of size n and we need to rearrange the array alternately. By rearrange array alternately, we should rearrange the array so that the first greatest element will come at the first position, and the first smallest element comes at the second position. Similarly, the second greatest element comes at ...

Serialize and Deserialize a Binary Tree

The need for data structures also demands the ability to convert them into a storable format and reconstruct back later. Serialization is the process of converting a data structure into a sequence of bits. And deserialization is the process of reconstructing the data structure from the serialized sequence. Introduction Data structures play a crucial role in designing efficient software, and ...

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