Programming Articles

Page 481 of 2547

Program to find list of elements which are less than limit and XOR is maximum in Python

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

Suppose we have a list of numbers nums and a list of queries where each query contains [x, limit]. We have to find a list such that for each query [x, limit], we find an element e in nums such that e ≤ limit and e XOR x is maximized. If there is no such element, return -1. Problem Understanding For example, if nums = [3, 5, 9] and queries = [[4, 6], [2, 0]], then the output will be [3, -1]. For the first query, we can use 3 or 5 from nums (both ≤ 6). 3 ...

Read More

Program to find length of subsequence that can be removed still t is subsequence of s in Python

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

Suppose we have a string s and another string t, where t is a subsequence of s. We need to find the maximum length of a substring that can be removed from s such that t remains a subsequence of the remaining string. So, if the input is like s = "xyzxyxz" and t = "yz", then the output will be 4, as we can remove a substring of length 4 while keeping "yz" as a subsequence. Approach The algorithm works by considering three scenarios ? Remove a suffix after matching all characters of t ...

Read More

Program to find maximum possible value of smallest group in Python

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

We need to split a list of numbers into k contiguous groups and find the maximum possible value of the smallest group's sum. This is a classic binary search optimization problem. For example, given nums = [2, 6, 4, 5, 8] and k = 3, we can split into groups [2, 6], [4, 5], [8] with sums 8, 9, 8. The smallest sum is 8, which is our answer. Algorithm Approach We use binary search on the answer space. For each potential minimum sum, we check if it's possible to split the array into exactly k groups ...

Read More

Program to find how max score we can get by removing 10 or 01 from binary string in Python

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

Suppose we have a binary string s and two values zero_one and one_zero. We can delete any substring "01" and receive zero_one points, or remove any substring "10" and receive one_zero points. We need to find the maximum number of points we can get after any number of operations. So, if the input is like s = "10100101", zero_one = 3, one_zero = 2, then the output will be 11. We can remove "01" three times to get 3×3 = 9 points. Then the remaining string is "10". By removing this we get another 2 points, so the total ...

Read More

Program to Find Out the Maximum Points From Removals in Python

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

In this problem, we need to find the maximum points from removing contiguous sublists of identical values. When we remove a sublist of length t with the same value, we get t * t points. The key insight is that we can strategically remove elements to merge identical values and maximize our score. Problem Explanation Given a list like [4, 4, 6, 4, 4], we can: First remove the single 6 → get 1 * 1 = 1 point Then remove all four 4s together → get 4 * 4 = 16 points Total: 1 + 16 ...

Read More

Program to Find Out the Largest K-Divisible Subsequence Sum in Python

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

Given a list of non-negative numbers and a positive value k, we need to find the maximum sum of a subsequence such that the sum is divisible by k. A subsequence can be formed by removing some elements from the original array while maintaining the relative order. Problem Understanding For example, if we have nums = [4, 6, 8, 2] and k = 2, the sum of all elements is 20, which is divisible by 2. So the answer is 20. Approach The algorithm works as follows: Calculate the total sum of all numbers ...

Read More

Program to Find the longest subsequence where the absolute difference between every adjacent element is at most k in Python.

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

Finding the longest subsequence where the absolute difference between every adjacent element is at most k is a dynamic programming problem that can be efficiently solved using a segment tree for range maximum queries. So, if the input is like nums = [5, 6, 2, 1, −6, 0, −1] and k = 4, then the output will be 6. Algorithm Overview The approach uses coordinate compression and a segment tree to efficiently track the maximum subsequence length ending at each position − Sort the array to create coordinate mapping For each element, find the range ...

Read More

Program to Find K-Largest Sum Pairs in Python

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

When working with two lists of numbers, we often need to find the k largest sum pairs where each pair contains one element from each list. This problem can be efficiently solved using a max heap to prioritize the largest sums. For example, if we have nums1 = [8, 6, 12], nums2 = [4, 6, 8], and k = 2, we need to find the 2 largest sum pairs. The largest pairs would be [12, 8] = 20 and [12, 6] = 18, giving us a total sum of 38. Algorithm Approach We'll use a max heap ...

Read More

Program to Find Out the Edges that Disconnect the Graph in Python

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

An edge in a graph is called a bridge (or cut-edge) if removing it increases the number of connected components. In other words, removing a bridge edge disconnects the graph. We can find bridge edges using Tarjan's algorithm with depth-first search. Algorithm Overview The algorithm uses DFS to assign discovery times to each node and tracks the lowest discovery time reachable from each subtree. An edge is a bridge if there's no back edge from the subtree that can reach an ancestor. Example Let's implement the bridge-finding algorithm ? class Solution: ...

Read More

Program to Find Out the Occurrence of a Digit from a Given Range in Python

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

When working with digit counting problems, we need to find how many times a specific digit appears in a range of numbers. This problem asks us to count occurrences of digit d in all numbers from 1 to n. For example, if n = 45 and d = 5, we need to count digit 5 in numbers: [5, 15, 25, 35, 45], giving us a total count of 5. Using Recursive Approach The recursive solution breaks down the problem by processing digits systematically ? class Solution: def solve(self, n, d): if n < 0: return 0 k = n // 10 - 1 ans = self.solve(k, d) * 10 + k + 1 if d == 0: ans -= 1 m = n // 10 * 10 while m

Read More
Showing 4801–4810 of 25,466 articles
« Prev 1 479 480 481 482 483 2547 Next »
Advertisements