Suppose we have a list of numbers called weights representing people's weights and a value limit that determines the weight limit of one rocket ship. Each rocket ship can take at most two people. We need to find the minimum number of rocket ships required to rescue everyone. So, if the input is like weights = [300, 400, 300], limit = 600, then the output will be 2. One rocket ship takes the two people with weights 300 each (total 600), and another takes the person with weight 400. Algorithm Approach To solve this problem efficiently, we ... Read More
Suppose we have a singly linked list and another value k, we have to reverse every k contiguous group of nodes in the list. So, if the input is like List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3, then the output will be [3, 2, 1, 6, 5, 4, 9, 8, 7, 10]. Algorithm To solve this, we will follow these steps − Create a dummy node to simplify edge cases Use two pointers: one to track the previous group ... Read More
A directed graph reversal means changing the direction of all edges − if an edge goes from node u to v, it becomes an edge from v to u. Given an adjacency list representation, we need to create the reverse graph where all edge directions are flipped. Problem Understanding Consider a directed graph with nodes numbered from 0 to n-1. If the original graph has an edge from node i to node j, the reversed graph will have an edge from node j to node i. Original Graph ... Read More
A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a pointer to the next node. Reversing a linked list means changing the direction of pointers so that the last node becomes the first node. For example, if we have a linked list: 2 → 4 → 6 → 8, the reversed list will be: 8 → 6 → 4 → 2. Approach We can reverse a linked list using a recursive approach with these steps − Define a recursive function ... Read More
Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list. So, if the input is like [2 −> 4 −> 6 −> 1 −> 4 −> 6 −> 9], then the output will be [2 −> 4 −> 6 −> 1 −> 9]. Algorithm To solve this, we will follow these steps − if node is not null, then l ... Read More
Suppose we have a list of numbers called nums and a list of operations. Each operation has three fields [L, R, X], which indicates that we should increment by X all the elements from indices L to R (inclusive). We have to apply all operations and return the final list. So, if the input is like nums = [8, 4, 2, -9, 4] and operations = [[0, 0, 3], [1, 3, 2], [2, 3, 5]], then the output will be [11, 6, 9, -2, 4]. Step-by-Step Process Let's trace through the operations with the initial list [8, ... Read More
The rainwater trapping problem involves calculating how much water can be trapped between elevation bars after it rains. Given an array representing bar heights, we need to find the total trapped water volume. ... Read More
Suppose we have two lists of positive numbers called coins and salaries. Here coins[i] indicates the value for coin i and salaries[j] indicates the least amount of salary required to pay for worker j. We have one coin per type and must give each worker exactly one coin. We need to compute the number of ways to distribute coins to workers such that each worker receives a coin with value greater than or equal to their required salary. Two ways are different if some worker receives one type of coin in one way but a different type of coin ... Read More
Suppose we have a binary tree containing values 0, 1 and 2. The root has at least one 0 node and one 1 node. We need to find how many ways we can delete one edge such that the resulting two trees don't both contain 0 and 1 nodes. In other words, after deleting an edge, each resulting subtree should contain either only 0s and 2s, or only 1s and 2s, but not both 0s and 1s together. Example Tree Structure 0 0 ... Read More
In a binary tree, a leaf node has no children, while a non-leaf node has at least one child. This program counts both types of nodes using a recursive approach that traverses the entire tree. So, if the input is like: 6 2 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance