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
Python Articles
Page 340 of 855
Program 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 MoreProgram to find maximum score from removing stones in Python
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 MoreProgram to find minimum length of string after deleting similar ends in Python
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 MoreProgram to count minimum semesters to cover all different courses in Python
Suppose we have a number n, indicating that there are n different courses labeled from 1 to n. We also have an array called relations where relations[i] contains a pair (prevCourse_i, nextCourse_i), representing a prerequisite relationship between the course prevCourse_i and the course nextCourse_i: so course prevCourse_i has to be taken before the course nextCourse_i. The last parameter we have is k. In one semester, we can take at most k number of courses as long as we have taken all the prerequisites in the previous semester for the courses we are taking. We have to find the minimum number ...
Read MoreProgram to find maximum absolute sum of any subarray in Python
Sometimes we need to find the maximum absolute sum of any subarray within an array. This problem extends Kadane's algorithm by considering both maximum and minimum subarray sums, as the absolute value of a negative sum might be larger than the positive maximum. Problem Understanding Given an array nums, we need to find the maximum absolute sum of any contiguous subarray. The absolute sum of a subarray [nums_l, nums_l+1, ..., nums_r] is |nums_l + nums_l+1 + ... + nums_r|. For example, if nums = [2, -4, -3, 2, -6], the subarray [2, -4, -3, 2] has sum ...
Read More