Find Array of Length K with Minimum Unfairness in Python

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

600 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

160 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

285 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

Count Intervals Intersecting at a Given Point in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:37:17

326 Views

Suppose we have a list of intervals and a value called point. Each interval interval[i] contains [si, ei] represents start time and end time of interval i (both inclusive). We have to find the number of intervals that are intersecting at given point.So, if the input is like intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] point = 5, then the output will be 3, because at time 5, there are 3 intervals those are [3, 6], [4, 10], [5, 9]To solve this, we will follow these steps βˆ’count := 0for each start time i and end time j in intervals, doif point >= i and point = i and point

Insert New Element into Linked List Before Given Position in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:35:37

1K+ Views

Suppose we have a list of elements; these elements are stored in a singly linked list. We also have a value pos and value val. We have to insert val before index pos of the linked list.So, if the input is like nums = [1, 5, 3, 6, 8] pos = 3 val = 7, then the output will be [1, 5, 3, 7, 6, 8]To solve this, we will follow these steps βˆ’new := create a linked list node with value same as valif pos is same as 0, thennext of new := list_headreturn newtemp := list_headwhile temp is ... Read More

Find Size of Common Special Substrings in Python

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

169 Views

Suppose we have two strings s1 and s2. We have to find the size of longest string s3 which is special substring of both s1 and s2.We can say a string x is special substring of another string y if x can be generated by removing 0 or more characters from y.So, if the input is like s1 = 'pineapple' s2 = 'people', then the output will be 5 as the special substring is 'peple', of size 5.To solve this, we will follow these steps βˆ’prev := a new dictionary, where if some key is not present, return 0for i ... Read More

Find Index Where Left and Right Sums are Equal in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:28:14

698 Views

Suppose we have a list of items called nums, we have to find the smallest index i such that the sum of the numbers which are present at the left of i is equal to the sum of numbers present at right of i. If we cannot find any such solution, return -1.So, if the input is like nums = [8, 2, 3, 6, 5, 2, 5, 9, 1, 2], then the output will be 4, because sum of elements that are left of index 4 is [8, 2, 3, 6] = 19, and sum of elements that are present ... Read More

Find Highest Common Factor of a List of Elements in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:26:33

4K+ Views

Suppose we have a list of elements called nums, we have to find the largest positive value that divides each of the integers.So, if the input is like nums = [15, 81, 78], then the output will be 3, as 3 is the largest integer that divides all 15, 81, and 78.To solve this, we will follow these steps βˆ’if size of nums is same as 1, thenreturn nums[0]div := gcd of nums[0] and nums[1])if size of nums is same as 2, thenreturn divfor i in range 1 to size of nums - 2, dodiv := gcd of div and ... Read More

Check if N Can Be Represented as Sum of K Primes in Python

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

693 Views

Suppose we have two inputs n and k. We have to check whether n can be represented as a sum of k number of prime values or not.So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17.To solve this, we will follow these steps βˆ’if n < k*2, then return Falseif k > 2, then return Trueif k is same as 2, thenif n is even, then return Trueif (n-2) is prime, then return Truereturn Falseif n is prime, then return ... Read More

Advertisements