Server Side Programming Articles

Page 486 of 2109

Program to check some elements in matrix forms a cycle or not in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 283 Views

A matrix cycle exists when we can start from a cell, move through adjacent cells with the same value, and return to the starting position without revisiting the previous cell. This problem uses depth-first search (DFS) to detect such cycles. Problem Understanding Given a 2D matrix, we need to check if we can form a cycle by moving through adjacent cells (up, down, left, right) that have the same value. The key constraint is that we cannot revisit the cell we just came from. For example, consider this matrix ? 2 2 2 1 ...

Read More

Program to count how many ways we can cut the matrix into k pieces in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 291 Views

Suppose we have a binary matrix and a value k. We want to split the matrix into k pieces such that each piece contains at least one 1. The cutting rules are: Select a direction: vertical or horizontal Select an index in the matrix to cut into two sections If we cut vertically, we can no longer cut the left part but can only continue cutting the right part If we cut horizontally, we can no longer cut the top part and can only continue cutting the bottom part We need to find the number of ...

Read More

Program to find maximum possible population of all the cities in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 752 Views

Consider a country represented as a tree with N nodes and N-1 edges. Each node represents a town, and each edge represents a bidirectional road. We need to upgrade some towns into cities following these constraints: no two cities should be adjacent, and every town must be adjacent to at least one city. Our goal is to find the maximum possible population of all upgraded cities. Problem Understanding Given source and dest arrays representing road connections, and a population array for each town, we need to select nodes (upgrade to cities) such that ? No two ...

Read More

Program to count subsets that sum up to k in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 547 Views

Suppose we have a list of numbers called nums and another value k, we have to find the number of subsets in the list that sum up to k. If the answer is very large then mod this with 10^9 + 7. So, if the input is like nums = [2, 3, 4, 5, 7] k = 7, then the output will be 3, as we can choose the subsets [2, 5], [3, 4] and [7]. Algorithm To solve this, we will follow these steps − dp := a list of ...

Read More

Program to check given point in inside or boundary of given polygon or not in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 957 Views

Suppose we have a list of cartesian points [(x1, y1), (x2, y2), ..., (xn, yn)] representing a polygon, and also have two values x and y. We need to check whether point (x, y) lies inside this polygon or on the boundary. So, if the input is like points = [(0, 0), (1, 3), (4, 4), (6, 2), (4, 0)] and pt = (3, 1): ...

Read More

Program to check how many ways we can choose empty cells of a matrix in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 295 Views

Suppose we have an N x N binary matrix where 0 represents empty cells and 1 represents blocked cells. We need to find the number of ways to choose N empty cells such that every row and every column has at least one chosen cell. If the answer is very large, return the result mod 10^9 + 7. Problem Understanding Given a matrix like this: .cell { fill: white; stroke: #333; stroke-width: 1; } .blocked { ...

Read More

Program to find minimum number of buses required to reach final target in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 436 Views

Suppose we have a n x 3 matrix where each row contains three fields [src, dest, id] − this means the bus has route from src to dest. It takes one unit of money to get on a new bus, but if we stay on the same bus we have to pay one unit only. We have to find the minimum cost necessary to take the bus from location 0 to the final stop (largest location). If there is no solution return -1. Problem Example Consider the following bus routes ? Source Destination Bus ID ...

Read More

Program to evaluate one mathematical expression without built-in functions in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

Sometimes we need to evaluate mathematical expressions without using Python's built-in eval() function. This requires parsing the expression manually while respecting operator precedence rules. So, if the input is like s = "2+3*5/7", then the output will be 4, as 2 + ((3 * 5) / 7) = 4. Algorithm Overview To solve this, we follow these steps − Reverse the input string to process from right to left Create a get_value() function to parse numbers and handle signs Create a get_term() function to handle multiplication and division (higher precedence) In the main function, handle ...

Read More

Program to find maximum profit we can make after k Buy and Sell in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 294 Views

Stock trading problems are common in dynamic programming. Given a list of stock prices and a maximum number of transactions k, we need to find the maximum profit possible. Each transaction consists of buying and then selling a stock. So, if the input is like prices = [7, 3, 5, 2, 3] and k = 2, then the output will be 3. We can buy at price 3, sell at 5 (profit = 2), then buy at 2 and sell at 3 (profit = 1), giving us total profit of 3. Approach We'll use dynamic programming with ...

Read More

Program to find minimum cost to reach final index with at most k steps in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 301 Views

Suppose we have a list of numbers nums and another value k. The items at nums[i] represent the costs of landing at index i. We start from index 0 and need to reach the last index of nums. In each step we can jump from position X to any position up to k steps away. We need to minimize the sum of costs to reach the last index. So, if the input is like nums = [2, 3, 4, 5, 6] and k = 2, then the output will be 12, as we can select the path: 2 + ...

Read More
Showing 4851–4860 of 21,090 articles
« Prev 1 484 485 486 487 488 2109 Next »
Advertisements