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
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
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
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
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
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
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
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
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
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