Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 141 of 2547
Program to find maximum score from performing multiplication operations in Python
Suppose we have two arrays nums and multipliers of size n and m respectively (n >= m). We want to perform exactly m operations to achieve the maximum score. Our initial score is 0. In each operation, we can: Select one value x from either the start or the end of the nums array Add multipliers[i] * x to the score Remove x from the array nums We need to find the maximum score after performing m operations. Example If the input is nums = [5, 10, ...
Read MoreProgram to find minimum number of increments on subarrays to form a target array in Python
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 MoreProgram to find minimum number of operations to move all balls to each box in Python
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 MoreProgram to find maximum number of non-overlapping substrings in Python
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 MoreProgram to find best position for a service center in Python
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 MoreProgram to generate array by concatenating subarrays of another array in Python
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 MoreProgram to find minimum possible integer after at most k adjacent swaps on digits in Python
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 MoreProgram to find minimum limit of balls in a bag in Python
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 MoreProgram to count number of homogenous substrings in Python
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 MoreProgram to find max value of an equation in Python
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