Found 33676 Articles for Programming

Program to Find Out the Maximum Final Power of a List in Python

Arnab Chakraborty
Updated on 23-Dec-2020 07:03:03

235 Views

Suppose, we have a list and the power of a list is defined by the sum of (index + 1) * value_at_index over all indices. Alternatively, we can represent it like this −$$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$Now, we have a list nums that has N positive integers. We can select any singular value in the list, and move (not swap) it to any position, it can be shifted to the beginning of the list, or to the end. We can also choose to not move any position at all. We have to find the maximum possible final power of the list. The ... Read More

Program to Find Out the Minimum Cost Possible from Weighted Graph in Python

Arnab Chakraborty
Updated on 23-Dec-2020 07:00:57

781 Views

Suppose we have a 2D list of integers called edges which are a representation of an undirected graph. Every row in the input represents an edge [u, v, w] meaning nodes u and v are connected and the edge has the weight w. The graph consists of n nodes from 0 to n-1.The cost of a path is defined here as the product of the number of edges and the maximum weight for any edge in the path. We have to find out the minimum cost possible from node 0 to node n-1, or we declare the answer as -1 ... Read More

Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:58:19

215 Views

Suppose we have a string s which represents an equation of the form x+y=z. We have to find the minimum number of digits that we need to add into s so the equation becomes true.So, if the input is like s = '2+6=7', then the output will be 2.We can turn the equation into "21+6=27" by inserting "1" and "2". So the total number of corrections required is 2.To solve this, we will follow these steps −split s into parts based on "+" character, put left part into A and right part into restsplit rest into parts based on "=" ... Read More

Program to Find Out the Points Achievable in a Contest in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:55:53

219 Views

Suppose we are in a programming contest where there are multiple problems but the contest ends when we solve one problem. Now if we have two lists of numbers of the same length called points, and chances. To explain it, here for the ith problem, we have a chances[i] percent chance of solving it for points[i] points. We also have another value k which represents the number of problems we can attempt. The same problem cannot be attempted twice.If we devise an optimal strategy, we will have to find the expected value of the number of points we can get ... Read More

Program to Find Out the Minimum Cost to Purchase All in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:53:54

346 Views

Suppose, we have N items, which are marked as 0, 1, 2, …, N-1. Then, we are given a 2D list of size S called sets. Here, we can purchase the i-th set for the price sets[i, 2], and we receive every item between sets[i, 0] to sets[i, 1]. Also, we have a list of size N called removals, where we can throw away 1 instance of the i-th element for the price removals[i]. So, we have to find out the minimum cost to purchase precisely one of every element from 0 to N-1 inclusive, or the result is -1 ... Read More

Program to Find Out the Cost after Finding k Unique Subsequences From a Given String in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:51:32

200 Views

Suppose, we have a string s and another value k. We have to select some subsequences of s, so that we can get k unique subsequences. Here, the cost of selecting a subsequence equals the length of (s) - length of (subsequence). So, we have to find the lowest total cost possible after selecting k unique subsequences. If we are unable to find out this set, we will return -1. We will consider the empty string as a valid subsequence.So, if the input is like s = "pqrs", k = 4, then the output will be 3.To solve this, we ... Read More

Program to Find Out the Strings of the Same Size in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:49:48

148 Views

Suppose, we have a string 'i' consisting of lowercase letters and another integer 'j'. We have to find out how many strings there are that are equal to the size of 'i' and are lexicographically smaller or equal to 'i' and having no consecutive equal characters greater than 'j'.The answer has to calculated by finding the Mod the result by 10 ^ 9 + 7.So, if the input is like i = "app", j = 2, then the output will be 405.To solve this, we will follow these steps −if j j is non-zero, thenreturn 0if pos is same ... Read More

Program to Find Out the Number of Squares in a Grid in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:48:11

659 Views

Suppose we have two values p and q, we have to find the number of unique squares that can be generated from a grid with p rows and q columns in which the points are placed evenly. If the answer is very large return result mod 10^9 + 7. In this problem, a square is a set of 4 points that form the four vertices of a square. The sides of the square must have the same length, and it does not always have the need to be aligned with the axes of the grid.So, if the input is like ... Read More

Program to Find Out the Special Nodes in a Tree in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:46:36

552 Views

Suppose we have a 2D list of values called 'tree' which represents an n-ary tree and another list of values called 'color'. The tree is represented as an adjacency list and its root is tree[0].The characteristics of an i-th node −tree[i] is its children and parent.color[i] is its color.We call a node N "special" if every node in the subtree whose root is at N has a unique color. So we have this tree, we have to find out the number of special nodes.So, if the input is like tree = [    [1, 2],    [0],    [0, 3], ... Read More

Program to Find Out if There is a Short Circuit in Input Words in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:43:44

133 Views

Suppose we have a list of words. We have to check the given words can be chained to form a circle. A word A can be placed in front of another word B in a chained circle if only the last character of A is identical to the first character of B. Every word has to be used and can be used only once (the first/last word will not be considered).So, if the input is like words = ["ant", "dog", "tamarind", "nausea", "gun"], then the output will be True.To solve this, we will follow these steps −graph := a new ... Read More

Advertisements