Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to expand string represented as n(t) format in Python

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

String expansion in the n(t) format is a common programming problem where we need to decode compressed strings. The format n(t) means concatenate string t exactly n times, and t can be either a regular string or another encoded string recursively. So, if the input is like s = "3(pi)2(3(am))0(f)1(u)", then the output will be "pipipiamamamamamamu". Algorithm Steps To solve this problem, we will follow these steps − Initialize index i := 0 Define a recursive function parse() that processes the string Create an empty result list While i < string length and current character ...

Read More

Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python

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

Suppose we have a list of numbers called heights that represents the height of trees and we have another list of values called costs that represents the price needed to increase height of a tree by one. We have to find the smallest cost to make each height in the heights list different from adjacent heights. So, if the input is like heights = [3, 2, 2] costs = [2, 5, 3], then the output will be 3, as we can increase the last height by 1, which costs 3. Algorithm Steps To solve this, we will ...

Read More

Program to find maximum value of k for which we can maintain safe distance in Python

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

Suppose we have a binary matrix where 0 signifies an empty cell, and 1 signifies a cell with a person. The distance between two cells is the maximum value between the difference in x coordinates and the difference in y coordinates (Chebyshev distance). A matrix is considered safe with a factor k if there is an empty square such that the distance from the cell to each person in the matrix, and each side of the matrix is all greater or equal to k. We have to find the maximum value of factor k for which we can be safe. ...

Read More

Program to find size of smallest sublist whose sum at least target in Python

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

Suppose we have a list of numbers called nums, and another input called target, we have to find the size of the shortest sublist such that its sum value is same as target or larger. If there is no such sublist then return -1. So, if the input is like nums = [2, 11, -4, 17, 4] target = 19, then the output will be 2, as we can select [17, 4] to get sum of at least 19. Algorithm Approach To solve this, we will follow these steps − Create ...

Read More

Program to find length of shortest supersequence in Python

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

A supersequence is a string that contains both input strings as subsequences. The shortest supersequence problem finds the minimum length string that includes all characters from both strings while preserving their order. For example, if s = "pipe" and t = "people", then "pieople" is a supersequence with length 7. Algorithm Approach The solution uses dynamic programming to find the Longest Common Subsequence (LCS), then calculates the shortest supersequence length using the formula: Shortest Supersequence Length = len(s) + len(t) - LCS_length Step-by-Step Process Create a 2D table to store LCS lengths Fill ...

Read More

Program to find distance of shortest bridge between islands in Python

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

Suppose we have a binary matrix, where 0 represents water and 1 represents the land. An island is a group of connecting 1s in 4 directions. Islands are either surrounded by 0s (water) or by the edges. We have to find the length of shortest bridge that connects two islands. So, if the input is like: 0 0 ...

Read More

Program to evaluate s-expression as string in Python

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

Suppose we have a string s as S-expression. We have to evaluate that S-expression and return result as integer. As we know that the S-expression is an expression which is either one number, or a recursive expression wrapped in parentheses like (+ (- 3 2) (* 3 3)), which indicates (3 - 2) + (3 * 3) = 10. Here valid operators are +, -, *, and /. So, if the input is like s = "(- (+ 3 2) 2)", then the output will be 3, as ((3 + 2) - 2) = 3. Algorithm Steps ...

Read More

Program to check regular expression pattern is matching with string or not in Python

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

Regular expression pattern matching is a common programming problem where we need to check if a string matches a given pattern. In Python, we can implement this using dynamic programming with recursive approach. Regular Expression Rules The pattern matching follows these rules ? . (period) matches any single character * (asterisk) matches zero or more of the preceding element Algorithm Steps To solve this problem, we follow these steps ? Get the length of string s and pattern p Create a recursive function dp(i, j) where i is the current position ...

Read More

Program to count number of unique paths that includes given edges in Python

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

Given a list of edges representing a tree, we need to find the total number of unique paths that include each edge. A path in a tree connects any two nodes, and we count how many such paths pass through each given edge. Problem Understanding For the input edges = [[0, 1], [0, 2], [1, 3], [1, 4]], we have a tree structure where removing an edge splits the tree into two subtrees. The number of unique paths through an edge equals the product of nodes in each subtree. 0 ...

Read More

Program to count number of points that lie on a line in Python

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

Given a list of coordinates representing points on a Cartesian plane, we need to find the maximum number of points that lie on a single line. This problem requires calculating slopes between points and grouping them accordingly. So, if the input is like coordinates = [[6, 2], [8, 3], [10, 4], [1, 1], [2, 2], [6, 6], [7, 7]], then the output will be 4, as the points [1, 1], [2, 2], [6, 6], [7, 7] lie on the same line with slope 1. Algorithm To solve this problem, we follow these steps ? Initialize ...

Read More
Showing 5841–5850 of 61,297 articles
« Prev 1 583 584 585 586 587 6130 Next »
Advertisements