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 46 of 377
Program to fill Min-max game tree in Python
The Min-max game tree algorithm is used in two-player games where one player wants to maximize the score while the other wants to minimize it. In this implementation, Player 1 (maximizer) plays at even levels and Player 2 (minimizer) plays at odd levels. How the Algorithm Works The algorithm uses a bottom-up approach: Leaf nodes contain the actual game scores Internal nodes at even levels (Player 1) choose the maximum value from their children Internal nodes at odd levels (Player 2) choose the minimum value from their children Example Tree Structure ...
Read MoreProgram to find maximum sum of two non-overlapping sublists in Python
Finding the maximum sum of two non-overlapping sublists is a classic dynamic programming problem. Given a list of numbers and two lengths x and y, we need to find two sublists with these lengths that don't overlap and have the maximum combined sum. So, if the input is like nums = [3, 2, 10, -2, 7, 6], x = 3, y = 1, then the output will be 22, as the sublist with length 3 we select [3, 2, 10] (sum = 15) and for the other we select [7] (sum = 7). Algorithm Steps To solve ...
Read MoreProgram to find maximum sum by removing K numbers from ends in python
Suppose we have a list of numbers called nums and another value k. We have to find the maximum sum of elements that we can delete, given that we must pop exactly k times, where each pop can be from the left or the right end. So, if the input is like nums = [2, 4, 5, 3, 1] and k = 2, then the output will be 6, as we can delete 2 and 4 from the left end. Algorithm To solve this, we will follow these steps − window ...
Read MoreProgram to find maximum additive score by deleting numbers in Python
Suppose we have a list of numbers called nums. We can perform an operation where we select a number (not the first or last), remove it, and increase our score by the sum of that number and its two adjacent numbers. We have to find the maximal score possible by performing this operation as many times as we want. So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 39. We can select 5, then sum will be (4 + 5 + 6) = 15, array becomes [2, 3, 4, 6]. ...
Read MoreProgram to find a target value inside given matrix or not in Python
Suppose we have a 2D matrix where each row and column is sorted in non-decreasing order. We need to check whether a given target value is present inside it or not. So, if the input is like ? 2 4 30 3 4 31 6 6 32 And target = 31, then the output will be True. Approach To solve this efficiently, we can start from the top-right corner of the matrix and move either left or down based on the comparison with the target ...
Read MoreProgram to generate matrix where each cell holds Manhattan distance from nearest 0 in Python
The Manhattan distance between two points is the sum of absolute differences of their coordinates. In this problem, we need to find the Manhattan distance from each cell to the nearest 0 in a binary matrix. Given a binary matrix, we transform it so each cell contains the Manhattan distance to the closest 0. At least one 0 is guaranteed to exist. Example Input and Output If the input matrix is ? 101 101 110 The output will be ? 101 101 210 The bottom-left cell has distance 2 ...
Read MoreProgram to find number of combinations of coins to reach target in Python
Suppose we have a list of coins and another value amount, we have to find the number of combinations there are that sum to amount. If the answer is very large, then mod the result by 10^9 + 7. So, if the input is like coins = [2, 5] amount = 10, then the output will be 2, as we can make these combinations − [2, 2, 2, 2, 2], [5, 5] Algorithm To solve this, we will follow these steps − m := 10^9 + 7 ...
Read MoreProgram to find lowest sum of pairs greater than given target in Python
Given a list of numbers and a target value, we need to find the lowest sum of any pair that is greater than the target. This problem uses a two-pointer approach on a sorted array for efficient solution. For example, if nums = [2, 4, 6, 10, 14] and target = 10, the output will be 12 (pair: 2 + 10). Algorithm Steps To solve this problem, we follow these steps − Sort the list nums in ascending order Initialize two pointers: left at start (0) and right at end (n-1) Set answer to a ...
Read MoreNested List Weight Sum II in Python
The Nested List Weight Sum II problem calculates the weighted sum of integers in a nested list structure, where weights increase from bottom to top. Unlike the regular version where deeper elements have higher weights, here the deepest elements have weight 1, and shallower elements have progressively higher weights. For example, given [[1, 1], 2, [1, 1]], the structure has: Four 1's at depth 2 (weight 1 each) = 4 × 1 = 4 One 2 at depth 1 (weight 2) = 1 × 2 = 2 Total sum = 4 + 2 = 6 Algorithm ...
Read MoreProgram to find nth term in Look and Say Sequence in Python
The Look and Say Sequence is a famous sequence where each term describes the previous term by counting consecutive identical digits. Starting with "1", each subsequent term is generated by reading the previous term aloud. Understanding the Sequence The first few terms of the Look and Say sequence are: Term 1: 1 Term 2: 11 (one 1) Term 3: 21 (two 1s) Term 4: 1211 (one 2, one 1) Term 5: 111221 (one 1, one 2, two 1s) Each term is formed by counting consecutive identical digits in the previous term and stating the ...
Read More