Server Side Programming Articles

Page 540 of 2109

Find minimum time to finish all jobs with given constraints in Python

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

We need to find the minimum time to finish all jobs when we have k assignees and each assignee takes t time units per job unit. The key constraints are that each assignee can only work on contiguous jobs, and no job can be split between assignees. The problem can be solved using binary search on the answer. We search for the minimum possible time limit such that all jobs can be completed by k assignees within that limit. Algorithm Overview The solution uses binary search between the minimum possible time (maximum single job) and maximum possible ...

Read More

Find minimum adjustment cost of an array in Python

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

The minimum adjustment cost problem involves modifying array elements to ensure adjacent differences don't exceed a target value while minimizing the total cost of changes. We use dynamic programming to find the optimal solution. Problem Statement Given an array of positive numbers, we need to replace elements so that the difference between any two adjacent elements is at most equal to a given target. The goal is to minimize the adjustment cost, which is the sum of absolute differences between original and new values: ∑|A[i] - Anew[i]|. Example If input array is [56, 78, 53, 62, ...

Read More

Find median of BST in O(n) time and O(1) space in Python

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

Finding the median of a Binary Search Tree (BST) efficiently requires understanding that an inorder traversal of a BST gives nodes in sorted order. The median is the middle element for odd number of nodes, or the average of two middle elements for even number of nodes. For a BST with n nodes: Odd nodes: median = (n+1)/2th node Even nodes: median = average of (n/2)th and (n/2+1)th nodes We'll use Morris Traversal to achieve O(n) time complexity with O(1) space complexity by avoiding recursion stack. Example Tree ...

Read More

Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python

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

Given an array of positive numbers, we need to find the maximum sum of a triplet (a[i] + a[j] + a[k]) such that 0 ≤ i < j < k < n and a[i] < a[j] < a[k]. This means we're looking for three increasing elements at increasing indices. For example, if the input is A = [3, 6, 4, 2, 5, 10], the valid triplets are (3, 4, 5): sum = 12, (3, 6, 10): sum = 19, (3, 4, 10): sum = 17, (4, 5, 10): sum = 19, and (2, 5, 10): sum = 17. The ...

Read More

Find maximum points which can be obtained by deleting elements from array in Python

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

When we delete an element from an array, we might need to remove related elements as well. This problem asks us to find the maximum points we can obtain by strategically deleting elements, where deleting an element ax gives us ax points but also forces us to remove elements in the range [ax-L, ax+R]. So, if the input is like A = [2, 4, 3, 10, 5], l = 1, r = 2, then the output will be 18. Algorithm Steps To solve this, we will follow these steps − Find the ...

Read More

Find maximum operations to reduce N to 1 in Python

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

We have two numbers P and Q that form N = P!/Q!. Our goal is to reduce N to 1 by performing the maximum number of operations possible. In each operation, we can replace N with N/X when N is divisible by X. We need to find the maximum number of operations possible. The key insight is that the maximum number of operations equals the total count of prime factors in N. Since N = P!/Q!, we need to count prime factors in factorials efficiently. Algorithm Approach We use a sieve-like approach to precompute the count of ...

Read More

Find maximum N such that the sum of square of first N natural numbers is not more than X in Python

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

Suppose we have a given integer X, we have to find the maximum value N so that the sum of squares of first N natural numbers should not exceed the value X. The sum of squares of first N natural numbers follows the formula: 1² + 2² + 3² + ... + N² = N × (N + 1) × (2N + 1) / 6 So, if the input is like X = 7, then the output will be 2 as 2 is the maximum possible value of N. For N = 3, the sum would be 1² ...

Read More

Find maximum length Snake sequence in Python

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

A snake sequence is a path in a grid where adjacent numbers differ by exactly 1. You can move right or down in the grid, and each step must be to a number that is either +1 or -1 from the current value. Problem Understanding Given a grid of numbers, we need to find the longest snake sequence. For example, in this grid ? 10 7 6 3 9 8 7 6 8 4 2 7 2 2 2 8 The longest snake sequence has length ...

Read More

Find maximum distance between any city and station in Python

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

Sometimes we need to find the maximum distance between any city and its nearest station. Given N cities numbered from 0 to N-1 and a list of cities with stations, we need to calculate the furthest any city is from a station. So, if the input is like N = 6 and stations = [2, 4], then the output will be 2, because city 0 is distance 2 from the nearest station at city 2. Algorithm Overview To solve this problem, we will follow these steps − Create a boolean array to mark which cities ...

Read More

Find maximum difference between nearest left and right smaller elements in Python

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

Given an array of integers, we need to find the maximum absolute difference between the nearest left and right smaller elements for each position. If no smaller element exists on either side, we consider it as 0. For example, with array [3, 5, 9, 8, 8, 10, 4], the left smaller elements are [0, 3, 5, 5, 5, 8, 3] and right smaller elements are [0, 4, 8, 4, 4, 4, 0]. The maximum absolute difference is |8 - 4| = 4. Algorithm Steps We use a stack-based approach to find nearest smaller elements efficiently ? ...

Read More
Showing 5391–5400 of 21,090 articles
« Prev 1 538 539 540 541 542 2109 Next »
Advertisements