Found 33676 Articles for Programming

Program to fill Min-max game tree in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:55:51

3K+ Views

Suppose we have a binary tree representing a game state of a two player game. Every internal node is filled with 0 and the leaves values represent the end score. Player 1 wants to maximize the end score while player 2 wants to minimize the end score. Player 1 will always make moves on nodes at even levels and player 2 will always make moves on odd levels. We have to fill in the binary tree with the resulting scores assuming both of players play optimally.So, if the input is likethen the output will beTo solve this, we will follow ... Read More

Program to find maximum sum of two non-overlapping sublists in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:52:48

313 Views

Suppose we have a list of numbers called nums and two values x and y, we have to find the maximum sum of two non-overlapping sublists in nums which have lengths x and y.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] and for the other we select [7].To solve this, we will follow these steps −P := a list with single element 0for each x in A, doinsert (last element of ... Read More

Program to find maximum sum by removing K numbers from ends in python

Arnab Chakraborty
Updated on 20-Nov-2020 05:51:18

400 Views

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] k = 2, then the output will be 6, as we can delete 2 and the 4.To solve this, we will follow these steps −window := sum of all numbers from index 0 through k - 1ans := windowfor i in range ... Read More

Program to find maximum additive score by deleting numbers in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:50:07

185 Views

Suppose we have a list of numbers called nums. Let us consider an operation where we can select a number, then remove it and increase our score by the sum of the number and its two adjacent numbers. If we can perform this operation as many times as we want as long as we do not select the first or the last number in the list. We have to find the maximal score possible.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 39, as we can select 5, then sum will ... Read More

Program to find a target value inside given matrix or not in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:48:13

442 Views

Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to check whether given target is present inside it or not.So, if the input is like243034316632And target = 31, then the output will be TrueTo solve this, we will follow these steps −col := column size of matrix - 1for i in range 0 to row size of matrix, dowhile matrix[i, col] > target and col >= 0, docol := col - 1if matrix[i, col] is same as target, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live ... Read More

Program to find nth smallest number from a given matrix in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:47:00

178 Views

Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to find the nth smallest number.So, if the input is like243034316632And n = 4, then the output will be 6.To solve this, we will follow these steps −lst := a new listfor each row i in matrix, dofor each cell j in i, doinsert j at the end of lstsort the list lstreturn lst[n]Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, matrix, n):       lst = []       for i ... Read More

Program to generate matrix where each cell holds Manhattan distance from nearest 0 in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:45:39

234 Views

Suppose we have a binary matrix. We have to find the same matrix, but each cell's value will be the Manhattan distance to the nearest 0. We can assume at least one 0 exists in the matrix.So, if the input is like101101110then the output will be101101210as only the bottom left cell has distance of 2 to the nearest 0.To solve this, we will follow these steps −m := row size of matrix, n := column size of matrixfor y in range 0 to m, dofor x in range 0 to n, doif matrix[y, x] is non-zero, thenmatrix[y, x] := infinityfor ... Read More

Program to find number of combinations of coins to reach target in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:43:36

1K+ Views

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]To solve this, we will follow these steps −m := 10^9 + 7dp := a list of size same as amount + 1, and fill it with 0dp[0] := 1for ... Read More

Program to find lowest sum of pairs greater than given target in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:40:22

256 Views

Suppose we have a list of numbers called nums and another value target. We have to find the lowest sum of pair of numbers that is larger than target.So, if the input is like nums = [2, 4, 6, 10, 14] target = 10, then the output will be 12, as we pick 2 and 10To solve this, we will follow these steps −sort the list numsn := size of numsanswer := 10^10i := 0, j := n - 1while i < j, doif nums[i] + nums[j] > target, thenanswer := minimum of answer and (nums[i] + nums[j])j := j ... Read More

PHP Pushing values into an associative array?

AmitDiwan
Updated on 20-Nov-2020 05:38:42

2K+ Views

To push values into an associative array, use the brackets [] []. At first create an associative array −$details= array (    'id' => '101',    'name' => 'John Smith',    'countryName' => 'US' );The PHP code is as follows to insert values −Example Live Demo OutputArray (  [studentDetails] => Array (     [0] => Array (        [id] => 101 [name] => John Smith [countryName] => US       )    ) )

Advertisements