Python Articles

Page 626 of 855

Program to find the largest sum of the path between two nodes in a binary tree in Python

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

In a binary tree, we often need to find the maximum sum path between any two nodes. This path doesn't have to go through the root and can be between any two nodes in the tree. Given a binary tree like this: .node-circle { fill: #e6f3ff; stroke: #0066cc; stroke-width: 2; } .node-text { font-family: Arial, sans-serif; font-size: 12px; text-anchor: middle; dominant-baseline: central; } .edge-line { stroke: #666; stroke-width: 2; } ...

Read More

Program to find largest sum of non-adjacent elements of a list in Python

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

The problem of finding the largest sum of non-adjacent elements in a list is a classic dynamic programming problem. Given a list of numbers, we need to select elements such that no two selected elements are adjacent, and their sum is maximized. So, if the input is like [3, 5, 7, 3, 6], then the output will be 16, as we can take 3, 7, and 6 to get the maximum sum without selecting adjacent elements. Algorithm Approach We use dynamic programming with two variables to track the maximum sum: take: Maximum sum including the ...

Read More

Program to find sum of contiguous sublist with maximum sum in Python

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

The Maximum Subarray Problem asks us to find the contiguous sublist within an array that has the largest sum. This is a classic problem that can be efficiently solved using Kadane's Algorithm with dynamic programming. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the maximum sum subarray is [4, -1, 2, 1] with sum 6. Algorithm Approach We use dynamic programming to solve this problem efficiently ? Create an array dp of the same size as input array Set dp[0] = ...

Read More

Program to find k-sized list where difference between largest and smallest item is minimum in Python

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

Given a list of numbers and an integer k, we need to select k elements to create a sublist where the difference between the largest and smallest elements is minimized. This problem requires finding the optimal contiguous subsequence after sorting. For example, if nums = [3, 11, 6, 2, 9] and k = 3, the output will be 4 because the best sublist we can make is [2, 3, 6] with difference 6 - 2 = 4. Algorithm To solve this problem, we follow these steps: Sort the input list to group similar numbers together ...

Read More

Program to find the largest grouping of anagrams from a word list in Python

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

Suppose we have a list of strings words, we have to group all anagrams together and return the size of the largest grouping. Anagrams are words formed by rearranging the letters of another word, like "xyz" and "zyx". So, if the input is like words = ["xy", "yx", "xyz", "zyx", "yzx", "wwwww"], then the output will be 3, as ["xyz", "zyx", "yzx"] is the largest grouping. Algorithm To solve this, we will follow these steps − Create a dictionary to store anagram groups Initialize result as 0 For each ...

Read More

Program to find minimum required chances to form a string with K unique characters in Python

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

Suppose we have a string s of lowercase alphabet characters, and another number k. We need to find the minimum number of required changes in the string so that the resulting string has at most k distinct characters. A change means modifying a single character to any other character. So, if the input is like s = "wxxyyzzxx", k = 3, then the output will be 1, as we can change the letter "w" to get 3 distinct characters (x, y, and z). Approach To solve this problem, we will follow these steps − count ...

Read More

Program to find the K-th last node of a linked list in Python

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

Suppose we have a singly linked list, we have to find the value of the k-th last node (0-indexed) in a single pass. This is a classic two-pointer technique problem. So, if the input is like node = [5, 4, 6, 3, 4, 7], k = 2, then the output will be 3, as the second last (index 3) node has the value of 3. Algorithm To solve this, we will follow these steps − Initialize two pointers: klast and last, both pointing to the head Move the last pointer k steps ahead Move both ...

Read More

Program to find maximum sum of popped k elements from a list of stacks in Python

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

This problem involves finding the maximum sum by popping exactly k elements from a list of stacks. We can only pop elements from the top of each stack, making this a dynamic programming problem where we explore different combinations of popping elements. The key insight is that for each stack, we can choose to pop 0, 1, 2, ... up to all elements from that stack, and recursively solve for the remaining stacks with the remaining number of elements to pop. Algorithm Approach We use a recursive function that takes the current stack index and the number ...

Read More

Program to find a list of numbers where each K-sized window has unique elements in Python

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

Given a list of numbers and a window size k, we need to find the count of distinct numbers in each sliding window of size k. This is a common sliding window problem that can be efficiently solved using a dictionary to track element frequencies. So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4]. Algorithm To solve this, we will follow these steps − ...

Read More

Program to find the perimeter of an island shape in Python

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

Finding the perimeter of an island shape in a binary matrix involves counting the exposed edges of all connected 1s. Each cell with value 1 contributes to the perimeter based on how many of its four sides are exposed (not adjacent to another 1). Problem Understanding Given a binary matrix where 0 represents empty cells and 1 represents land blocks, we need to calculate the total perimeter. Each land block initially has 4 sides, but we subtract 1 for each adjacent land block. For the input matrix: 00000 00111 00110 ...

Read More
Showing 6251–6260 of 8,546 articles
« Prev 1 624 625 626 627 628 855 Next »
Advertisements