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
Programming Articles
Page 514 of 2547
Program to check robot can reach target by keep moving on visited spots in Python
Suppose we have a robot that is currently sitting at position (0, 0) on a Cartesian plane. If we have a list of moves containing N(North), S(South), W(West), and E(East), the robot has a special behavior: when it reaches a spot it has visited before, it continues moving in the same direction until it reaches a new unvisited spot. We need to check whether after all its moves, it will end at a target coordinate (x, y). ...
Read MoreProgram to find minimum number of rocketships needed for rescue in Python
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 MoreProgram to reverse linked list by groups of size k in Python
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 MoreProgram to reverse the directed graph in Python
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 MoreProgram to reverse a linked list in Python
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 MoreProgram to remove duplicate entries in a linked list in Python
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 MoreProgram to update elements in a given range in Python
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 MoreProgram 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 More