Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 511 of 2109
Program to find how many total amount of rain we can catch in Python
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 MoreProgram to count number of ways we can distribute coins to workers in Python
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 MoreProgram to count how many ways we can divide the tree into two trees in Python
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 MoreProgram to find leaf and non-leaf nodes of a binary tree in Python
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 MoreProgram to check whether inorder sequence of a tree is palindrome or not in Python
Suppose we have a binary tree where each node contains a digit from 0-9, we have to check whether its in-order traversal is palindrome or not. So, if the input is like 6 2 6 10 2 then the output will be True, ...
Read MoreProgram to count maximum number of distinct pairs whose differences are larger than target in Python
Suppose we have a list of numbers called nums and another value target. We have to find the maximum number of pairs where for each pair i < j, i and j are not in any other pair, and |nums[i] - nums[j]| >= target. So, if the input is like nums = [2, 4, 6, 10, 11], target = 5, then the output will be 2, as we can get pairs: (2, 10) and (4, 11). Algorithm To solve this problem, we will follow these steps ? Sort the array to enable efficient pairing Use ...
Read MoreProgram to fill with color using floodfill operation in Python
Suppose we have a 2D grid containing colors as strings "r", "g", and "b". We need to perform a floodfill operation at row r, column c with the target color. The floodfill operation replaces all elements that are connected to grid[r, c] (up/right/down/left) and have the same color as grid[r, c] with the target color. Example Input and Output If the input grid is: RRR RGB GBB Starting floodfill at position (0, 0) with target color "G", the output will be: GGG GGB GBB The red cells connected to grid[0, ...
Read MoreProgram to pack same consecutive elements into sublist in Python
Sometimes we need to group consecutive elements of the same value into sublists. This is useful for data analysis, compression, or pattern recognition tasks. Given a list like [5, 5, 2, 7, 7, 7, 2, 2, 2, 2], we want to create sublists for each group of consecutive identical elements: [[5, 5], [2], [7, 7, 7], [2, 2, 2, 2]]. Algorithm Steps The approach follows these steps ? If the input list is empty, return an empty list Initialize result with first element in its own sublist ...
Read MoreProgram to find one minimum possible interval to insert into an interval list in Python
Suppose we have a 2D list of numbers called intervals where each row represents [start, end] (inclusive) interval. For an interval [a, b] (a < b), its size is (b - a). We must add one interval to the given list such that, after merging all the intervals, we get exactly one range left. We have to find the minimum possible size of the added interval. So, if the input is like intervals = [[15, 20], [30, 50]], then the output will be 10, as we can add the interval [20, 30] which is the smallest possible interval. ...
Read MoreProgram to find minimum cost to reduce a list into one integer in Python
Suppose we have a list of numbers called nums. We can reduce the length of nums by taking any two numbers, removing them, and appending their sum at the end. The cost of doing this operation is the sum of the two integers we removed. We have to find the minimum total cost of reducing nums to one integer. So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 45. We take 2 and 3 then remove to get [4, 5, 6, 5], then we take 4 and 5 then remove ...
Read More