Python Articles

Page 311 of 855

Program to find minimum value to insert at beginning for all positive prefix sums in Python

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

Suppose we have a list of numbers called nums. We have to find the minimum positive value that we can insert at the beginning of nums so that the prefix sums of the resulting list are all positive (greater than 0). So, if the input is like nums = [3, -6, 4, 3], then the output will be 4, because if we insert 4 to the list then we have [4, 3, -6, 4, 3]. Now the prefix sums are [4, 7, 1, 5, 8], all are positive. Algorithm To solve this, we will follow these steps ...

Read More

Program to find minimum distance of two given words in a text in Python

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

Finding the minimum distance between two words in a text is a common string processing problem. The distance is measured as the number of words between any occurrence of the two target words. Given a text string and two words w1 and w2, we need to find the smallest number of words between any occurrences of these words. If either word is not present, we return -1. Algorithm The approach involves tracking the most recent positions of both words as we iterate through the text ? Initialize index1 and index2 as None Set distance to ...

Read More

Program to find minimum amplitude after deleting K elements in Python

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

Given a list of numbers and a value k, we need to find the minimum amplitude (difference between maximum and minimum) after removing k elements from the list. So, if the input is like nums = [4, 10, 3, 2, 8, 9] and k = 3, then the output will be 2. If we remove 10, 8, and 9, the remaining elements are [4, 3, 2], where maximum is 4 and minimum is 2, giving us an amplitude of 2. Algorithm To solve this problem, we will follow these steps ? ...

Read More

Program to find maximum product of two distinct elements from an array in Python

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

Given an array of numbers, we need to find the maximum product of two distinct elements. The key insight is that the maximum product can come from either the two largest numbers or the two smallest numbers (if they are both negative). For example, if the input is nums = [8, -3, 1, -5], the output will be 15 because (-3) * (-5) = 15, which is the maximum product possible. Approach To solve this problem, we follow these steps ? Sort the array to get elements in ascending order ...

Read More

Program to find largest product of three unique items in Python

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

Suppose we have a list of numbers called nums, we have to find the largest product of three unique elements. So, if the input is like nums = [6, 1, 2, 4, -3, -4], then the output will be 72, as we can multiply (-3) * (-4) * 6 = 72. Algorithm To solve this, we will follow these steps − Sort the list nums n := size of nums maxScore := -infinity maxScore := maximum of maxScore and ...

Read More

Program to find maximum sum of multiplied numbers in Python

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

Given two lists nums and multipliers, we need to find the maximum sum by pairing and multiplying elements from both lists. We can remove any number from either list and multiply them together, repeating until one list is empty. The key insight is to pair negative multipliers with the smallest numbers and positive multipliers with the largest numbers to maximize the sum. Algorithm To solve this problem, we follow these steps: Sort both lists in ascending order Ensure nums is the longer list (swap if needed) For each multiplier: If multiplier ≤ 0: pair ...

Read More

Program to find maximum distance between empty and occupied seats in Python

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

Suppose we have a list with only 0s and 1s called seats, where seats[i] represents a seat. When it is 1, the seat is occupied; otherwise, it's free. Given that there is at least one free seat and at least one occupied seat, we need to find the maximum distance from a free seat to the nearest occupied seat. For example, if the input is seats = [1, 0, 1, 0, 0, 0, 1], the output will be 2, because we can occupy seat seats[4], and the distance to the nearest occupied seat is 2. Algorithm Approach ...

Read More

Program to count number of BST with n nodes in Python

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

Suppose we have n different nodes, all distinct. We need to find how many ways we can arrange them to form a Binary Search Tree (BST). In a BST, the left subtree always holds smaller values and the right subtree holds greater values. To solve this problem, we use Catalan numbers. The Catalan number C(n) represents the number of structurally different binary search trees with n different keys. Catalan Number Formula The formula for the nth Catalan number is: C(n) = (2n)! / ((n+1)! × n!) Alternatively, it can be calculated as: C(n) = ...

Read More

Program to filter all values which are greater than x in an array

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

Filtering arrays based on a condition is a common task in Python programming. The filter() function provides an efficient way to extract elements that meet specific criteria. In this example, we'll filter numbers that are less than a given threshold value. Problem Statement Given a list of numbers and a threshold value x, we need to find all numbers that are less than x using Python's filter() function. For example, if nums = [1, 5, 8, 3, 6, 9, 12, 77, 55, 36, 2, 5, 6, 12, 87] and x = 50, the output should be [1, ...

Read More

Program to find replicated list by replicating each element n times

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

Suppose we have a list of n elements; we have to repeat each element in the list n number of times. So, if the input is like nums = [1, 5, 8, 3], then the output will be [1, 1, 1, 1, 5, 5, 5, 5, 8, 8, 8, 8, 3, 3, 3, 3] To solve this, we will follow these steps − n := size of nums ret := a new list for each num in nums, do ...

Read More
Showing 3101–3110 of 8,546 articles
« Prev 1 309 310 311 312 313 855 Next »
Advertisements