Python Articles

Page 336 of 855

Program to find out special types of subgraphs in a given graph in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 558 Views

A special graph is defined as having exactly one "head" vertex connected to exactly t "feet" vertices. Given an undirected, unweighted graph, we need to find how many such special subgraphs can be formed where each subgraph uses disjoint vertices (no vertex appears in multiple subgraphs). The key insight is that feet vertices have degree 1 (connected only to their head), while head vertices have degree equal to the number of feet they connect to. Special Graph Structure Head ...

Read More

Program to find out the path between two vertices in a graph that has the minimum penalty (Python)

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 414 Views

When working with weighted graphs, sometimes we need to find a path that minimizes a specific cost function. In this problem, we find the path between two vertices where the penalty is minimized. The penalty of a path is the bitwise OR of all edge weights in the path. Given an undirected, weighted graph, we need to find the minimum penalty path from node a to node b. If no path exists, we return -1. Problem Example 1 2 ...

Read More

Program to find out the minimum size of the largest clique in a graph (Python)

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 280 Views

A clique in a graph is a subset of vertices where every pair of vertices is connected by an edge. Finding the largest clique is computationally challenging, but we can determine the minimum size of the largest clique using a binary search approach with edge counting. The problem asks: given the number of nodes and edges in a graph, what is the minimum possible size of the largest clique? Understanding the Problem If we have a graph with n nodes and e edges, we need to find the smallest value k such that any graph with these ...

Read More

Program to find number of ways to split array into three subarrays in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 798 Views

Suppose we have an array called nums, we have to find the number of good ways to split this array. A split is considered good if the array is divided into three non-empty contiguous subarrays where sum(left) ≤ sum(middle) ≤ sum(right). Since the answer may be large, return result modulo 10^9 + 7. Problem Understanding If the input is nums = [2, 3, 3, 3, 7, 1], then the output will be 3 because there are three different ways of splitting − [2], [3], [3, 3, 7, 1] → sums: 2 ≤ 3 ≤ 11 [2], ...

Read More

Program to find out an MST using Prim's algorithm in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

A Minimum Spanning Tree (MST) is a subset of edges in a weighted graph that connects all vertices with the minimum possible total edge weight and no cycles. Prim's algorithm builds the MST by starting from a vertex and greedily adding the minimum weight edge that connects a visited vertex to an unvisited one. Original Graph ...

Read More

Program to find out the minimum moves in a snakes and ladders game in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

The Snakes and Ladders game requires finding the minimum number of dice rolls to reach position 100 from position 1. In this problem, we can roll any number from 1 to 6 on the dice, and the board contains ladders (which help us climb up) and snakes (which make us slide down). We'll use a Breadth-First Search (BFS) approach to find the shortest path, as BFS guarantees finding the minimum number of moves in an unweighted graph. Problem Understanding Given the positions of ladders and snakes: Ladders: [(start, end)] where end > start (move forward) ...

Read More

Program to count good meals with exactly two items in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 343 Views

Suppose we have an array deli where deli[i] is the deliciousness of the ith food. We need to find the number of different good meals we can make from this list. A good meal contains exactly two different food items with a sum of deliciousness that is a power of two. If the answer is too large, return the result modulo 10^9 + 7. So, if the input is like deli = [1, 7, 3, 6, 5], then the output will be 3 because we can make pairs (1, 3), (1, 7) and (3, 5) whose sums are powers ...

Read More

Program to find maximum number of eaten apples in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 435 Views

Suppose we have two arrays called apples and days of same length n. There is a special kind of apple tree that grows apples every day for n consecutive days. On the ith day, it grows apples[i] number of apples that will rot after days[i] days. We can take at most one apple a day and want to find the maximum number of apples we can eat. So, if the input is like apples = [1, 2, 3, 5, 2] and days = [3, 2, 1, 4, 2], then the output will be 7 because ? On ...

Read More

Program to find Reordered Power of 2 in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 215 Views

Given a positive integer N, we need to check if we can reorder its digits to form a power of 2. The reordered number must have no leading zeros. For example, if N = 812, we can rearrange it to 128, which is 2^7, so the answer is True. Algorithm To solve this problem, we will follow these steps − Start with i = 1 (which is 2^0) While i ≤ 1000000000, do the following: Convert i to string and sort its digits Convert N to string and sort its digits If both sorted ...

Read More

Program to find maximum binary string after change in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 535 Views

Given a binary string, we can apply two types of operations any number of times to maximize its numeric value: Replace substring "00" with "10" Replace substring "10" with "01" We need to find the maximum possible binary string after applying these operations. Understanding the Problem The key insight is that we want to move as many 1s as possible to the left (higher positions) while keeping at least one 0 in the string. The operations allow us to: "00" → "10": Creates a 1 to the left of a 0 "10" ...

Read More
Showing 3351–3360 of 8,546 articles
« Prev 1 334 335 336 337 338 855 Next »
Advertisements