Found 33676 Articles for Programming

Find if there is a path of more than k length from a source in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:41:27

212 Views

Suppose we have a graph, we also have a source vertex and a number k. The k is the path length of graph between source to destination, we have to check whether we can find a simple path (without cycle) starting from source and ending at any other vertex (as destination). The graph is shown in following −So, if the input is like Source = 0, k = 64, then the output will be True as there exists a simple path 0 to 7 to 1 to 2 to 8 to 6 to 5 to 3 to 4, this path ... Read More

Find if neat arrangement of cups and shelves can be made in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:38:41

144 Views

Suppose we have three different types of cups in array p and saucers in array q and m number of shelves, we have to check whether a neat arrangement of cups and shelves can be made.We can say that arrangement of the cups and saucers will be neat if it follows these conditions − 1. No shelf can hold both cups and saucers. 2. A self may contain at most 5 cups. 3. A self may contain at most 10 saucers.So, if the input is like p = [4, 3, 7] q = [5, 9, 10] m = 11, then ... Read More

Find if it is possible to get a ratio from given ranges of costs and quantities in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:36:21

91 Views

Suppose we have a range of cost from lowCost to upCost and another range of quantity from lowQuant to upQuant, we have to check whether we can find the given ratio r where r=cost/quantity, and lowCost ⇐ cost ⇐ upCost and lowQuant ⇐ quantity ⇐ upQuant.So, if the input is like lowCost = 2, upCost = 10, lowQuant = 3, upQuant = 9 and r = 3, then the output will be True as the cost = r * quantity = 3 * 3 = 9 where cost is in range [1, 10] and quantity is in [2, 8]To solve ... Read More

Find if given vertical level of binary tree is sorted or not in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:33:34

106 Views

Suppose we have binary tree; we have to check whether the given vertical level of the binary tree is sorted or not. When two nodes are overlapping, check they are in sorted sequence in the level they belong.So, if the input is like l = -1then the output will be True, as elements in level -1 are 3, 7 and they are sorted.To solve this, we will follow these steps −if root is null, thenreturn Trueprevious_value := -infcurrent_level := 0current_node := a new tree node with value 0Define one dequeue named qinsert (root, 0) at the end of qwhile q ... Read More

Find if an undirected graph contains an independent set of a given size in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:30:58

328 Views

Suppose we have a given undirected graph; we have to check whether it contains an independent set of size l. If there is any independent set of size l then return Yes otherwise No.We have to keep in mind that an independent set in a graph is defined as a set of vertices which are not directly connected to each other.So, if the input is like L = 4, then the output will be yesTo solve this, we will follow these steps −Define a function is_valid() . This will take graph, arrfor i in range 0 to size of arr, ... Read More

Find Four points such that they form a square whose sides are parallel to x and y axes in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:27:11

515 Views

Suppose we have n pair of points; we have to find four points so that they can generate a square whose sides are parallel to x and y axes otherwise return "not possible" If we can find more than one square then select the one with whose area is maximum.So, if the input is like n = 6, points = [(2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)], then the output will be 3, points are (2, 2) (5, 2) (2, 5) (5, 5)To solve this, we will follow these steps −my_map := a new mapfor ... Read More

Find four missing numbers in an array containing elements from 1 to N in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:21:54

218 Views

Suppose we have an array of distinct numbers where each number lies in the range [1, N], the array size is (N-4) and no single element is repeated. So, we can understand four numbers, from 1 to N, are missing in the array. We have to find these 4 missing numbers in sorted manner.So, if the input is like A =[2, 8, 4, 13, 6, 11, 9, 5, 10], then the output will be [1, 3, 7, 12]To solve this, we will follow these steps −temp_arr := an array of size 4 with all 0sfor i in range 0 to ... Read More

Find First element in AP which is multiple of given Prime in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:13:48

95 Views

Suppose we have a first term (A) and common difference (d) of an AP series, and we also have a prime number P, we have to find the position of the first element in the given AP which is the a multiple of the given prime number P.So, if the input is like A = 3, D = 4, P = 5, then the output will be 3 as fourth term of the given AP is a multiple of the prime number 5. So, first term = 3, second term = 3+4 = 7, third term = 3+2*4 = 11 ... Read More

Find element position in given monotonic sequence in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:11:49

269 Views

Suppose we have a number l and a monotonic increasing sequence f(m), where f(m) = am + bm [log2(m)] + cm^3 and (a = 1, 2, 3, …), (b = 1, 2, 3, …), (c = 0, 1, 2, 3, …)Here [log2(m)] is the log to the base 2 and round the value down. so, if m = 1, the value is 0.if m = 2-3, the value is 1.if m = 4-7, the value is 2.if m = 8-15, the value is 3. and so, onwe have to find the value m such that f(m) = l, if l ... Read More

Find Duplicates of array using bit array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:09:37

189 Views

Suppose we have an array of n different numbers; n can be 32, 000 at max. The array may have duplicate entries and we do not know what is the value of n. Now if we have only 4-Kilobytes of memory, how would display all duplicates in the array?So, if the input is like [2, 6, 2, 11, 13, 11], then the output will be [2, 11] as 2 and 11 appear more than once in given array.To solve this, we will follow these steps −Create one byte-array type data structure bit_arr, it has following methodsDefine constructor This will take ... Read More

Advertisements