Python Articles

Page 321 of 855

Program to find number of possible position in n-person line with few person at front and back in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 223 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 for us. So, if the input is like n = 10, a = 3, b = 4, then the output will be 5. There are 10 people in the line with at least 3 people in front and ...

Read More

Program to find lexicographically largest mountain list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 359 Views

A mountain list is a sequence that strictly increases to a peak and then strictly decreases. Given three positive numbers n, lower, and upper, we need to find the lexicographically largest mountain list of length n with all values in range [lower, upper]. The challenge is ensuring both increasing and decreasing parts are non-empty while maximizing the lexicographic order. Problem Analysis For a valid mountain list of length n: Must have at least one increasing element and one decreasing element Total possible unique values = upper − lower + 1 Maximum mountain length = 2 ...

Read More

Program to find maximum sum by performing at most k negate operations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 327 Views

Suppose we have a list of elements called nums and another value k. We can perform an operation where we select an element from nums and negate it. We can perform exactly k number of operations and need to find the maximum resulting sum. So, if the input is like nums = [2, 1, -6, -2] and k = 3, then the output will be 9. If we negate -6 and -2 and 1, we get [2, -1, 6, 2] and its sum is 9. Algorithm To solve this problem, we follow these steps − ...

Read More

Program to find value for which given array expression is maximized in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 308 Views

Suppose we have two arrays called nums and values, both contain integers where the values of nums are strictly increasing and their lengths are the same. We have to find the maximum 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] and values = [-4, 6, 5], then the output will be 16. If we pick i = 1 and j = 2 we get 6 + 5 + 7 - ...

Read More

Program to find number of elements in matrix follows row column criteria in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 748 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] = 1 matrix[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. In simple terms, we need to find cells that contain 1 and are the only 1 in both their row and column. Example Input So, if the input is like ...

Read More

Program to find k where k elements have value at least k in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 374 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 value, 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]. Algorithm To solve this, we will follow these steps − ...

Read More

Program to find array of length k from given array whose unfairness is minimum in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 637 Views

Suppose we have an array A and another value k. We have to form an array of size k by 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. Approach The key insight is that ...

Read More

Program to find total duration of K most watched shows in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 198 Views

Suppose we have a list of strings called shows, a list of integers called durations, and a value k. Here shows[i] and durations[i] represent a show and its duration watched by the ith person. We need 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" with total durations of ...

Read More

Program to count number of intervals which are intersecting at given point in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 371 Views

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

Read More

Program to find expected value of given equation for random numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 337 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.696 Mathematical Background For a nested radical expression $Y = \sqrt{x+\sqrt{x+\sqrt{x+\sqrt{x+...}}}}$, we can solve this algebraically. If we let $Y = \sqrt{x + Y}$, then squaring both sides gives us $Y^2 ...

Read More
Showing 3201–3210 of 8,546 articles
« Prev 1 319 320 321 322 323 855 Next »
Advertisements