Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to Reverse a Substring Enclosed Within Brackets in Python

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

When working with strings containing parentheses, we often need to reverse the content within brackets. This problem requires us to reverse every substring enclosed within parentheses in a recursive manner. For example, if the input string is "back(aps)ce", we need to reverse "aps" to get "spa", resulting in "backspace". Algorithm Approach We'll use a two-phase approach ? Phase 1: Create a mapping of matching parentheses using a stack Phase 2: Traverse the string and reverse content within brackets recursively Step-by-Step Solution Phase 1: Map Matching Brackets First, we identify which opening ...

Read More

Program to Find Out the Number of Moves to Reach the Finish Line in Python

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

In this problem, we have a car starting at position 0 with speed 1 on a one-dimensional road. We need to find the minimum number of moves to reach a target position using two possible operations: Acceleration: position += speed and speed *= 2 Reverse: speed = -1 if speed > 0, otherwise speed = 1 For example, if the target is 10, the minimum number of moves required is 7. Algorithm Approach We use a depth-first search (DFS) with memoization to explore all possible move combinations. The algorithm considers different sequences of acceleration ...

Read More

Program to Find Out the Probability of Having n or Fewer Points in Python

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

Suppose we are playing a unique game with three values n, k, and h. We start from 0 points, then we can select a number randomly between 1 and h (inclusive) and we will get that many points. We stop when we have scored a minimum of k points. We have to find the probability that we have n or fewer points when we stop. So, if the input is like n = 2, k = 2, h = 10, then the output will be 0.11. Algorithm Steps To solve this, we will follow these steps: ...

Read More

Program to Find Out a Sequence with Equivalent Frequencies in Python

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

Finding the longest sequence where all numbers have equal frequencies after removing at most one element is a complex frequency tracking problem. We need to monitor how frequencies change as we build the sequence and check if we can achieve uniform distribution. Problem Understanding Given a list of numbers, we want the longest prefix where we can remove at most one number to make all remaining numbers appear the same number of times ? # Example: [2, 4, 4, 7, 7, 6, 6] # All numbers appear twice, so we can use the entire sequence numbers ...

Read More

Program to Find Minimum Jumps Required to Reach a Value with Different Parity in Python

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

Given a list of numbers, we can jump from index i to either i + nums[i] or i - nums[i] (if valid indices). We need to find the minimum jumps required from each position to reach a value with different parity (odd to even or even to odd). If the input is numbers = [7, 3, 4, 5, 6, 9, 6, 7], the output will be [-1, 1, 2, -1, -1, -1, 1, -1]. Understanding the Problem For each starting position, we use BFS to find the shortest path to any number with different parity. Two numbers ...

Read More

Program to find island count by adding blocks into grid one by one in Python

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

Suppose we have an infinite grid of water. We can add blocks of land to that grid one by one. We have a list of coordinates called land_requests where each coordinate is in the form [r, c] where r is for row and c is for column. We need to find a list where each element represents the number of islands that exist after adding each block of land from land_requests. For example, if the input is land_requests = [[1, 1], [2, 4], [1, 2], [1, 4], [1, 3]], then the output will be [1, 2, 2, 2, 1]. ...

Read More

Program to find sum of rectangle whose sum at most k in Python

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

Given a 2D matrix and a value k, we need to find the largest sum of a rectangle whose sum is at most k. This problem combines matrix manipulation with optimization techniques. Problem Understanding Consider this matrix with k = 15: 5 -2 7 10 We can form rectangles like [5, 7] with sum = 12, [5, -2, 7, 10] with sum = 20, etc. The largest sum ≤ 15 is 12. Algorithm Steps For each pair of rows (i1, i2), compress the matrix vertically Convert the ...

Read More

Program to find largest average of sublist whose size at least k in Python

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

Suppose we have a list of numbers called nums and another value k, we have to find the largest average value of any sublist whose length is at least k. So, if the input is like nums = [2, 10, -50, 4, 6, 6] and k = 3, then the output will be 5.33333333, as sublist [4, 6, 6] has the largest average value. Algorithm Steps To solve this, we will follow these steps − Initialize left := minimum of nums, right := maximum of nums Calculate initial sum ...

Read More

Program to find kth lexicographic sequence from 1 to n of size k Python

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

Suppose we have two values n and k. We need to consider a list of numbers in range 1 through n [1, 2, ..., n] and generate every permutation of this list in lexicographic order. For example, if n = 4 we have [1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321]. We have to find the kth value of this permutation sequence as a string. So, if the input is like n = 4, k = 5, then the output will be "1423" ...

Read More

Program to count number of ways we can make a list of values by splitting numeric string in Python

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

Suppose we have a string s containing digits from 0-9 and another number k. We need to find the number of different ways that s can be represented as a list of numbers from [1, k]. If the answer is very large, return result mod 10^9 + 7. So, if the input is like s = "3456", k = 500, then the output will be 7, as we can represent s like [3, 4, 5, 6], [34, 5, 6], [3, 4, 56], [3, 45, 6], [34, 56], [345, 6], [3, 456]. Algorithm Approach We'll use dynamic programming ...

Read More
Showing 5811–5820 of 61,299 articles
« Prev 1 580 581 582 583 584 6130 Next »
Advertisements