Found 10476 Articles for Python

Program to find best position for a service center in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:47:26

164 Views

Suppose we have a list of positions, which is containing a list of coordinate points where some houses are located. If you want to make a service center at (xc, yc) such that the sum of Euclidean distance from any given point to (xc, yc) is minimum. So we have to find the sum of minimum distance.So, if the input is like positions = [(10, 11), (11, 10), (11, 12), (12, 11)], then the output will be 4.0To solve this, we will follow these steps −numIter := 50Define a function total() . This will take cx, cy, positionstotal := 0.0for ... Read More

Program to check whether Amal can win stone game or not in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:42:04

193 Views

Suppose there are two players Amal and Bimal, they are playing a game, and with Amal starts first. Initially, there are n different stones in a pile. On each player's turn, he makes a move consisting of removing any square number (non-zero) of stones from the pile. Also, if one player is unable to make a move, he loses the game. So if we have n, we have to check whether Amal can win the game or not.So, if the input is like n = 21, then the output will be True because at first Amal can take 16, then ... Read More

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

Arnab Chakraborty
Updated on 06-Oct-2021 08:43:13

259 Views

Suppose we have one 2D array called groups, and another array nums. We have to check whether we can select n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray will appear before the ith subarray in nums.So, if the input is like groups = [[2, -2, -2], [4, -3, 0]] nums = [1, -1, 0, 2, -2, -2, 4, -3, 0], then the output will be true because array group[0] is present from index 3 to 5 of nums and group[1] is from index ... Read More

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

Arnab Chakraborty
Updated on 06-Oct-2021 08:39:46

199 Views

Suppose we have a string called num representing a very large integer number and also have another value k. We can swap any two adjacent digits of the values at most k times. We have to find the minimum value we can get.So, if the input is like num = "5432" k = 4, then the output will be 2453 because at first number is 5432. Then after first phase it will be 4532, then 4523, then 4253 and at final phase 2453.To solve this, we will follow these stepsmin_num := sort the digits of numi := 0, to_find := ... Read More

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

Arnab Chakraborty
Updated on 06-Oct-2021 08:37:19

485 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 the input is like nums = [4, 8, 16, 4], mx = 4, then the ... Read More

Program to count number of homogenous substrings in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:32:27

428 Views

Suppose we have a string s, we have to find the number of homogenous substrings of s. The answer may be very large, so return answer modulo 10^9+7. A string is said to be homogenous when all the characters of the string are the same.So, if the input is like s = "xyyzzzxx", then the output will be 13 because the homogenous substrings are listed like1."x" appears thrice."xx" appears once.3. "y" appears twice."yy" appears once.5. "z" appears thrice."zz" appears twice."zzz" appears once.so , (3 + 1 + 2 + 1 + 3 + 2 + 1) = 13.To solve this, ... Read More

Program to find max value of an equation in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:36:30

530 Views

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

Program to find maximum score from removing stones in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:22:38

456 Views

Suppose we have three values a, b and c. We are playing a solitaire game with three piles of stones whose sizes are a, b, and c respectively. Each turn the player selects two different non-empty piles, take one stone from each, and add 1 point to his score. The game ends when there are fewer than two non-empty piles. So we have to find the maximum score you can get.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 ... Read More

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

Arnab Chakraborty
Updated on 06-Oct-2021 08:17:25

363 Views

Suppose we have a string s with only three characters 'a', 'b', and 'c'. We shall apply the following algorithm on the string any number of times −Select a non-empty prefix from the s where all the characters in the prefix are same.Select a non-empty suffix from the s where all the characters in the suffix are same.The prefix and the suffix are disjoint.The characters from the prefix and suffix must be the same.Remove both the prefix and the suffix from s.Finally, we have to find the minimum length of s after performing the above operation any number of times ... Read More

Program to count minimum semesters to cover all different courses in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:27:17

400 Views

Suppose we have a number n, indicates 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. And the last parameter we have that 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 ... Read More

Advertisements