Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 54 of 377

Program to find minimum number of bricks required to make k towers of same height in Python

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

Suppose we have a list of tower heights, and a positive value k. We want to select k towers and make them all the same height by adding more bricks, but using as few bricks as possible. We have to find the minimum number of bricks needed to pick k towers and make them the same height. So, if the input is like heights = [4, 7, 31, 14, 40] k = 3, then the output will be 17, as we can select towers with heights [7, 14, 31] and make them all height 31, requiring (31-7) + (31-14) ...

Read More

Program to find sum of non-adjacent elements in a circular list in python

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

Suppose we have a list of numbers representing a circular list. We need to find the largest sum of non-adjacent elements, where the first and last elements are considered adjacent due to the circular nature. For example, if the input is nums = [10, 3, 4, 8], the output will be 14 by selecting elements 10 and 4. We cannot select 10 and 8 because they are adjacent in the circular arrangement. Algorithm Approach Since the list is circular, we need to handle two cases separately: Case 1: Include the first element but exclude the ...

Read More

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 442 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 193 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 237 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 214 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
Showing 531–540 of 3,768 articles
« Prev 1 52 53 54 55 56 377 Next »
Advertisements