Server Side Programming Articles

Page 300 of 2109

Program to find minimum number of increments on subarrays to form a target array in Python

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

Suppose we have an array called target with positive values. Now consider an array initial of same size with all zeros. We have to find the minimum number of operations required to generate a target array from the initial if we do this operation: Select any subarray from initial and increment each value by one. So, if the input is like target = [2, 3, 4, 3, 2], then the output will be 4. How It Works The key insight is that we only need to increment when moving to a higher value. When the array decreases, ...

Read More

Program to find minimum number of operations to move all balls to each box in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 828 Views

Suppose we have a binary string called boxes, where boxes[i] is '0' indicates the ith box is empty, and '1' indicates it contains one ball. In one operation, we can move one ball from a box to an adjacent box. We need to find an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box. Problem Understanding If the input is boxes = "1101", then the output will be [4, 3, 4, 5] ? To put all balls in first box: take from ...

Read More

Program to find maximum number of non-overlapping substrings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 712 Views

Given a string with lowercase letters, we need to find the maximum number of non-overlapping substrings where each substring contains all occurrences of every character it includes. Problem Understanding The algorithm must satisfy two conditions ? Substrings are non-overlapping A substring containing character 'ch' must include all occurrences of 'ch' in the string For example, if we have "pqstpqqprrr", the valid substrings are ["pqstpqqprrr", "pqstpqqp", "st", "s", "t", "rrr"]. We want the maximum count, so we choose ["s", "t", "rrr"]. Algorithm Approach The solution uses a greedy approach ? Find ...

Read More

Program to find best position for a service center in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 221 Views

Suppose we have a list of positions containing coordinate points where houses are located. We want to find the optimal location (xc, yc) for a service center such that the sum of Euclidean distances from all houses to the service center is minimized. This is known as the geometric median problem. So, if the input is like positions = [(10, 11), (11, 10), (11, 12), (12, 11)], then the output will be 4.0 ...

Read More

Program to generate array by concatenating subarrays of another array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 312 Views

This problem involves checking if we can find all subarrays from a groups list within a nums array in sequential order. Each subarray in groups must appear as a contiguous sequence in nums, and they must appear in the same order as defined in groups. Problem Understanding Given a 2D array groups and a 1D array nums, we need to verify if we can select disjoint subarrays from nums such that: The i-th subarray equals groups[i] Each subarray appears after the previous one in nums All subarrays are found without overlapping Algorithm Steps ...

Read More

Program to find minimum possible integer after at most k adjacent swaps on digits in Python

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

Suppose we have a string called num representing a very large integer number and another value k. We can swap any two adjacent digits at most k times to find the minimum possible value. So, if the input is like num = "5432" and k = 4, then the output will be 2453. The transformation happens as: 5432 → 4532 → 4523 → 4253 → 2453. Algorithm To solve this problem, we follow these steps: min_num := sort the digits of num i := 0, to_find := 0 While num is not same as min_num ...

Read More

Program to find minimum limit of balls in a bag in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 551 Views

Suppose we have an array nums where the ith element indicates a bag containing nums[i] number of balls. We also have another value called mx. We can perform the following operation at most mx times − Select any bag of balls and divide it into two new bags with at least one ball. Here penalty is the maximum number of balls in a bag. We have to minimize the penalty after the operations. So finally, we have to find the minimum possible penalty after performing the operations. So, if ...

Read More

Program to count number of homogenous substrings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 490 Views

A homogenous substring is a substring where all characters are the same. Given a string s, we need to count the total number of homogenous substrings and return the result modulo 10^9+7. Understanding the Problem For the string "xyyzzzxx", the homogenous substrings are ? "x" appears 3 times (at positions 0, 6, 7) "xx" appears 1 time (positions 6-7) "y" appears 2 times (at positions 1, 2) "yy" appears 1 time (positions 1-2) "z" appears 3 times (at positions 3, 4, 5) "zz" appears 2 times (positions 3-4, 4-5) "zzz" appears 1 time (positions 3-5) ...

Read More

Program to find max value of an equation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 630 Views

Suppose we have an array called points containing coordinate points on a 2D plane, sorted by x-values, where points[i] = (x_i, y_i) and x_i < x_j for all 1

Read More

Program to find maximum score from removing stones in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 572 Views

Suppose we have three values a, b and c representing three piles of stones. We are playing a solitaire game where each turn the player selects two different non-empty piles, takes one stone from each, and adds 1 point to their score. The game ends when there are fewer than two non-empty piles. We need to find the maximum score possible. So, if the input is like a = 4, b = 4, c = 6, then the output will be 7 because the initial state is (4, 4, 6), then we can follow these steps − ...

Read More
Showing 2991–3000 of 21,090 articles
« Prev 1 298 299 300 301 302 2109 Next »
Advertisements