Count Trailing Zeros of Minimum Number Divisible by 1 to K in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:56:38

168 Views

Suppose we have a number k, now consider the smallest positive integer value x where all values from 1 to k divide evenly. In other words, consider the smallest value x where x is divisible by all numbers from 1 through k. We have to find the number of trailing zeroes in x.So, if the input is like k = 6, then the output will be 0, as the smallest x here is 60, 60 can be divided using 1, 2, 3, 4, 5 and 6. There is only one trailing zeroes in 60.To solve this, we will follow these steps −res := 0x := 1while x * 5

Find Top View of a Binary Tree in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:55:52

380 Views

Suppose we have a binary tree, we have to find the top view of the tree, they will be sorted left−to−right.So, if the input is like image, then the output will be [3, 5, 8, 6, 9], as 3 is above 2 and 5 is above 7 so they are not visible.To solve this, we will follow these steps −view := a new empty mapq := a double ended queueinsert pair (root, 0) at the end of qstart := inf, end := −infwhile q is not empty, do(node, coord) := left element of q, then remove left element of qstart ... Read More

Minimum Steps to Meet All Persons at Any Cell in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:53:06

209 Views

Suppose we have a 2D matrix where these values are present: 0 represents an empty cell. 1 represents a wall. 2 represents a person. Now a person can walk any of the four direction of up, down, left, right otherwise stay in one time unit. We have to find a walkable cell such that it minimizes the time it would take for everyone to meet and return the time. We have to keep in mind that two people can walk through the same empty cell and you can assume there is always some path between any two people.So, if the ... Read More

Find Minimum Steps to Catch Opponent in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:49:05

259 Views

Suppose we have a list of tree edges in the form [u, v], this indicates there is an undirected edge between u and v. And we also have two values x and y. If we are at node x, and our opponent is at node y. In the first round, we move, then in the next round the opponent moves and so on. The opponent can select to not make a move in a round. We have to find the minimum number of rounds it we need to catch the opponent.So, if the input is like edges = [[0, 1], ... Read More

Minimum Number of Swaps to Arrange Pairs of Socks in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:46:28

211 Views

Suppose we have a list of numbers called row and this is representing socks lying in a row. They are not sorted, but we want to rearrange them so that each pair of socks are side by side like (0, 1), (2, 3), (4, 5), and so on. We have to find the minimum number of swaps required to rearrange them.So, if the input is like row = [0, 5, 6, 2, 1, 3, 7, 4], then the output will be 2, as the row orders are[0, 5, 6, 2, 1, 3, 7, 4][0, 1, 6, 2, 5, 3, 7, ... Read More

Sum of Medians of All Odd Length Sublists in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:44:53

302 Views

Suppose we have a list of numbers called nums, we have to find the sum of the medians of every odd−length sublist of the given list.So, if the input is like nums = [2, 4, 6, 3], then the output will be 23, as the odd−length sublists are − [2], [4], [6], [3], [2, 4, 6], [4, 6, 3], so the sum of the medians is 2 + 4 + 6 + 3 + 4 + 4 = 23To solve this, we will follow these steps −ret := 0for initialize i := 0, when i < size of nums, update ... Read More

Find the Shortest Distance Between Two Points in C++

Arnab Chakraborty
Updated on 23-Dec-2020 07:04:51

2K+ Views

Suppose we have a list of coordinates where each element is of the form [x, y], representing Euclidean coordinates. We have to find the smallest squared distance (x1 - x2) 2 + (y1 - y2) 2 between any two provided coordinates.So, if the input is like coordinates = {{1, 2}, {1, 4}, {3, 5}}, then the output will be 4.To solve this, we will follow these steps −Define one map ytorightmostxsort the array coordinatesret := infinityfor each p in cordinates −it = return the value where (p[1] - sqrt(ret)) is in ytorightmostx or the smallest value greater than it from ... Read More

Find Maximum Final Power of a List in Python

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

258 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

Find Minimum Cost from Weighted Graph in Python

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

806 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

Find Number of Corrections to Fix an Equation in Python

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

240 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

Advertisements