Programming Articles

Page 516 of 2547

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 413 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 267 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 311 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

Program to find maximum time to finish K tasks in Python

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

Suppose we have a matrix of tasks where each row has 3 values representing costs for different resources. We also have another value k. We need to select k rows from tasks to minimize the total cost, which is calculated as the sum of maximum values from each of the 3 columns in the selected rows. The cost formula is: max(column_0) + max(column_1) + max(column_2) from the k selected rows. Problem Example If the input is like tasks = [[2, 3, 3], [4, 5, 2], [4, 2, 3]] and k = 2, then the output will be ...

Read More

Program to find minimum difference of max and mins after updating elements at most three times in Python

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

Suppose we have a list of numbers called nums, and we can perform an operation to update any element to any value. We can perform at most 3 such operations, and we need to find the resulting minimum difference between the max and min values in nums. So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 2. We can change the list to [4, 3, 4, 5, 4, 4] and then 5 - 3 = 2. Algorithm To solve this problem, we follow these steps − ...

Read More

Program to find minimum difference between two elements from two lists in Python

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

Sometimes we need to find the smallest possible difference between any element from one list and any element from another list. Python provides an efficient solution using the two-pointer technique after sorting both lists. So, if the input is like L1 = [2, 7, 4], L2 = [16, 10, 11], then the output will be 3, as the smallest difference is 10 - 7 = 3. Algorithm Steps To solve this efficiently, we will follow these steps ? Sort both lists L1 and L2 Initialize minimum difference as infinity ...

Read More

Program to find minimum number of deletions required from two ends to make list balanced in Python

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

Suppose we have a list containing 0s and 1s, we have to remove values from the front or from the back of the list. Finally, we have to find the minimum number of deletions required such that the remaining list has an equal number of 0s and 1s. So, if the input is like nums = [1, 1, 1, 0, 0, 1], then the output will be 2, as we can delete the first one 1 and last one 1 so that there's two 1s and two 0s. Algorithm To solve this, we will follow these steps ...

Read More

Program to find maximum length of non-sharing words in Python

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

Given a list of lowercase alphabetical strings, we need to find the maximum sum of lengths of two distinct words that do not share any common letters. This problem can be efficiently solved using bit manipulation to represent character sets. Problem Understanding For the input words = ["abcd", "mno", "abdcmno", "amno"], we need to find two words with no common letters. The words "abcd" and "mno" share no common letters, giving us a total length of 4 + 3 = 7. Algorithm Approach We use bit manipulation where each bit position represents a letter (a=0, b=1, ...

Read More

Program to find maximum sum of non-adjacent nodes of a tree in Python

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

Finding the maximum sum of non-adjacent nodes in a binary tree is a classic dynamic programming problem. We need to select nodes such that no parent-child pairs are included, maximizing the total sum. Problem Understanding Given a binary tree, we want to find the maximum sum where no two selected nodes are adjacent (parent-child relationship). For each node, we have two choices: include it or exclude it. 1 2 10 ...

Read More

Program to maximize the number of equivalent pairs after swapping in Python

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

Suppose we have two lists A and B of the same length, and a 2D list C containing pairs [i, j] that allow us to swap elements A[i] and A[j] as many times as needed. We need to find the maximum number of equivalent pairs where A[i] = B[i] after optimal swapping. The key insight is that elements connected through swap operations form groups, and within each group, we can arrange elements in any order to maximize matches. Example If the input is A = [5, 6, 7, 8], B = [6, 5, 8, 7], and C ...

Read More
Showing 5151–5160 of 25,466 articles
« Prev 1 514 515 516 517 518 2547 Next »
Advertisements