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

Arnab Chakraborty
Updated on 26-Mar-2026 14:07:27

846 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
Updated on 26-Mar-2026 14:07:06

727 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
Updated on 26-Mar-2026 14:06:46

232 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
Updated on 26-Mar-2026 14:06:20

321 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
Updated on 26-Mar-2026 14:06:01

298 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
Updated on 26-Mar-2026 14:05:38

564 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
Updated on 26-Mar-2026 14:05:19

503 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
Updated on 26-Mar-2026 14:04:56

644 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

Program to find maximum score from removing stones in Python

Arnab Chakraborty
Updated on 26-Mar-2026 14:04:28

600 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

Program to find minimum length of string after deleting similar ends in Python

Arnab Chakraborty
Updated on 26-Mar-2026 14:04:07

440 Views

Suppose we have a string s with only three characters 'a', 'b', and 'c'. We can apply the following algorithm on the string any number of times to minimize its length: Select a non-empty prefix from s where all characters in the prefix are the same. Select a non-empty suffix from s where all characters in the suffix are the same. The prefix and suffix must be disjoint (non-overlapping). The characters from the prefix and suffix must be the same. Remove both the prefix and the suffix from s. Our goal is to find the minimum ... Read More

Advertisements