Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 108 of 377

Program to fill with color using floodfill operation in Python

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

Suppose we have a 2D grid containing colors as strings "r", "g", and "b". We need to perform a floodfill operation at row r, column c with the target color. The floodfill operation replaces all elements that are connected to grid[r, c] (up/right/down/left) and have the same color as grid[r, c] with the target color. Example Input and Output If the input grid is: RRR RGB GBB Starting floodfill at position (0, 0) with target color "G", the output will be: GGG GGB GBB The red cells connected to grid[0, ...

Read More

Program to pack same consecutive elements into sublist in Python

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

Sometimes we need to group consecutive elements of the same value into sublists. This is useful for data analysis, compression, or pattern recognition tasks. Given a list like [5, 5, 2, 7, 7, 7, 2, 2, 2, 2], we want to create sublists for each group of consecutive identical elements: [[5, 5], [2], [7, 7, 7], [2, 2, 2, 2]]. Algorithm Steps The approach follows these steps ? If the input list is empty, return an empty list Initialize result with first element in its own sublist ...

Read More

Program to find one minimum possible interval to insert into an interval list in Python

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

Suppose we have a 2D list of numbers called intervals where each row represents [start, end] (inclusive) interval. For an interval [a, b] (a < b), its size is (b - a). We must add one interval to the given list such that, after merging all the intervals, we get exactly one range left. We have to find the minimum possible size of the added interval. So, if the input is like intervals = [[15, 20], [30, 50]], then the output will be 10, as we can add the interval [20, 30] which is the smallest possible interval. ...

Read More

Program to find minimum cost to reduce a list into one integer in Python

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

Suppose we have a list of numbers called nums. We can reduce the length of nums by taking any two numbers, removing them, and appending their sum at the end. The cost of doing this operation is the sum of the two integers we removed. We have to find the minimum total cost of reducing nums to one integer. So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 45. We take 2 and 3 then remove to get [4, 5, 6, 5], then we take 4 and 5 then remove ...

Read More

Program to find a list of product of all elements except the current index in Python

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

Given a list of numbers, we need to find a new list where each element at index i is the product of all numbers in the original list except the one at index i. This problem must be solved without using division. So, if the input is nums = [2, 3, 4, 5, 6], then the output will be [360, 240, 180, 144, 120]. Approach We use two auxiliary arrays to store left and right products: Left array: Contains products of all elements to the left of each index ...

Read More

Program to check whether all palindromic substrings are of odd length or not in Python

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

Sometimes we need to check if all palindromic substrings in a string have odd lengths. A key insight is that any even-length palindrome must contain adjacent identical characters. Therefore, we can solve this by simply checking for consecutive duplicate characters. Algorithm The approach is straightforward ? Iterate through the string from index 1 to the end If any character equals the previous character, return False If no adjacent duplicates are found, return True Why This Works Any even-length palindrome like "aa", "abba", or "deed" must have at least one pair of adjacent identical ...

Read More

Program to find minimum number of hops required to reach end position in Python

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

Suppose we have one array nums, where all elements are positive. We are at index 0. Here, each element in the array represents our maximum jump length at that position. Our goal is to reach to the final index (n-1, where n is size of nums) with minimum number of jumps. So if the array is like [2, 3, 1, 1, 4], then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index. Approach We use a greedy approach to solve this problem efficiently ...

Read More

Program to check whether every one has at least a friend or not in Python

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

Suppose we have n people represented as numbers from 0 to n-1, and a list of friendship pairs, where friends[i][0] and friends[i][1] are friends. We need to check whether everyone has at least one friend or not. So, if the input is like n = 3 and friends = [[0, 1], [1, 2]], then the output will be True. Person 0 is friends with Person 1, Person 1 is friends with Person 0 and 2, and Person 2 is friends with Person 1. Algorithm To solve this problem, we follow these steps − ...

Read More

Program to find most frequent subtree sum of a binary tree in Python

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

Suppose we have a binary tree, we have to find the most frequent subtree sum. The subtree sum of a node is the sum of all values under a node, including the node itself. So, if the input is like -6 3 6 Subtree sums: 3, 6, 3 (-6+3+6) then the output will be 3 as it occurs twice — ...

Read More

Program to count non-empty subsets where sum of min and max element of set is less than k in Python

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

Given a list of numbers and a value k, we need to find the number of non-empty subsets where the sum of minimum and maximum elements is less than or equal to k. Note that subsets are treated as multisets, meaning duplicate values can appear since they refer to specific positions in the list. For example, if nums = [2, 2, 5, 6] and k = 7, the valid subsets are: [2], [2], [2, 2], [2, 5], [2, 5], [2, 2, 5], giving us 6 subsets total. Algorithm The approach uses a two-pointer technique after sorting the ...

Read More
Showing 1071–1080 of 3,768 articles
« Prev 1 106 107 108 109 110 377 Next »
Advertisements