Found 33676 Articles for Programming

Program to count number of ways we can make a list of values by splitting numeric string in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:50:41

112 Views

Suppose we have a strings s. The s is containing digits from 0 - 9 and we also have another number k. We have to find the number of different ways that s can be represented as a list of numbers from [1, k]. If the answer is very large then return result mod 10^9 + 7.So, if the input is like s = "3456" k = 500, then the output will be 7, as we can represent s like [3, 4, 5, 6], [34, 5, 6], [3, 4, 56], [3, 45, 6], [34, 56], [345, 6], [3, 456]To solve ... Read More

Program to find number of coins we can pick from topleft to bottom-right cell and return in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:48:38

167 Views

Suppose we have a 2D matrix with 3 possible values −0 for an empty cell.1 for a coin.−1 for a wall.We have to find the maximum number of coins we can take by starting from the top−left cell and reaching the bottom−right cell by moving only right or down direction. Then come back to the top−left cell by only moving up or left direction. When we pick up a coin, the cell value becomes 0. If we cannot reach the bottom−right cell, then return 0.So, if the input is like011111−111011then the output will be 8.To solve this, we will follow ... Read More

Program to check person 1 can win the candy game by taking maximum score or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:44:27

327 Views

Suppose two players are playing a game. Where several candies placed on a line, and person 1 is given a list of numbers called nums that is representing the point value of each candy. On each person's turn, they can pick 1, 2, or 3 candies from the front of the line, then delete them from list, and get the sum of their points added to their score. This game will end when all the candies are deleted and the person with the higher score will be the winner. We have to check person 1 can win this game or ... Read More

Program to check whether first player can take more candies than other or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:43:07

168 Views

Suppose we have a list of numbers called candies and two persons are racing to collect the most number of candies. Here the race is turn based, person 1 is starting first, and in each turn he can pick up candies from the front or from the back. We have to check whether person 1 can collect more candies than other or not.So, if the input is like candies = [1, 4, 3, 8], then the output will be True, as person 1 can take 8 candies in the first round and regardless of whether second person picks 1 or ... Read More

Program to count number of permutations where sum of adjacent pairs are perfect square in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:40:54

228 Views

Suppose we have a list of numbers called nums. We have to find the number of permutations of nums such that sum of every pair of adjacent values is a perfect square. Two permutations A and B are unique when there is some index i where A[i] is not same as B[i].So, if the input is like nums = [2, 9, 7], then the output will be 2, as we have [2, 7, 9] and [9, 7, 2]To solve this, we will follow these steps −res := 0Define a function util() . This will take iif i + 1 is ... Read More

Program to check whether final string can be formed using other two strings or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:38:50

152 Views

Suppose we have two strings s, t, and another string r we have to check whether there is any way to get r by merging characters in order from s and t.So, if the input is like s = "xyz" t = "mno" r = "xymnoz", then the output will be True, as xymnoz can be formed by interleaving xyz and mno.To solve this, we will follow these steps −Define a function solve() . This will take s, t, rif s, t and r are empty, thenreturn Trueif r is empty, thenreturn Falseif s is empty, thenreturn true when t ... Read More

Program to count number of flipping required to make all x before y in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:37:17

181 Views

Suppose we have a lowercase string s with letters x and y. Now consider an operation in which we change a single x to y or vice versa. We have to find the minimum number of times we need to perform that operation to set all x's come before all y's.So, if the input is like s = "yxyyyyxyxx", then the output will be 4.To solve this, we will follow these steps −y_left := 0x_right := number of "x" in s, res := number of "x" in sfor each item in s, doif item is same as "x", thenx_right := ... Read More

Program to check person can reach top-left or bottomright cell avoiding fire or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:34:04

131 Views

Suppose we have a 2D matrix with few different values like below −0 for empty cell1 for a person2 for fire3 for a wallNow assume there is only one person and in each turn the fire expands in all four directions (up, down, left and right) but fire cannot expand through walls. We have to check whether the person can move to either the top-left corner or the bottom-right corner or the matrix. We have to keep in mind that in each turn, the person moves first, then the fire expands. If the person makes it to any of the ... Read More

Program to count number of walls required to partition top-left and bottom-right cells in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:31:50

220 Views

Suppose we have a 2d binary matrix where 0 represents empty cell and 1 represents a wall. We have to find the minimum number cells that need to become walls so that there will be no path between top−left cell and bottom-right cell. We cannot put walls on the top−left cell and the bottom−right cell. We can move only left, right, up and down not diagonally.So, if the input is like0000010001100000then the output will be 2, 0100010001100010To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixvisited := a new settin ... Read More

Program to find number of unique subsequences same as target in C++

Arnab Chakraborty
Updated on 25-Dec-2020 06:03:39

151 Views

Suppose we have two lowercase strings s and t, we have to find the number of subsequences of s that are equal to t. If the answer is very large then return result by 10^9 + 7.So, if the input is like s = "abbd" t = "bd", then the output will be 2, as there are two possible subsequences "bd".s[1] concatenate s[3]s[2] concatenate s[3].To solve this, we will follow these steps −m := 10^9 + 7if size of t is same as 0, then −return 0if t is same as s, then −return 1if size of t > size ... Read More

Advertisements