Articles for category: Data Structures and Algorithms

Ugly Numbers

Problem Statement We are provided with a number n. We need to print the n-th ugly number. An ugly number is a number whose prime factors are 2, 3, or 5 only. Note: $1$ is considered as an ugly number (conventionally). Refer to the Example and Explanation sections for more details and the Approach section ...

Sort a Linked List

A linked list is a sequential collection of data elements connected via links. The data element of a linked list is known as a node which contains two parts namely- the data part and the pointer. For sorting a linked list, we can use the insertion sort-based algorithm as well as the merge sort algorithm. The insertion sort-based algorithm uses ...

Skip List in Data structure

Skip list data structure is an extension to a sorted linked list; it provides indexing and querying data with reduced time complexity because there is a concept of skipping nodes while exploring data, somewhat similar to binary search. Each node contains a few forward pointers (not just one or two as in a linked list), therefore ...

Time and Space Complexity of Selection Sort

Time complexity measures the execution time of an algorithm’s instructions. For the selection sort, the average, worst-case, and best-case time complexity of the selection sort are all $O(N^2)$, and the space complexity is O(1), indicating it requires a constant amount of extra storage space. Time Complexity of Selection Sort The Time complexity of Selection Sort ...

Tower of Hanoi Algorithm

The Tower of Hanoi problem consists of three rods (for example A, B, and C) and N disks initially stacked on rod A in decreasing order of diameter (smallest one is at the top). The goal is to transfer the entire stack to another rod (in this case, rod C) with the help of auxiliary rod(in this ...

Subsequence of a String

Problem Statement You are given a string as an input. The task is to print all the possible subsequence of a string in any order. Note: A subsequence is a string derived from the input string by deleting zero or more elements from the input string without changing the order of the remaining characters. Example: str=”abcd” Example Let us ...

Searching in Data Structure

Searching in data structures involves finding the location of a specific element or value within a collection. It is a fundamental operation that can greatly influence data handling efficiency in applications like databases and search engines. There are various types of searching in data structure, the most common being Linear Search and Binary Search. Types of ...

Trailing Zeros in Factorial

Problem Statement Given an integer n, our task is to write a function that returns the count of trailing zeros in n!. Here n! is the factorial of a number n which is the product of all numbers in the range $[1, n]$. Example : Explanation Example – 1 :When $n = 3$, Factorial of ...

Transpose of a Matrix

Problem Statement Given a 2D matrix having $n$ rows and $m$ number of columns. Print a transpose of the given matrix. The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns. Example Let us have a look at some examples on how to transpose a matrix. Example1 ...