Programming Articles

Page 475 of 2547

Check if a point lies on or inside a rectangle in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 6K+ Views

When working with geometric problems in Python, checking if a point lies inside a rectangle is a fundamental task. Given a rectangle defined by its bottom-left and top-right corner points, we can determine if any point (x, y) falls within its boundaries. Problem Statement Given a rectangle represented by two points: bottom-left corner (x1, y1) and top-right corner (x2, y2), we need to check whether a given point (x, y) lies inside or on the rectangle. For example, if bottom_left = (1, 1), top_right = (8, 5), and point = (5, 4), the output should be True ...

Read More

Program to Find Out the Smallest Substring Containing a Specific String in Python

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

Finding the smallest substring that contains a specific string as a subsequence is a challenging problem. Given two strings s and t, we need to find the shortest substring in s where t appears as a subsequence. If multiple substrings exist with the same minimum length, we return the leftmost one. For example, if s = "abcbfbghfb" and t = "fg", the output will be "fbg" because it's the smallest substring containing "f" and "g" in sequence. Algorithm Overview We use dynamic programming to solve this problem efficiently ? Create a DP array where dp[i] ...

Read More

Program to Reverse a Substring Enclosed Within Brackets in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 582 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 287 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 183 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 248 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 283 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 214 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 388 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 363 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
Showing 4741–4750 of 25,466 articles
« Prev 1 473 474 475 476 477 2547 Next »
Advertisements