Found 10476 Articles for Python

Program to find maximum length of non-sharing words in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:26:17

236 Views

Suppose we have a list of lowercase alphabetical strings called words, we have to find the maximum sum of the lengths of two distinct words that do not share a common letter. So, if the input is like words = ["abcd", "mno", "abdcmno", "amno"], then the output will be 7, as the words do not share any common letters are ["abcd", "mno"], total length is 7.To solve this, we will follow these steps −Define a function sign() . This will take wordvalue := 0for each c in word, dovalue := value OR (2^(ASCII of c - ASCII of 'a'))return valueFrom ... Read More

Program to find maximum sum of non-adjacent nodes of a tree in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:24:39

326 Views

Suppose we have a binary tree, we have to find the maximum sum of the values that can be obtained given no two values can be adjacent parent to child.So, if the input is likethen the output will be 17 as 10, 4, 3 are not adjacent to each other.To solve this, we will follow these steps −Define a function f() . This will take nodeif node is null, thenreturn (0, 0)(a, b) := f(left of node)(c, d) := f(right of node)return a pair (maximum of value of node + b + d and a + c, a + c)from ... Read More

Program to maximize the number of equivalent pairs after swapping in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:21:26

231 Views

Suppose we have a list of numbers A and list of numbers B of same length. We also have a 2D list of numbers C where each element is of the form [i, j] this indicates we can swap A[i] and A[j] as many times as we want. We have to find the maximum number of pairs where A[i] = B[i] after the swapping.So, if the input is like A = [5, 6, 7, 8], B = [6, 5, 8, 7], C = [[0, 1], [2, 3]], then the output will be 4, as we can swap A[0] with A[1] ... Read More

Program to maximize the minimum value after increasing K sublists in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:19:15

242 Views

Suppose we have a list of numbers called nums and two values, size and k. Now suppose there is an operation where we take a contiguous sublist of length size and increment every element by one. We can perform this operation k times, we have to find the largest minimum value possible in nums.So, if the input is like nums = [2, 5, 2, 2, 7], size = 3, k = 2, then the output will be 3, as we can increase [2, 5, 2] to get [3, 6, 3, 2, 7] and then increment [6, 3, 2] to get ... Read More

Program to make pairwise adjacent sums small in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:16:59

337 Views

Suppose we have a list of non-negative numbers say nums and a non-negative value k. Now suppose we can perform an operation where we select a single positive umber in nums and decrease it by 1. We have to find the minimum number of operations required such that every pair of adjacent values in the list sums

Program to find number of coins needed to make the changes with given set of coins in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:15:15

265 Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 64, the output is 14. This is formed using 12*5 + 2 + 2 = 64.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Program to find number of coins needed to make the changes in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:13:36

1K+ Views

Suppose we have coins of different denominations (1, 5, 10, 25) and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. So if the input is 64, the output is 7. This is formed using 25 + 25 + 10 + 1 + 1 + 1 + 1 = 64.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then return -1define one array called dp, of size amount + 1, ... Read More

Program to convert one list identical to other with sublist sum operation in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:11:05

165 Views

Suppose we have two lists l1 and l2, we have to make the lists equal by applying this operation repeatedly − Choose a sublist, and replace the whole sublist with its sum. Finally return the size of the longest resulting list possible after applying above operations. If there's no solution, return -1.So, if the input is like l1 = [1, 4, 7, 1, 2, 10] l2 = [5, 6, 1, 3, 10], then the output will be 4, as if we perform this operation as follows −Take l1's sublist [1, 4] we get [5, 7, 1, 2, 10]Take l1's sublist ... Read More

Program to check minimum number of characters needed to make string palindrome in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:04:58

488 Views

Suppose we have a string s, we have to find the minimum number of characters needed to be inserted so that the string becomes a palindrome.So, if the input is like s = "mad", then the output will be 2, as we can insert "am" to get "madam".To solve this, we will follow these steps −Define a function dp(). This will take i, jif i >= j, thenreturn 0if s[i] is same as s[j], thenreturn dp(i + 1, j - 1)otherwise, return minimum of dp(i + 1, j) and dp(i, j - 1) + 1From the main method, do the ... Read More

Program to find an ancestor which is common of two elements in a binary tree in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:01:59

246 Views

Suppose we have a binary tree, and we also have two numbers a and b, we have to find the value of the lowest node that has a and b as descendants. We have to keep in mind that a node can be a descendant of itself.So, if the input is likea = 6, b = 2, then the output will be 4To solve this, we will follow these steps −Define a method solve() this will take root and a, bif root is null, thenreturn -1if value of root is either a or b, thenreturn value of rootleft := solve(left ... Read More

Advertisements