Server Side Programming Articles

Page 285 of 2109

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 328 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 310 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 749 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 375 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 639 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 200 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 372 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 338 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

Program to insert new element into a linked list before the given position in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

Inserting a new element into a linked list at a specific position is a common operation in data structures. This involves creating a new node and adjusting the links between existing nodes to maintain the list structure. 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]. Algorithm To solve this, we will follow these steps − new := create a linked list node with value same as val if pos is same as 0, ...

Read More

Program to find size of common special substrings of two given strings in Python

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

Suppose we have two strings s1 and s2. We have to find the size of longest string s3 which is a special substring of both s1 and s2. We can say a string x is a special substring of another string y if x can be generated by removing 0 or more characters from y. This is also known as the Longest Common Subsequence (LCS) problem. So, if the input is like s1 = 'pineapple' and s2 = 'people', then the output will be 5 as the special substring is 'peple', of size 5. Algorithm To ...

Read More
Showing 2841–2850 of 21,090 articles
« Prev 1 283 284 285 286 287 2109 Next »
Advertisements