Count Subsets that Sum Up to K in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:53:12

514 Views

Suppose we have a list of numbers called nums and another value k, we have to find the number of subsets in the list that sum up to k. If the answer is very large then mod this with 10^9 + 7So, if the input is like nums = [2, 3, 4, 5, 7] k = 7, then the output will be 3, as We can choose the subsets [2,5],[3,4] and [7].To solve this, we will follow these steps −dp := a list of size (k + 1) and fill with 0dp[0] := 1m := 10^9 + 7for i in range 0 to size of nums - 1, dofor j in range k down to 0, decrease by 1, doif nums[i]

Count Operations to Convert Binary Matrix to Zero Matrix in C++

Arnab Chakraborty
Updated on 03-Dec-2020 05:52:05

181 Views

Suppose we have a binary matrix. Now consider an operation where we take one cell and flip it and all its neighboring cells (up, down, left, right). We have to find the minimum number of operations required such that matrix contains only 0s. If there is no solution, then return -1.So, if the input is like0010then the output will be 3.To solve this, we will follow these steps −Define an array dir of size: 4 x 2 := {{1, 0}, {0, 1}, { - 1, 0}, {0, - 1}}const int inf = 10^6Define a function getPos(), this will take i, ... Read More

Check Point Inside or Boundary of Polygon in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:49:15

911 Views

Suppose we have a list of cartesian points [(x1, y1), (x2, y2), ..., (xn, yn)], that is representing a polygon, and also have two values x and y, we have to check whether (x, y) lies inside this polygon or on the boundary.So, if the input is like points = [(0, 0), (1, 3), (4, 4), (6, 2), (4, 0)] pt = (3, 1)then the output will be TrueTo solve this, we will follow these steps −ans := Falsefor i in range 0 to size of polygon - 1, do(x0, y0) := polygon[i](x1, y1) := polygon[(i + 1) mod size ... Read More

Find Length of Longest Circular Increasing Subsequence in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:44:58

205 Views

Suppose we have a list of numbers called nums, we have to find the length of the longest increasing subsequence and we are assuming the subsequence can wrap around to the beginning of the list.So, if the input is like nums = [6, 5, 8, 2, 3, 4], then the output will be 5, as the longest increasing subsequence is [2, 3, 4, 6, 8].To solve this, we will follow these steps −a := make a list of size twice of nums and fill nums twiceans := 0for i in range 0 to size of nums, dodp := a new ... Read More

Ways to Choose Empty Cells of a Matrix in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:43:06

235 Views

Suppose we have a N x N binary matrix where 0 is for empty cells and 1 is a blocked cells, we have to find the number of ways to choose N empty cells such that every row and every column has at least one chosen cells. If the answer is very large return result mod 10^9 + 7So, if the input is like000000010then the output will be 4, as we have following configurations (where x is a selected cell) −To solve this, we will follow these steps −n := size of matrixDefine a function f() . This will take ... Read More

Find Minimum Roads to Reach Any City in C++

Arnab Chakraborty
Updated on 03-Dec-2020 05:41:15

773 Views

Suppose we have two lists costs_from and costs_to of same size where each index i represents a city. It is making a one-way road from city i to j and their costs are costs_from[i] + costs_to[j]. We also have a list of edges where each edge contains [x, y] indicates there is already a one-way road from city x to y. If we want to go to any city from city 0, we have to find the minimum cost to build the necessary roads.So, if the input is like costs_from = [6, 2, 2, 12] costs_to = [2, 2, 3, ... Read More

Minimum Number of Buses Required to Reach Final Target in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:38:42

395 Views

Suppose we have a n x 3 matrix where each row contains three fields [src, dest, id] this means the bus has route from src to dest. It takes one unit of money to get on a new bus, but if we stay on the same bus we have to pay one unit only. We have to find the minimum cost necessary to take the bus from location 0 to the final stop (largest location). If there is no solution return -1.So, if the input is like010120230351502then the output will be 2, as we can take the 0 at location ... Read More

Evaluate Mathematical Expression Without Built-in Functions in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:37:08

3K+ Views

Suppose we have a string that represents a mathematical expression with (+, -, *, /) Here / is representing integer division, we have to evaluate and return the result without using any built-in function.So, if the input is like s = "2+3*5/7", then the output will be 4, as 2 + ((3 * 5) / 7) = 4To solve this, we will follow these steps −s := reverse the given stringDefine a function get_value().sign := 1if s is not empty and last element of s is same as "-", thendelete last element from ssign := -1value := 0while s is ... Read More

Find Maximum Profit After K Buy and Sell in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:34:22

261 Views

Suppose we have a list of numbers called nums that is representing the stock prices of a company in chronological order and we also have another value k, we have to find the maximum profit we can make from up to k buys and sells (We must buy before sell, and sell before buy).So, if the input is like prices = [7, 3, 5, 2, 3] k = 2, then the output will be 3, as we can buy at 3, then sell at 5, again buy at 2, and sell at 3.To solve this, we will follow these steps ... Read More

Count of Words with Specific Letter Conditions in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:44:19

224 Views

We are given a string str[] as input. The goal is to count the words from str[] that have the same length as str[] and have positions of letters such that ith letter is replaced with letter at position (i1) or (i) or (i+1).For the first letter replacement will be from position i or i+1For the last letter replacement will be from position i-1 or i.Let us understand with examples.Input − str[] = “TPP”Output − Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are − 4Explanation Replacing T by T (i)th or 1st ... Read More

Advertisements