Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 98 of 377
Program to find the sum of largest K sublist in Python
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 MoreProgram to find area of largest island in a matrix in Python
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 MoreProgram to find largest distance pair from two list of numbers in Python
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 MoreProgram to find difference between node and a descendent in Python
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 MoreProgram to find the percentage of places where knight can move and not left the board in Python
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 MoreProgram to find minimum steps to reach target position by a chess knight in Python
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 MoreProgram to find k sublists with largest sums and return sums in ascending order in Python
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 MoreProgram to find a sub-list of size at least 2 whose sum is multiple of k in Python
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 MoreProgram to find number of elements in A are strictly less than at least k elements in B in Python
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 MoreProgram to check number of global and local inversions are same or not in Python
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