Program to find largest distance pair from two list of numbers in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:30:44

235 Views

We need to find the maximum value of the expression |a[i] - a[j]| + |b[i] - b[j]| + |i - j| for all valid pairs (i, j) where 0 ≤ i < j < n from two lists A and B of equal length. The key insight is to transform the absolute value expression using the mathematical property that |x| + |y| = max(x+y, x-y, -x+y, -x-y). This allows us to convert the problem into finding maximum and minimum values efficiently. Understanding the Problem Given two lists A and B, we want to maximize: |a[i] - ... Read More

Program to find difference between node and a descendent in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:30:18

218 Views

In binary tree problems, we often need to find the maximum difference between a node and its descendants. This involves traversing the tree and tracking the minimum and maximum values in each subtree to calculate the largest absolute difference. So, if the input is like: .node-circle { fill: lightblue; stroke: black; stroke-width: 2; } .node-text { font-family: Arial; font-size: 14px; text-anchor: middle; dominant-baseline: middle; } .edge-line { stroke: black; stroke-width: 2; } ... Read More

Program to find the percentage of places where knight can move and not left the board in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:29:57

259 Views

Suppose we have four values n, x, y, and k. Here n indicates an n x n chessboard and (x, y) coordinates represent where a knight is placed. The knight has to take exactly k steps, where at each step it can move in any of the 8 directions with equal probability. We have to find the percentage chance (nearest integer) that the knight remains on the chessboard after taking k moves. The key condition is that once the knight leaves the board, it cannot enter again. Example Scenario If the input is n = 8, (x ... Read More

Program to find minimum steps to reach target position by a chess knight in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:29:28

1K+ Views

Finding the minimum number of moves for a chess knight to reach a target position is a classic problem that can be solved using mathematical optimization. A knight moves in an L-shape: two squares in one direction and one square perpendicular to that direction. Problem Understanding Given a knight starting at position (0, 0) on an infinite chessboard, we need to find the minimum steps to reach position (r, c). The knight has 8 possible moves from any position ? ... Read More

Program to find k sublists with largest sums and return sums in ascending order in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:28:59

241 Views

Suppose we have a list of numbers called nums, and another value k, we have to find k sublists with the largest sums and return the sums in non-decreasing order. So, if the input is like nums = [2, 4, 5, -100, 12, -30, 6, -2, 6] k = 3, then the output will be [10, 11, 12], as we have these 3 sublists with the largest sums − [6, -2, 6], [2, 4, 5], [12]. Algorithm To solve this, we will follow these steps − Create a prefix sum array to calculate sublist sums ... Read More

Program to find a sub-list of size at least 2 whose sum is multiple of k in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:28:38

198 Views

Suppose we have a list of non-negative numbers called nums and another positive value k. We have to check whether there is any sublist of length at least 2 whose sum is multiple of k or not. So, if the input is like nums = [12, 6, 3, 4] k = 5, then the output will be True, as the sublist is [12, 3] sums to 15 which is divisible by 5. Algorithm To solve this, we will follow these steps ? sum := 0 m := ... Read More

Program to find number of elements in A are strictly less than at least k elements in B in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:28:14

2K+ Views

When working with two lists of numbers, we sometimes need to find how many elements from the first list are strictly smaller than at least k elements in the second list. This problem can be solved efficiently by sorting and comparison. So, if the input is like A = [6, -2, 100, 11], B = [33, 6, 30, 8, 14], k = 3, then the output will be 3, as -2, 6, and 11 are strictly less than 3 elements in B. Algorithm To solve this, we will follow these steps − if k is ... Read More

Program to check number of global and local inversions are same or not in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:27:51

231 Views

Suppose we have a list of distinct numbers called nums. Here a global inversion is when there are indices i < j such that nums[i] > nums[j]. And a local inversion is when there is an index i such that nums[i] > nums[i + 1]. We have to check whether the number of global inversions is equal to the number of local inversions or not. So, if the input is like nums = [3, 2, 4], then the output will be True, as the indices 0 and 1 are both a global and local inversion. Key Insight ... Read More

Program to find overlapping intervals and return them in ascending order in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:27:33

452 Views

Overlapping intervals occur when two intervals share common values. Given two sorted lists of non-overlapping intervals, we need to find their intersections and return them in ascending order. The key insight is to use a two-pointer approach to compare intervals from both lists and calculate their overlap using the maximum of start points and minimum of end points. Problem Statement Given two lists of intervals where each list is sorted and non-overlapping internally, find all overlapping intervals between the two lists. Example If we have inv1 = [[50, 100], [190, 270], [310, 330]] and inv2 ... Read More

Program to find total unique duration from a list of intervals in Python

Arnab Chakraborty
Updated on 25-Mar-2026 12:27:11

1K+ Views

Suppose we have a list of intervals where each interval represents a range [start, end] (inclusive). We need to find the total unique duration covered by all intervals combined, handling any overlaps correctly. So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]], then the output will be 50, as the total unique covered distance is (11 - 2 + 1) = 10 then (31 - 13 + 1) = 19 and (61 - 41 + 1) = 21, so total is 50. Algorithm To solve this, we will follow these steps − ... Read More

Advertisements