Found 10476 Articles for Python

Program to count minimum invalid parenthesis to be removed to make string correct in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:45:21

443 Views

Suppose we have a string of parentheses; we have to write a function to compute the minimum number of parentheses to be removed to make the string correct (each open parenthesis is eventually closed).So, if the input is like "(()))(", then the output will be 2, as the correct string is "(())", remove ")(".To solve this, we will follow these steps −total := 0, temp := 0for each p in s, doif p is same as "(", thentotal := total + 1otherwise when p is same as ")" and total is not 0, thentotal := total - 1otherwise, temp := ... Read More

Program to remove duplicate entries in a linked list in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:34:42

211 Views

Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list.So, if the input is like [2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9], then the output will be [2 -> 4 -> 6 -> 1 -> 9].To solve this, we will follow these steps −if node is not null, thenl := a new settemp := nodeinsert value of temp into lwhile ... Read More

Program to update elements in a given range in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:31:20

335 Views

Suppose we have a list of numbers called nums and a list of operations. Here each operation has three fields [L, R, X], this indicated that we should increment by X all the elements from indices L to R (inclusive). We have to apply all operations and return the final list.So, if the input is like nums = [8, 4, 2, -9, 4] operations = [ [0, 0, 3], [1, 3, 2], [2, 3, 5] ], then the output will be [11, 6, 9, -2, 4], as the initial list was [8, 4, 2, -9, 4].Performing first operation [0, 0, ... Read More

Program to find how many total amount of rain we can catch in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:45:17

136 Views

Suppose we have an array of n non-negative integers. These are representing a height where the width of each bar is 1, we have to compute how much water it is able to catch after raining. So the map will be like −Here we can see there are 8 blue boxes, so the output will be 8.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := stack ... Read More

Program to find maximum product of contiguous subarray in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:43:33

1K+ Views

Suppose we have an array called nums, we have to find the product of elements of a contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [1, 9, 2, 0, 2, 5], the output will be 18, as contiguous subarray [1, 9, 2] has max product.To solve this, we will follow these steps −max_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0for i in range 1 to length of numsmax_list[i] ... Read More

Program to find maximum value we can get in knapsack problem by taking multiple copies in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:39:39

598 Views

Suppose we have two lists of same length they are called weights and values, and we also have another value capacity. Here weights[i] and values[i] represent the weight and value of the ith item. If we can take at most capacity weights, and that we can take any number of copies for each item, we have to find the maximum amount of value we can get.So, if the input is like weights = [1, 2, 3], values = [1, 5, 3], capacity = 5, then the output will be 11To solve this, we will follow these steps −Define a function ... Read More

Program to count number of ways we can distribute coins to workers in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:36:17

335 Views

Suppose we have two lists of positive numbers called coins and salaries. Here coins[i] indicates the value for coin i and salaries[j] indicates the least amount of salary required to pay for worker j. Now suppose we have one coin per type and we must give each worker exactly one coin, we have to compute the number of ways to give coins to each worker. Here two ways are different if some worker receives one type of coin in one way but a different type of coin in the other way. If the result is very large return result mod ... Read More

Program to count how many ways we can divide the tree into two trees in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:31:58

200 Views

Suppose we have a binary tree containing values 0, 1 and 2. The root has at least one 0 node and one 1 node. Now suppose there is an operation where we delete an edge in the tree and the tree becomes two different trees. We have to find the number of ways we can delete one edge such that none of the two trees contain both a 0 node and a 1 node.So, if the input is likethen the output will be 1 as we can only delete the 0 to 2 edge.To solve this, we will follow these ... Read More

Program to find leaf and non-leaf nodes of a binary tree in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:27:36

1K+ Views

Suppose we have a binary tree, we have to find a list of two numbers where the first number is the count of leaves in the tree and the second number is the count of non-leaf nodes.So, if the input is likethen the output will be (3, 2), as there are 3 leaves and 2 non-leaf nodes.To solve this, we will follow these steps −if n is null, thenreturn (0, 0)if left of n is null and right of n is null, thenreturn (1, 0)left := solve(left of n)right := solve(right of n)return (left[0] + right[0], 1 + left[1] + ... Read More

Program to check whether inorder sequence of a tree is palindrome or not in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:25:11

265 Views

Suppose we have a binary tree where each node contains a digit from 0-9, we have to check whether its in-order traversal is palindrome or not.So, if the input is likethen the output will be True, as its inorder traversal is [2, 6, 10, 6, 2].To solve this, we will follow these steps −if root is null, thenreturn Truestack := a new stackcurr := rootinorder := a new listwhile stack is not empty or curr is not null, dowhile curr is not null, dopush curr into stackcurr := left of currnode := popped element from stackinsert value of node at ... Read More

Advertisements