Python Articles

Page 332 of 855

Program to find maximum value at a given index in a bounded array in Python

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

Suppose we have three values: n, index and maxSum. We need to construct an array nums and find the maximum possible value at nums[index] that satisfies the following conditions: Size of nums is n All elements in nums are positive |nums[i] - nums[i+1]|

Read More

Program to find partition array into disjoint intervals in Python

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

Given an array nums, we need to partition it into two disjoint subarrays called left and right such that: Each element in left subarray is less than or equal to each element in right subarray Both left and right subarrays are non-empty Left subarray has the smallest possible size We have to find the length of left after such a partitioning. So, if the input is like nums = [5, 0, 3, 8, 6], then the output will be 3 because left array will be [5, 0, 3] and right subarray will be [8, 6]. ...

Read More

Program to find maximum number of consecutive values you can make in Python

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

Suppose we have an array called coins with n elements, representing the coins that we own. The value of the ith coin is denoted as coins[i]. We can make some value x if we can select some of our n coins such that their values sum up to x. We have to find the maximum number of consecutive values that we can get with the coins starting from and including 0. So, if the input is like coins = [1, 1, 3, 4], then the output will be 10, because ? 0 = [] (empty selection) 1 ...

Read More

Program to find out the number of accepted invitations in Python

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

This is a bipartite matching problem where we need to find the maximum number of boy-girl pairs for a party. Each boy can invite multiple girls, but each girl can accept only one invitation. We'll use a depth-first search (DFS) approach to solve this matching problem. Problem Understanding Given an m × n matrix where m boys invite n girls: Matrix[i][j] = 1 means boy i sent an invitation to girl j Matrix[i][j] = 0 means no invitation was sent Each girl can accept at most one invitation We need to find the maximum number of ...

Read More

Program to find out the number of pairs of equal substrings in Python

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

Suppose we are given two strings, both made of lowercase alphabets. We have to find out the number of quadruples (p, q, r, s) satisfying the given conditions − 0

Read More

Program to sort out phrases based on their appearances in Python

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

Suppose we are given two lists: phrases that contains selected phrases and sentences that contains several sentences. We need to find which phrases appear in the sentences and sort the phrases based on their frequency of appearances. The phrase with the most appearances comes first. So, if the input is like phrases = ['strong', 'durable', 'efficient'], sentences = ['the product is durable and efficient', 'strong and durable', 'it is efficient', 'like it because it is efficient'], then the output will be ['efficient', 'durable', 'strong'] The phrase 'efficient' appears in 3 sentences (indices 0, 2, and 3), making it ...

Read More

Program to find out the buildings that have a better view in Python

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

Suppose, we are provided with an array that contains the heights of different buildings. The buildings are located on a line, and a building has a better view if it is not obstructed by another taller building. So provided the array containing heights, we have to find out the buildings that do not have other taller buildings to obstruct the view from them. The indices are returned from the array that satisfies the criteria. So, if the input is like height = [5, 6, 8, 7], then the output will be [2, 3]. The buildings in array index 0 ...

Read More

Program to design a queue that moves the most recently used element to the end of it in Python

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

We need to design a queue that moves the most recently used element to the end. The queue is initialized with integers 1 to n, and we implement a function that moves an element from a given position to the end while returning that element's value. For example, with n = 5 (queue contains [1, 2, 3, 4, 5]) and move operations at positions 5, 2, 3, and 1, the output will be 5, 2, 4, 1 respectively. Algorithm Steps To solve this efficiently, we use a square root decomposition approach − Find the correct ...

Read More

Program to find out the sum of the maximum subarray after a operation in Python

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

Given an array of integers, we can perform one operation where we replace any element array[i] with its squared value array[i] * array[i]. Our goal is to find the maximum possible subarray sum after performing this operation. So, if the input is like array = [4, 1, -2, -1], then the output will be 17. If we replace the value in array[0] with its squared value, the array becomes [16, 1, -2, -1]. The maximum subarray from this is [16, 1], which has the sum 16 + 1 = 17. Algorithm We use dynamic programming with ...

Read More

Program to restore the array from adjacent pairs in Python

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

Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] representing that the elements ui and vi are adjacent in an array called nums. In nums there are n unique elements. We have to find the array nums. So, if the input is like adPair = [[3, 2], [4, 5], [4, 3]], then the output will be [2, 3, 4, 5]. Algorithm To solve this, we will follow these steps ? my_map := an empty map to store adjacency list for different ...

Read More
Showing 3311–3320 of 8,546 articles
« Prev 1 330 331 332 333 334 855 Next »
Advertisements