Programming Articles

Page 515 of 2547

Program to find leaf and non-leaf nodes of a binary tree in Python

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

In a binary tree, a leaf node has no children, while a non-leaf node has at least one child. This program counts both types of nodes using a recursive approach that traverses the entire tree. So, if the input is like: 6 2 ...

Read More

Program to check whether inorder sequence of a tree is palindrome or not in Python

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

Suppose we have a binary tree where each node contains a digit from 0-9, we have to check whether its in-order traversal is palindrome or not. So, if the input is like 6 2 6 10 2 then the output will be True, ...

Read More

Program to count maximum number of distinct pairs whose differences are larger than target in Python

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

Suppose we have a list of numbers called nums and another value target. We have to find the maximum number of pairs where for each pair i < j, i and j are not in any other pair, and |nums[i] - nums[j]| >= target. So, if the input is like nums = [2, 4, 6, 10, 11], target = 5, then the output will be 2, as we can get pairs: (2, 10) and (4, 11). Algorithm To solve this problem, we will follow these steps ? Sort the array to enable efficient pairing Use ...

Read More

Program to fill with color using floodfill operation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 340 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 609 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 218 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 882 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 771 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 371 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 741 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
Showing 5141–5150 of 25,466 articles
« Prev 1 513 514 515 516 517 2547 Next »
Advertisements