Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 98 of 377

Program to find the sum of largest K sublist in Python

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

When we have a list of numbers and need to find the maximum sum of a contiguous sublist from the list concatenated k times, we can use Kadane's algorithm with optimization for large k values. The key insight is that we only need to process at most 2 complete iterations of the original list to find the optimal solution, then add the contribution from remaining iterations if beneficial. Algorithm Steps The algorithm works as follows: Initialize sum (s), answer (ans), and lowest prefix sum (lo) to 0 Iterate through the list at most min(k, 2) ...

Read More

Program to find area of largest island in a matrix in Python

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

Suppose we have a binary matrix where 1 represents land and 0 represents water. An island is a group of connected 1s whose perimeter is surrounded by water. We can assume that the edges of the matrix are surrounded by water. We need to find the area of the largest island in the matrix. So, if the input matrix is like ? 0011111 0000000 0111100 0011000 0000011 0000010 Then the output will be 6, as the largest island has 6 connected land cells. Algorithm To solve this problem, we use Depth-First ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 209 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
Arnab Chakraborty
Updated on 25-Mar-2026 194 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
Arnab Chakraborty
Updated on 25-Mar-2026 239 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
Arnab Chakraborty
Updated on 25-Mar-2026 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
Arnab Chakraborty
Updated on 25-Mar-2026 215 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
Arnab Chakraborty
Updated on 25-Mar-2026 178 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
Arnab Chakraborty
Updated on 25-Mar-2026 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
Arnab Chakraborty
Updated on 25-Mar-2026 204 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
Showing 971–980 of 3,768 articles
« Prev 1 96 97 98 99 100 377 Next »
Advertisements