Server Side Programming Articles

Page 294 of 2109

Program to find minimum cost to merge stones in Python

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

The minimum cost to merge stones problem involves merging K consecutive piles of stones into one pile, where the cost equals the total stones in those K piles. We need to find the minimum cost to merge all piles into a single pile. This is a classic dynamic programming problem that uses interval DP to find the optimal way to merge stone piles. Understanding the Problem Given N piles of stones and a merge factor K, we can only merge exactly K consecutive piles at a time. The key insight is that for a solution to exist, ...

Read More

Program to find number of squareful arrays in Python

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

Given a target string and a stamp, we need to find the sequence of stamp positions that can transform a string of question marks into the target string. This is a reverse engineering problem where we work backwards from the target to find valid stamping positions. Problem Understanding We start with a sequence of '?' marks and use a stamp to gradually build the target string. The key insight is to work backwards − start from the target string and figure out which stamp positions could have created it. Algorithm Steps The algorithm follows these steps ...

Read More

Program to find number of distinct subsequences in Python

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

Given a string s, we need to count the number of distinct subsequences in the string. A subsequence is formed by deleting some (possibly zero) characters from the original string while maintaining the relative order of remaining characters. If the answer is large, return the result modulo 10^9 + 7. For example, if the input is s = "bab", the output will be 6 because there are 6 different subsequences: "" (empty), "b", "a", "ba", "ab", and "bab". Algorithm We use dynamic programming to solve this problem efficiently ? Create a dp array of size ...

Read More

Program to check existence of edge length limited paths in Python

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

Given an undirected weighted graph with n nodes and an edgeList where each edge is represented as (u, v, w) indicating a path from node u to node v with distance w. We also have queries in the format (p, q, lim) asking whether there exists a path from node p to node q with total distance less than lim. This problem can be efficiently solved using the Union-Find (Disjoint Set Union) data structure combined with sorting techniques. Algorithm Overview The approach involves: Sort edges by weight in ascending order Sort queries by their limit ...

Read More

Program to minimize deviation in array in Python

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

Suppose we have an array nums. We can perform two types of operations on any element of the array any number of times ? For even elements, divide it by 2 For odd elements, multiply it by 2. The deviation of the array is the maximum difference between any two elements in the array. We have to find the minimum deviation the array can have after performing some number of operations. So, if the input is like nums = [6, 3, 7, 22, 5], then the output will be 5 because we can make our ...

Read More

Program to distribute repeating integers in Python

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

When distributing items to customers, we need to ensure each customer gets exactly the quantity they ordered, with all items being identical. This problem involves checking if we can satisfy all customer demands using available items. The approach uses backtracking with frequency counting. We count how many times each frequency appears, then try to allocate items to customers in descending order of demand. Algorithm Steps Here's how the solution works ? Count frequency of each unique number in the array Count frequency of frequencies (how many numbers ...

Read More

Program to find number of ways to form a target string given a dictionary in Python

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

Suppose we have a list of strings called words, where all elements are of the same length. We also have a string called target. We have to generate target using the given words under the following rules: We should generate target from left to right. To get the ith character (0-indexed) of target, we can select the kth character of the jth string in words when target[i] is same as words[j][k]. Once we use the kth character of the jth string of words, we cannot use the xth character of any string in words where x

Read More

Program to find minimum one bit operations to make integers zero in Python

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

We need to transform a number n into 0 using specific bit operations. The operations allowed are: Select the rightmost bit in the binary representation of n. Change the ith bit in the binary representation of n when the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. We need to find the minimum number of operations required to transform n into 0. Understanding the Problem For example, if n = 6, the binary representation is "110". The transformation steps are ? Start: "110" (6 ...

Read More

Program to find out how many transfer requests can be satisfied in Python

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

Suppose there are n hostel rooms numbered from 0 to n-1. Students want to transfer to different rooms and place transfer requests. Since no room can remain vacant, a transfer is only satisfied if another student takes the transferring student's place. We need to find the maximum number of requests that can be satisfied simultaneously. Problem Understanding For a set of transfer requests to be valid, the number of students leaving each room must equal the number of students entering that room. This creates a balanced system where no room becomes empty or overcrowded. Example If ...

Read More

Program to find out the shortest path to reach the goal in Python

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

Finding the shortest path in a grid is a classic problem that can be solved using Breadth-First Search (BFS). In this problem, we need to navigate through a grid where different symbols represent different cell types and find the minimum moves to reach the goal. Grid Symbol Meanings '#' is the goal cell that we want to reach 'O' is a free cell via which we can travel to the goal cell '*' is our current position in the grid 'X' is a blocked cell, via which we cannot travel Problem Example Given the ...

Read More
Showing 2931–2940 of 21,090 articles
« Prev 1 292 293 294 295 296 2109 Next »
Advertisements