Server Side Programming Articles - Page 1399 of 2650

Program to Find the longest subsequence where the absolute difference between every adjacent element is at most k in Python.

Arnab Chakraborty
Updated on 15-Dec-2020 13:12:14

488 Views

Suppose we are given a list of numbers and another value k. This time our task is to find the length of the longest subsequence where the absolute difference between every adjacent element is at most k.So, if the input is like nums = [5, 6, 2, 1, −6, 0, −1, k = 4, then the output will be 6.To solve this, we will follow these steps −Define a function update() . This will take i, xi := i + nwhile i is non−zero, dosegtree[i] := maximum of segtree[i], xi := i / 2Define a function query() . This will ... Read More

Program to Find K-Largest Sum Pairs in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:09:00

255 Views

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

Program to Find the Inverted Inversions in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:06:52

171 Views

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

Program to Find Out the Edges that Disconnect the Graph in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:04:55

329 Views

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

Program to Find Out the Occurrence of a Digit from a Given Range in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:01:25

902 Views

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

Program to Find Out Currency Arbitrage in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:00:12

1K+ Views

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

Program to Connect a Forest in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:57:59

219 Views

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

Program to Find Out the Best Interval to Remove in C++

Arnab Chakraborty
Updated on 15-Dec-2020 12:54:00

208 Views

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

Program to find out the efficient way to study in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:51:27

105 Views

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

Program to find maximum value by inserting operators in between numbers in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:49:50

357 Views

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

Advertisements