Server Side Programming Articles

Page 477 of 2109

Program to find minimum possible difference of indices of adjacent elements in Python

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

Given a list of numbers, two numbers nums[i] and nums[j] are considered adjacent when there is no number with a value between them in the original list. We need to find the minimum possible difference of indices |j - i| such that nums[j] and nums[i] are adjacent. For example, if the input is nums = [1, -9, 6, -6, 2], the output will be 2, as we can see that 2 and 6 are adjacent elements (no value between 2 and 6 exists in the list) and they are 2 indices apart. Algorithm Approach To solve this ...

Read More

Program to count number of words we can generate from matrix of letters in Python

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

Suppose we have a 4 x 4 board of letters and a list of words, we have to find the largest number of words that can be generated in the board by a sequence of adjacent letters, using one cell at most once per word (but we can reuse cells for other words). We can go up, down, left, right, or diagonal direction. Problem Example If the input is like ? ...

Read More

Program to find intervals by merging target interval in Python

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

Suppose we have a list of non-overlapping intervals sorted by end time. We need to merge a target interval into this list while maintaining the non-overlapping and sorted properties. For example, if we have intervals = [[1, 15], [25, 35], [75, 90]] and target = [10, 30], the output will be [[1, 35], [75, 90]] because the target interval [10, 30] overlaps with both [1, 15] and [25, 35], merging them into a single interval [1, 35]. Algorithm To solve this problem, we follow these steps: Add the target interval to the list of intervals ...

Read More

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 261 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 318 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
Showing 4761–4770 of 21,090 articles
« Prev 1 475 476 477 478 479 2109 Next »
Advertisements