Programming Articles

Page 498 of 2547

Program to generate matrix where each cell holds Manhattan distance from nearest 0 in Python

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

The Manhattan distance between two points is the sum of absolute differences of their coordinates. In this problem, we need to find the Manhattan distance from each cell to the nearest 0 in a binary matrix. Given a binary matrix, we transform it so each cell contains the Manhattan distance to the closest 0. At least one 0 is guaranteed to exist. Example Input and Output If the input matrix is ? 101 101 110 The output will be ? 101 101 210 The bottom-left cell has distance 2 ...

Read More

Program to find number of combinations of coins to reach target in Python

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

Suppose we have a list of coins and another value amount, we have to find the number of combinations there are that sum to amount. If the answer is very large, then mod the result by 10^9 + 7. So, if the input is like coins = [2, 5] amount = 10, then the output will be 2, as we can make these combinations − [2, 2, 2, 2, 2], [5, 5] Algorithm To solve this, we will follow these steps − m := 10^9 + 7 ...

Read More

Program to find lowest sum of pairs greater than given target in Python

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

Given a list of numbers and a target value, we need to find the lowest sum of any pair that is greater than the target. This problem uses a two-pointer approach on a sorted array for efficient solution. For example, if nums = [2, 4, 6, 10, 14] and target = 10, the output will be 12 (pair: 2 + 10). Algorithm Steps To solve this problem, we follow these steps − Sort the list nums in ascending order Initialize two pointers: left at start (0) and right at end (n-1) Set answer to a ...

Read More

Nested List Weight Sum II in Python

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

The Nested List Weight Sum II problem calculates the weighted sum of integers in a nested list structure, where weights increase from bottom to top. Unlike the regular version where deeper elements have higher weights, here the deepest elements have weight 1, and shallower elements have progressively higher weights. For example, given [[1, 1], 2, [1, 1]], the structure has: Four 1's at depth 2 (weight 1 each) = 4 × 1 = 4 One 2 at depth 1 (weight 2) = 1 × 2 = 2 Total sum = 4 + 2 = 6 Algorithm ...

Read More

Program to find nth term in Look and Say Sequence in Python

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

The Look and Say Sequence is a famous sequence where each term describes the previous term by counting consecutive identical digits. Starting with "1", each subsequent term is generated by reading the previous term aloud. Understanding the Sequence The first few terms of the Look and Say sequence are: Term 1: 1 Term 2: 11 (one 1) Term 3: 21 (two 1s) Term 4: 1211 (one 2, one 1) Term 5: 111221 (one 1, one 2, two 1s) Each term is formed by counting consecutive identical digits in the previous term and stating the ...

Read More

Program to find length of longest substring with character count of at least k in Python

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

Finding the longest substring where each character appears at least k times is a classic divide and conquer problem. We need to recursively split the string at characters that don't meet the frequency requirement. So, if the input is like s = "aabccddeeffghij" k = 2, then the output will be 8, as the longest substring here is "ccddeeff" where every character occurs at least 2 times. Algorithm To solve this, we will follow these steps − Count frequency of all characters in the string If all characters occur at least k times, return the ...

Read More

Program to find length of longest sublist where difference between min and max smaller than k in Python

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

Given a list of numbers and a value k, we need to find the length of the longest sublist where the absolute difference between the largest and smallest element is ≤ k. This is a classic sliding window problem that can be solved efficiently using deques. So, if the input is like nums = [2, 4, 6, 10] and k = 4, then the output will be 3, as we can select [2, 4, 6] where the absolute difference is 4. Algorithm Approach We use a sliding window technique with two deques to track maximum and minimum ...

Read More

Program to find length of longest set of 1s by flipping k bits in Python

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

In this problem, we have a binary list containing only 0s and 1s, and we can flip at most k zeros to ones. Our goal is to find the length of the longest contiguous subarray that contains all 1s after performing these flips. For example, if the input is nums = [0, 1, 1, 0, 0, 1, 1] and k = 2, we can flip the two middle 0s to get [0, 1, 1, 1, 1, 1, 1], resulting in a longest sequence of 6 consecutive 1s. Algorithm Approach We'll use the sliding window technique to solve ...

Read More

Program to find length of longest strictly increasing then decreasing sublist in Python

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

Suppose we have a list of numbers called nums. We have to find the length of the longest sublist (minimum length 3) whose values are strictly increasing and then strictly decreasing. So, if the input is like nums = [8, 2, 4, 6, 3, 1], then the output will be 5, as the sublist [2, 4, 6, 3, 1] is strictly increasing then decreasing. Algorithm To solve this, we will follow these steps − Initialize variables: i = 0, n = size of array, res = -infinity ...

Read More

Program to find length of longest sign alternating subsequence from a list of numbers in Python

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

Suppose we have a list of numbers called nums, we have to find the length of longest subsequence that flips sign on each consecutive number. A sign alternating subsequence means each consecutive pair of numbers has opposite signs (positive followed by negative, or negative followed by positive). So, if the input is like nums = [1, 3, -6, 4, -3], then the output will be 4, as we can pick [1, -6, 4, -3]. Algorithm To solve this, we will follow these steps − pos := 0, neg := 0 ...

Read More
Showing 4971–4980 of 25,466 articles
« Prev 1 496 497 498 499 500 2547 Next »
Advertisements