Check for Cycles in Matrix Elements using Python

Arnab Chakraborty
Updated on 03-Dec-2020 06:04:32

216 Views

Suppose we have a 2d matrix. We have to check whether we can start from some cell then move adjacent cells (up, down, left, right) of the same value, and come back to the same starting point. We cannot revisit a cell that we have visited last.So, if the input is like222121212221then the output will be True, as we can follow 2s to form a cycle.To solve this, we will follow these steps −R := row count of matrixC := column count of matrixvis := make a matrix of size R x C and fill with FalseDefine a function dfs() ... Read More

Count Ways to Cut Matrix into K Pieces in Python

Arnab Chakraborty
Updated on 03-Dec-2020 06:01:58

219 Views

Suppose we have a binary matrix and another value k. You want to split the matrix into k pieces such that each piece contains at least one 1 in it. But there are some rules for cutting, we have to follow in order: 1. Select a direction: vertical or horizontal 2. Select an index in the matrix to cut into two sections. 3. If we cut vertically, we can no longer cut the left part but can only continue cutting the right part.  4. If we cut horizontally, we can no longer cut the top part and can only continue ... Read More

Check Palindrome Formation by Deleting K Characters in Python

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

590 Views

Suppose we have a string s, we have to check whether we can make this string a palindrome after deleting at most k characters or not.So, if the input is like s = "lieuvrel", k = 4, then the output will be True, we can delete three characters to get palindrome "level".To solve this, we will follow these steps −Define a function lcs() . This will take a, bm := size of a, n := size of btable := matrix of size (m + 1) x (n + 1) and fill with 0for i in range 1 to m, dofor ... Read More

Create Largest Lexicographic Number from List of Numbers in C++

Arnab Chakraborty
Updated on 03-Dec-2020 05:57:20

330 Views

Suppose we have a list of numbers called nums, we have to rearrange its order to form the largest possible number and return that as a string.So, if the input is like nums = [20, 8, 85, 316], then the output will be "88531620".To solve this, we will follow these steps −Define an array tempfor each item i in nums:insert i into temp as stringsort the array temp based on lexicographic sequence (check for two strings a, b when a concatenate b is larger than b concatenate a or not)for each string s in temp:res := res concatenate sreturn resLet ... Read More

Find Maximum Possible Population of Cities in Python

Arnab Chakraborty
Updated on 03-Dec-2020 05:54:46

682 Views

Consider a country that is represented as a tree with N nodes and N-1 edges. Now each node represents a town, and each edge represents a road. We have two lists of numbers source and dest of size N-1. According to them the i-th road connects source[i] to dest[i]. And the roads are bidirectional. We also have another list of numbers called population of size N, where population[i] represents the population of the i-th town. We are trying to upgrade some number of towns into cities. But no two cities should be adjacent to each other and every node adjacent ... Read More

Count Subsets that Sum Up to K in Python

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

487 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

150 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

865 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

180 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

211 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

Advertisements