Found 26504 Articles for Server Side Programming

Program to Find Out the Maximum Points From Removals in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:16:35

266 Views

Suppose we are provided with a list of positive numbers. Now, here we can remove any contiguous sub list of some length t with the same value and get points t * t. One condition is to be considered, that we can perform this any number of times until the list is empty. So we have to determine the maximum number of points we can get.So, if the input is like nums = [4, 4, 6, 4, 4], then the output will be 17.For the output, we can first remove the 6 which has length 1 and yields 1 * ... Read More

Program to Find Out the Largest K-Divisible Subsequence Sum in Python

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

434 Views

Suppose we are given a list of non−negative numbers, and a positive value k. We have to find the maximum sum subsequence of numbers such that the sum is divisible by k.So, if the input is like, nums = [4, 6, 8, 2], k = 2, then the output will be 20.The sum of the whole array is 20, which is divisible by 2.To solve this, we will follow these steps −numsSum := sum of the values in input list numsremainder := numsSum mod kif remainder is same as 0, thenreturn numsSumsort the list numsfor each number combination tpl in ... Read More

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

479 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

243 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

168 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

322 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

867 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

211 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

197 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

Advertisements