Found 33676 Articles for Programming

Program to check whether every one has at least a friend or not in Python

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

340 Views

Suppose we have n people represented as a number from 0 to n - 1, we also have a list of friend’s tuples, where friends[i][0] and friends[i][1] are friends. We have to check whether everyone has at least one friend or not.So, if the input is like n = 3 friends = [ [0, 1], [1, 2] ], then the output will be True, as Person 0 is friends of Person 1, Person 1 is friends of Person 0 and 2, and Person 2 is friends of Person 1.To solve this, we will follow these steps −people := a list ... Read More

Program to find most frequent subtree sum of a binary tree in Python

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

203 Views

Suppose we have a binary tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all values under a node, including the node itself.So, if the input is likethen the output will be 3 as it occurs twice − once as the left leaf, and once as the sum of 3 - 6 + 6.To solve this, we will follow these steps −count := an empty mapDefine a function getSum() . This will take nodeif node is null, thenreturn 0mySum := getSum(left of node) + getSum(right of node) + ... Read More

Program to count non-empty subsets where sum of min and max element of set is less than k in Python

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

244 Views

Suppose we have a list of numbers called nums and another value k, we have to find the number of non-empty subsets S such that min of S + max of S K, doj := j - 1if i

Program to find maximum time to finish K tasks in Python

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

380 Views

Suppose we have a matrix of tasks where each row has 3 values. We also have another value k. We have to select k rows from tasks, call it S, such that the following sum is minimized and return the sum as: maximum of (S[0, 0], S[1, 0], ...S[k - 1, 0]) + maximum of (S[0, 1], S[1, 1], ...S[k - 1, 1]) + maximum of (S[0, 2], S[1, 2], ...S[k - 1, 2]) We can also say like: Each of the 3 columns contribute to a cost, and is calculated by taking the max value of that column in ... Read More

Program to find minimum difference of max and mins after updating elements at most three times in Python

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

217 Views

Suppose we have a list of numbers called nums, now consider an operation where we can update an element to any value. We can perform at most 3 of such operations, we have to find the resulting minimum difference between the max and the min value in nums.So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 2, as we can change the list to [4, 3, 4, 5, 4, 4] and then 5 - 3 = 2.To solve this, we will follow these steps −if size of nums

Program to find minimum difference between two elements from two lists in Python

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

2K+ Views

Suppose we have two lists L1 and L2, we have to find the smallest difference between a number from L1 and a number from L2.So, if the input is like L1 = [2, 7, 4], L2 = [16, 10, 11], then the output will be 3, as the smallest difference is 10 - 7 = 3.To solve this, we will follow these steps −sort the list L1 and sort the list L2ans := infinityi := 0, j := 0while i < size of L1 and j < size of L2, doans := minimum of ans and |L1[i] - L2[j]|if L1[i] ... Read More

Program to find minimum number of deletions required from two ends to make list balanced in Python

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

210 Views

Suppose we have a list containing 0s and 1s, we have to remove values from the front or from the back of the list. Finally, we have to find the minimum number of deletions required such that the remaining list has an equal number of 0s and 1s.So, if the input is like nums = [1, 1, 1, 0, 0, 1], then the output will be 2, as we can delete the first one 1 and last one 1 so that there's two 1s and two 0s.To solve this, we will follow these steps −longest := 0d := a map ... Read More

Program to merge two binary trees in C++

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

206 Views

Suppose we have two binary trees and consider that when we put one of them to cover the other, some nodes of the two trees are overlapped while the others are overlapping. We have to merge them into a new binary tree. The merge rule is like that if two nodes are overlapping, then sum node values up as the new value of the merged node. Otherwise, the non-empty node will be used as the node of new tree.So if the trees are −Then the output will be −To solve this, we will follow these steps −The method is solve(). ... Read More

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

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

238 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

328 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

Advertisements