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

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

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

Find Target Value Inside Given Matrix 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

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

Generate Matrix with 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

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

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

Pushing Values into an Associative Array in PHP

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       )    ) )

Extract Numbers After the Last Hyphen in PHP

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

850 Views

Let’s say the following is our string including hyphen and numbers as well −"John-45-98-78-7898906756"To extract numbers after – i.e. hyphen, use the concept of explode() with parameters - and $yourVariableName.The PHP code is as follows −Example Live Demo Output7898906756

Advertisements