Server Side Programming Articles

Page 296 of 2109

Program to construct the lexicographically largest valid sequence in Python

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

Suppose we have a number n, we have to find a sequence that satisfies all of the following rules ? 1 occurs once in the sequence. Each number in between 2 and n occurs twice in the sequence. For every i in range 2 to n, the distance between the two occurrences of i is exactly i. The distance between two numbers in the sequence, a[i] and a[j], is |j - i|. We have to find the lexicographically largest sequence. So, if the ...

Read More

Program to find out the length between two cities in shortcuts in Python

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

Finding the shortest path using shortcuts between cities is a graph theory problem. We have highways connecting some cities, and shortcuts exist between cities that are not directly connected by highways. Our goal is to find the minimum distances using only shortcuts from a starting city to all other cities. Problem Understanding Given a graph where edges represent highways, shortcuts exist between vertices that are not connected by highways. We need to find shortest paths using only these shortcuts ? 1 2 ...

Read More

Program to find out the minimum path to deliver all letters in Python

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

Suppose there are n cities connected with n-1 roads, forming a tree structure where any city can be reached from any other city. A postal worker needs to deliver k letters daily to different destination cities. We need to find the minimum distance the worker must travel to deliver all letters, starting from any city. Problem Understanding Given a tree of cities and a list of delivery destinations, we need to find the shortest path that visits all destination cities. The key insight is that in a tree, we need to traverse edges optimally — some edges might ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 559 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
Showing 2951–2960 of 21,090 articles
« Prev 1 294 295 296 297 298 2109 Next »
Advertisements