Found 10476 Articles for Python

Find lost element from a duplicated array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:01:10

302 Views

Suppose we have two arrays which are duplicates of each other except one element, so, one element from one of the given arrays is missing, we have to find that missing element.So, if the input is like A = [2, 5, 6, 8, 10], B = [5, 6, 8, 10], then the output will be 2 as 2 is missing from second array.To solve this, we will follow these steps −Define a function solve() . This will take A, B, Nif N is same as 1, thenreturn A[0];if A[0] is not same as B[0], thenreturn A[0]low := 0, high := ... Read More

Find longest palindrome formed by removing or shuffling chars from string in Python

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

300 Views

Suppose we have a string; we have to find the longest palindrome that can be generated by deleting or shuffling the characters from the string. And if there are more than one palindrome then return only one.So, if the input is like pqqprrs, then the output will be pqrsrqp.To solve this, we will follow these steps −count := array of size 256, filled with 0for i in range 0 to size of string, docount[ASCII of(string[i]) ] := count[ASCII of(string[i]) ] + 1begin := blank string, mid := blank string, end := blank stringcharacter := ASCII of('a')while character

Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:56:08

164 Views

Suppose we have two arrays; we have to find the longest possible bitonic sequence so that the increasing part should be from first array and should be a subsequence of first array. similarly decreasing part of must be from second array and a subsequence of the second one.So, if the input is like A = [2, 6, 3, 5, 4, 6], B = [9, 7, 5, 8, 4, 3], then the output will be [2, 3, 4, 6, 9, 7, 5, 4, 3]To solve this, we will follow these steps −Define a function index_ceiling() . This will take arr, T, ... Read More

Find largest subtree having identical left and right subtrees in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:52:45

111 Views

Suppose we have a binary tree; we have to find the largest subtree having identical left and right subtree. Preferred time complexity is O(n).So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function solve() . This will take root, encode, maxSize, maxNodeif root is None, thenreturn 0left_list := list with a blank stringright_list := list with a blank stringls := solve(root.left, left_list, maxSize, maxNode)rs := solve(root.right, right_list, maxSize, maxNode)size := ls + rs + 1if left_list[0] is same as right_list[0], thenif size > maxSize[0], thenmaxSize[0] := sizemaxNode[0] := rootencode[0] := ... Read More

Find k-th character of decrypted string - Set – 2 in Python

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

471 Views

Suppose we have one encoded string where repetitions of substrings are represented as substring followed by count of substrings. As an example, if the string is "pq2rs2" and k=5, so output will be 'r', this is because the decrypted string is "pqpqrsrs" and 5th character is 'r'. We have to keep in mind that the frequency of encrypted substring can be of more than one digit.So, if the input is like string = "pq4r2ts3" and k = 11, then the output will be i, as the string is pqpqpqpqrrtststsTo solve this, we will follow these steps −encoded := blank stringoccurrence ... Read More

Find Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python

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

289 Views

Suppose we have one binary array. We have to find the position of 0 that can be replaced with 1 to get maximum number of continuous sequence of 1s.So, if the input is like [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1], then the output will be 10, so the array will be [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1].To solve this, we will follow these steps −i := 0, n := size of Acount_left := 0, count_right := 0max_i := -1, last_i := -1count_max := 0while i < ... Read More

Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:43:26

186 Views

Suppose we have two strings S1 and S2 of same lengths, we have to find an index i such that S1[0…i] and S2[i+1…n-1] give a palindrome when they are concatenated together. When it is not possible, return -1.So, if the input is like S1 = "pqrsu", S2 = "wxyqp", then the output will be 1 as S1[0..1] = "pq", S2[2..n-1] = "ypq", then S1 + S2 = "pqyqp" indicates is a palindrome.To solve this, we will follow these steps −n := size of str1str := blank stringfor i in range 0 to n, dostr := str concatenate str1[i]temp := blank ... Read More

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

210 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

142 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

88 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

Advertisements