Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to find minimum number colors remain after merging in Python

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

Suppose we have a list of colors (R, G, B). When two different colors are adjacent, they can merge into a single color item of the third color. We need to find the minimum number of colors remaining after any possible sequence of such transformations. So, if the input is like colors = ["G", "R", "G", "B", "R"], then the output will be 1 as it can transform like below ? Initial: G R G ...

Read More

Program to find land with longest distance from water in Python

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

When given a binary matrix where 0 represents water and 1 represents land, we need to find the land cell with the longest Manhattan distance from any water cell. The Manhattan distance is the sum of absolute differences of coordinates. So, if the input matrix is ? 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 Then the output will be 3, as the cell at [0, 0] has a Manhattan distance of 3 from the nearest water. Algorithm ...

Read More

Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python

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

Suppose we have a number n, we have to find the number of ways we can fill a (3 x n) block with 2 x 1 dominos. We can rotate the dominos when required. If the answer is very large then return this mod 10^9 + 7. For example, if the input is n = 4, then the output will be 11. Algorithm To solve this problem, we need to use dynamic programming. The key insight is that we can only fill a 3×n grid if n is even, because each domino covers 2 cells. We ...

Read More

Program to find nth term of a sequence which are divisible by a, b, c in Python

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

Given four numbers n, a, b, and c, we need to find the nth (0-indexed) term of the sorted sequence of numbers divisible by a, b, or c. For example, if n = 8, a = 3, b = 7, c = 9, the first 9 terms of the sequence are [3, 6, 7, 9, 12, 14, 15, 18, 21], so the 8th term (0-indexed) is 18. Approach We use binary search with the inclusion-exclusion principle to count numbers divisible by a, b, or c up to a given value ? If minimum of a, ...

Read More

Program to find length of the largest subset where one element in every pair is divisible by other in Python

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

Given a list of unique numbers, we need to find the largest subset where every pair of elements satisfies the divisibility condition: either i % j = 0 or j % i = 0. This means one element must divide the other in every pair. For example, if the input is nums = [3, 6, 12, 24, 26, 39], the output will be 4 because the largest valid subset is [3, 6, 12, 24] where 6 is divisible by 3, 12 is divisible by 6, and 24 is divisible by 12. Algorithm We use dynamic programming with ...

Read More

Program to find number of sublists that contains exactly k different words in Python

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

Finding sublists that contain exactly k different words is a common problem in data analysis and text processing. We can solve this using the sliding window technique with a helper function approach. Problem Understanding Given a list of words and a value k, we need to count all sublists (contiguous subarrays) that contain exactly k distinct words. For example, with words = ["Kolkata", "Delhi", "Delhi", "Kolkata"] and k = 2, we find 5 sublists with exactly 2 unique words ? ["Kolkata", "Delhi"] ["Delhi", "Kolkata"] ["Kolkata", "Delhi", "Delhi"] ["Delhi", "Delhi", "Kolkata"] ["Kolkata", "Delhi", "Delhi", "Kolkata"] ...

Read More

Program to find how long it will take to reach messages in a network in Python

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

In network communication, messages propagate through nodes with varying transmission times. This problem finds the minimum time needed for a message starting at node 0 to reach all nodes in an undirected network graph. Given n nodes labeled 0 to n, and edges in the form (a, b, t) where t is the transmission time between nodes a and b, we need to find how long it takes for every node to receive the message. Problem Analysis If the input is n = 3 and edges = [[0, 1, 3], [1, 2, 4], [2, 3, 2]], the ...

Read More

Program to find number of steps required to change one word to another in Python

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

Suppose we have a list of words called dictionary and we have another two strings start and end. We want to reach from start to end by changing one character at a time and each resulting word should also be in the dictionary. Words are case-sensitive. So we have to find the minimum number of steps it would take to reach at the end. If it is not possible then return -1. So, if the input is like dictionary = ["may", "ray", "rat"] start = "rat" end = "may", then the output will be 3, as we can select ...

Read More

Program to check whether odd length cycle is in a graph or not in Python

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

In graph theory, an odd-length cycle is a cycle that contains an odd number of vertices. To detect such cycles in an undirected graph, we can use Depth-First Search (DFS) with path tracking to identify back edges that form odd cycles. Problem Understanding Given an undirected graph represented as an adjacency list, we need to determine if there exists any cycle with an odd number of vertices. For example, cycles like [1, 3, 4] (length 3) or [0, 1, 3, 4, 2] (length 5) are odd-length cycles. 0 ...

Read More

Program to get indices of a list after deleting elements in ascending order in Python

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

Suppose we have a list of distinct values and we want to remove each number in non-decreasing order. We have to find the indices of numbers in order of their deletion. So, if the input is like nums = [4, 6, 2, 5, 3, 1], then the output will be [5, 2, 3, 0, 1, 0]. We delete 1 (at index 5), so array becomes [4, 6, 2, 5, 3], then remove 2 (at index 2), array becomes [4, 6, 5, 3], then remove 3 (at index 3) to get [4, 6, 5], then remove 4 (at index 0) ...

Read More
Showing 5901–5910 of 61,299 articles
« Prev 1 589 590 591 592 593 6130 Next »
Advertisements