Check Point Inside or Boundary of Polygon in Python

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

879 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

192 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

218 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

752 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

377 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

241 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

211 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

Count Pairs with Sum as a Prime Number and Less than n in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:41:45

388 Views

We are given a positive number n as input. The goal is to find the count of possible pairs (i, j) such that each pair has sum (i+j) which is prime and is less than n. Also i != j and i, j>=1 If n is 4 then only 1 pair is possible which is (1, 2). Here 1+2 = 3 is prime and less than 4. Also 1, 2 >=1.Let us understand with examples.Input − n=7Output − Count of pairs with sum as a prime number and less than n are − 3Explanation − Pairs will be (1, 2), ... Read More

Count Permutations That Are First Decreasing Then Increasing in C++

Sunidhi Bansal
Updated on 02-Dec-2020 12:39:34

265 Views

We are a variable num. The goal is to find the count of permutations of numbers between [1, num] in which numbers are first decreasing then increasing. For example if num=3 then numbers are 1, 2, 3. The permutations will be [ 3, 1, 2 ] and [2, 1, 3] and count is 2.We know that in every permutation the change from decreasing of numbers to increasing of numbers will be decided based on position of 1 which is smallest. After each 1 the numbers will start increasing. For a permutation to decrease and then increase, 1 should lie between ... Read More

Advertisements