Find Number of Possible Positions in N-Person Line in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:08:03

179 Views

Suppose we have three numbers n, a and b. Consider we are in a line of n people. And we are unaware about our position in the line. But we know there are at least a number of people in front of us and at most b number of people behind us. We have to find the number of possible positions possible for us.So, if the input is like n = 10 a = 3 b = 4, then the output will be 5, because there are 10 people in the line and at least 3 are in front and ... Read More

Find Lexicographically Largest Mountain List in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:06:33

326 Views

Suppose we have three positive numbers say n, lower, and upper. We have to find a list whose length is n and that is strictly increasing and then strictly decreasing and all the numbers are in range [lower and upper] (both inclusive). And each increasing and decreasing parts should be non-empty. We have to find the lexicographically largest such list possible, if this is not possible, then return empty list.So, if the input is like n = 5 lower = 3 upper = 7, then the output will be [6, 7, 6, 5, 4], if we look closely, the [7, ... Read More

Find Maximum Sum by Performing At Most K Negate Operations in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:04:36

277 Views

Suppose we have a list of elements called nums we also have another value k. Now let us consider an operation where we select an element from nums and negate it. We can perform exactly k number of operations. We have to find the maximum resulting sum that can be generated.So, if the input is like nums = [2, 1, -6, -2] k = 3, then the output will be 9, if we negate -6 and -2 and 1 shall get [2, -1, 6, 2] and its sum is 9.To solve this, we will follow these steps −n := size ... Read More

Find Value to Maximize Array Expression in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:01:04

274 Views

Suppose we have two arrays called nums and values, both contains integers and the values of nums are strictly increasing and their lengths are also same. We have to find the value of v for a pair of indices i, j, such that: i ≤ j that maximizes v = values[i] + values[j] + nums[j] - nums[i].So, if the input is like nums = [1, 2, 7] values = [-4, 6, 5], then the output will be 16, if we take pick i = 1 and j = 2 we get 6 + 5 + 7 - 2 = 16.To ... Read More

Find Number of Elements in Matrix Following Row Column Criteria in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:56:54

698 Views

Suppose we have a binary matrix; we have to find the number of elements in matrix that follows the following rules −matrix[r, c] = 1matrix[r, j] = 0 for every j when j is not equal to c and matrix[i, c] = 0 for every i when i is not equal to r.So, if the input is like001100010then the output will be 3, because we have cells (0, 2), (1, 0) and (2, 1) those meet the criteria.To solve this, we will follow these steps −if matrix is empty, thenreturn 0row := a list of sum of all row entries ... Read More

Find K Where K Elements Have Value At Least K in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:54:05

350 Views

Suppose we have a list of numbers called nums, that contains only non-negative numbers. If there are exactly k number of elements in nums that are greater than or equal to k, find the value k. If we cannot find such, then return -1.So, if the input is like nums = [6, 4, 0, 8, 2, 9], then the output will be 4, because there are exactly 4 elements that are greater than or equal to 4: [6, 4, 8, 9].To solve this, we will follow these steps −sort the list nums in reverse orderfor i in range 1 to ... Read More

Find Array of Length K with Minimum Unfairness in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:51:07

614 Views

Suppose we have an array A and another value k. We have to form an array arr whose size is k bu taking elements from A and minimize the unfairness. Here the unfairness is calculated by this formula −(𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟) − (𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟)So, if the input is like A = [25, 120, 350, 150, 2500, 25, 35] and k = 3, then the output will be 10, because we can take elements [25, 25, 35] so max(arr) = 35 and min(arr) = 25. So their difference is 10.To solve this, we will follow these steps −i:= 0sort the ... Read More

Draw a Line Between Two Mouse Clicks Using Tkinter

Dev Prakash Sharma
Updated on 11-Oct-2021 07:45:01

1K+ Views

Consider a case for creating a GUI application such that when we click on the window with a mouse button, it stores the coordinates and creates a line between two given points. Tkinter provides events that allow the user to bind the keys or buttons with the functions.To draw a line between two points, we can follow these general steps, Create a canvas widget and pack it to display in the window.Define a function draw_line() that works as the event when the user does the click event.Create a global variable that counts the number of clicks in the canvas.If the ... Read More

Find Total Duration of K Most Watched Shows in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:41:27

169 Views

Suppose we have a list of list of strings called shows, also have a list of integers called durations, and another value k, here shows[i] and durations[i] represent a show and its duration watched by the ith man, we have to find the total duration watched of the k most watched shows.So, if the input is like shows = ["The BGT", "Jack jumper", "The BGT", "Jokers Company", "Music magic"] durations = [10, 8, 10, 18, 9] k = 2, then the output will be 38, as the top 2 most watched shows are "Jokers Company" and "The BGT" and total ... Read More

Find Expected Value of Given Equation for Random Numbers in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:38:29

298 Views

Suppose we have a number n. Consider x = rand() mod n, where rand() function generates integers between 0 and 10^100 (both inclusive) uniformly at random. And$$Y = \sqrt{x+\sqrt{x+\sqrt{x+\sqrt{x+...}}}}$$We have to find the expected value of Y. The value of n will be in range 1 and 5*10^6.So, if the input is like n = 5, then the output will be 1.696To solve this, we will follow these steps −err := 2235.023971557617max_n := 5 * 10^6pref := a list initially contains a single 0for i in range 1 to 5 * 10^6, doinsert (last item of pref + (1 +(4*i ... Read More

Advertisements