Python Articles

Page 582 of 855

Program to Find Minimum Jumps Required to Reach a Value with Different Parity in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 285 Views

Given a list of numbers, we can jump from index i to either i + nums[i] or i - nums[i] (if valid indices). We need to find the minimum jumps required from each position to reach a value with different parity (odd to even or even to odd). If the input is numbers = [7, 3, 4, 5, 6, 9, 6, 7], the output will be [-1, 1, 2, -1, -1, -1, 1, -1]. Understanding the Problem For each starting position, we use BFS to find the shortest path to any number with different parity. Two numbers ...

Read More

Program to find island count by adding blocks into grid one by one in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 220 Views

Suppose we have an infinite grid of water. We can add blocks of land to that grid one by one. We have a list of coordinates called land_requests where each coordinate is in the form [r, c] where r is for row and c is for column. We need to find a list where each element represents the number of islands that exist after adding each block of land from land_requests. For example, if the input is land_requests = [[1, 1], [2, 4], [1, 2], [1, 4], [1, 3]], then the output will be [1, 2, 2, 2, 1]. ...

Read More

Program to find sum of rectangle whose sum at most k in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 390 Views

Given a 2D matrix and a value k, we need to find the largest sum of a rectangle whose sum is at most k. This problem combines matrix manipulation with optimization techniques. Problem Understanding Consider this matrix with k = 15: 5 -2 7 10 We can form rectangles like [5, 7] with sum = 12, [5, -2, 7, 10] with sum = 20, etc. The largest sum ≤ 15 is 12. Algorithm Steps For each pair of rows (i1, i2), compress the matrix vertically Convert the ...

Read More

Program to find largest average of sublist whose size at least k in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 365 Views

Suppose we have a list of numbers called nums and another value k, we have to find the largest average value of any sublist whose length is at least k. So, if the input is like nums = [2, 10, -50, 4, 6, 6] and k = 3, then the output will be 5.33333333, as sublist [4, 6, 6] has the largest average value. Algorithm Steps To solve this, we will follow these steps − Initialize left := minimum of nums, right := maximum of nums Calculate initial sum ...

Read More

Program to find kth lexicographic sequence from 1 to n of size k Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 274 Views

Suppose we have two values n and k. We need to consider a list of numbers in range 1 through n [1, 2, ..., n] and generate every permutation of this list in lexicographic order. For example, if n = 4 we have [1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321]. We have to find the kth value of this permutation sequence as a string. So, if the input is like n = 4, k = 5, then the output will be "1423" ...

Read More

Program to count number of ways we can make a list of values by splitting numeric string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 170 Views

Suppose we have a string s containing digits from 0-9 and another number k. We need to find the number of different ways that s can be represented as a list of numbers from [1, k]. If the answer is very large, return result mod 10^9 + 7. So, if the input is like s = "3456", k = 500, then the output will be 7, as we can represent s like [3, 4, 5, 6], [34, 5, 6], [3, 4, 56], [3, 45, 6], [34, 56], [345, 6], [3, 456]. Algorithm Approach We'll use dynamic programming ...

Read More

Program to find number of coins we can pick from topleft to bottom-right cell and return in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 238 Views

This problem involves finding the maximum coins we can collect by making two simultaneous journeys: one from top-left to bottom-right, and another from bottom-right back to top-left. We can only move right or down in the first journey, and left or up in the return journey. Problem Understanding The matrix contains three types of values: 0 for an empty cell 1 for a coin −1 for a wall (impassable) We need to maximize coin collection while ensuring we can complete both journeys. If either path is blocked, we return 0. Example Consider ...

Read More

Program to check person 1 can win the candy game by taking maximum score or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 404 Views

Suppose two players are playing a candy game where several candies are placed in a line. Person 1 is given a list of numbers called nums representing the point value of each candy. On each player's turn, they can pick 1, 2, or 3 candies from the front of the line, delete them from the list, and add their points to their score. The game ends when all candies are taken, and the player with the higher score wins. We need to determine if person 1 can win this game by playing optimally. Example Scenario If the ...

Read More

Program to check whether first player can take more candies than other or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 235 Views

Suppose we have a list of numbers called candies and two persons are racing to collect the most number of candies. This is a turn-based game where person 1 starts first, and in each turn a player can pick candies from either the front or the back of the list. We need to check whether person 1 can collect more candies than person 2. So, if the input is like candies = [1, 4, 3, 8], then the output will be True. Person 1 can take 8 candies in the first round, and regardless of whether person 2 picks ...

Read More

Program to count number of permutations where sum of adjacent pairs are perfect square in Python

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

Suppose we have a list of numbers called nums. We have to find the number of permutations of nums such that sum of every pair of adjacent values is a perfect square. Two permutations A and B are unique when there is some index i where A[i] is not same as B[i]. So, if the input is like nums = [2, 9, 7], then the output will be 2, as we have [2, 7, 9] and [9, 7, 2]. In both cases: 2+7=9 (perfect square), 7+9=16 (perfect square), and 9+7=16 (perfect square), 7+2=9 (perfect square). Algorithm To ...

Read More
Showing 5811–5820 of 8,546 articles
« Prev 1 580 581 582 583 584 855 Next »
Advertisements