Articles for category: Data Structures and Algorithms

Unbounded Knapsack

Problem Statement Given a knapsack weight W and a set of N items with certain values(benefit or profit) val, and weights wt, find the maximum amount that could make up the exact knapsack weight.In Unbounded Knapsack, you may select an item multiple times. Example N = 4 W = 10 val[] = {15, 25, 20, ...

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

Subset Sum Problem

Problem Statement You have a collection of non-negative values, representing available options, and a non-negative target value. Your task is to determine whether there exists a subset within the collection whose sum equals the given target value. Example Example1:   Example2: Approach 1: Using Recursion We can approach the Subset Sum Problem recursively by considering ...

Static Data Structure

In this article, we are going to learn about the static data structure. Before getting started with the static data structure let us get a short overview of what is a data structure and which data structure is of type static. Data Structure: A computer can contain an abundant amount of data, so a Data Structures. helps in organizing those ...

Sort an Array of 0s 1s and 2s

Problem Statement We are given an array consisting of only 0s, 1s, and 2s. Our task is to write a program to sort the given array without using inbuilt functions. Example Example Explanation Since we already know that an array is a linear data structure that contains data elements of the same data type at ...