Suppose, we have been provided with two lists of numbers that are nums0 and nums1, and an integer k. Our goal is to find the k largest sum pairs where each pair contains one integer in nums0 and another in nums1. The sum of all of the pairs has to be returned.So, if the input is like nums1 = [8, 6, 12], nums2 = [4, 6, 8], k = 2, then the output will be 38. We have these largest pairs [12, 8] and [12, 6].To solve this, we will follow these steps −if k > len(nums0) * len(nums1) is ... Read More
Suppose we have been provided with a list of numbers nums. We have to find the number of existing quadruplets (a, b, c, d) such that a < b < c < d and nums[a] < nums[b] and nums[c] > nums[d].The array nums is a permutation of the integers 1...NSo, if the input is like nums = [3, 4, 7, 6, 5], then the output will be 5.From the given input, we have these inverted inversions −3, 4, 7, 63, 4, 6, 53, 4, 7, 53, 7, 6, 54, 7, 6, 5To solve this, we will follow these steps −m ... Read More
Suppose we have been provided with an undirected graph that has been represented as an adjacency list, where graph[i] represents node i's neighbor nodes. We have to find the number of edges that satisfies the following condition.If the edge is removed, the graph becomes disconnected.So, if the input is like graph = [ [0, 2], [0, 4], [1, 2, 3], [0, 3, 4], [4], [3], [2] ], then the output will be 1.To solve this, we will follow these steps −Define a function dfs(). This will take curr, pre, dans := infinitydep[curr] := ... Read More
Suppose we have been provided with two positive integers n and d where d is a digit within 0 to 9. We have to determine how many times the digit d appears within the integer numbers between 1 and n.So, if the input is like n = 45, d = 5, then the output will be 5.These numbers have the digit 5: [5, 15, 25, 35, 45].To solve this, we will follow these steps −Define a function solve(). This will take n and d as inputs.if n < 0, thenreturn 0k := floor of (n /10) − 1ans := solve(k, ... Read More
Suppose we have one N x N table of currency exchange rates. We have to check whether there is some sequence of trades we can make or not. Now starting with some amount A of any currency, we can end up with some amount greater than A of that currency. There are no transaction costs and we can also trade fractional quantities.The value at entry [I, j] in this matrix represents the amount of currency j we can buy with one unit of currency i. Now consider currency 0 is USD, 1 is CAD and 2 is EUR. We can ... Read More
Suppose we have graphs as an adjacency lists. This graph is actually a set of disjoint trees. We have to add a certain number of edges to the forest such that it becomes a single tree. We have to return the minimum distance possible of the longest path between any two nodes. So, if the input is likethen the output will be 4.We can add the edge 0 −> 5. Then, the longest path can be any of 3 −> 1 −> 0 −> 5 −> 7 or 4 −> 1 −> 0 −> 5 −> 7; and also these ... Read More
Suppose we have a list of intervals (inclusive) that are potentially overlapping. Now consider there is an operation where we delete one interval, then merge the remaining intervals and then count the number of intervals left over. We have to find the maximum number of leftover intervals possible after removal.So, if the input is like intervals = [ [5, 8], [6, 7], [7, 10], [9, 11]], then the output will be 2. This is because −If we delete the interval [5, 8] we get [6, 11] as the merge.If we delete the interval [6, 7] we get [5, 11] as ... Read More
Suppose, we have three lists whose lengths are same. These are deadlines, credits, and durations. They are representing course assignments. For the i−th assignment deadlines[i] shows its deadline, credits[i] shows its credit, and durations[i] shows the number of days it takes to finish the assignment. One assignment must be completed before starting another one. We have to keep in mind that we can complete an assignment on the day it's due and we are currently on the start of day 0.So, if the input is like, deadlines = [7, 5, 10], credits = [8, 7, 10], durations = [5, 4, ... Read More
Suppose we have a list of numbers called nums, we have to find the maximal value that can be generated by adding any binary operators like +, −, and * between the given numbers as well as insert any valid brackets.So, if the input is like nums = [−6, −4, −10], then the output will be 100, as we can make the expression like: ((−6) + (−4)) * −10.To solve this, we will follow these steps −OPS := a list of operators [+, −, *]N := size of Aif all elements in A is 0, thenreturn 0Define a function dp() ... Read More
Suppose we have a list of distinct words, we have to find the number of different ways we can concatenate two different words from the given list of words to make a palindrome.So, if the input is like words = ["time", "emit", "mo", "m"], then the output will be 3, as we can make "timeemit", "emittime", and "mom".To solve this, we will follow these steps −res := 0ln := number of words in the arrayfor k in range 0 to 1, dofor i in range 0 to ln − 1, dofor j in range i + 1 to ln − ... Read More