Found 26504 Articles for Server Side Programming

Program to make sum divisible by P in Python

Arnab Chakraborty
Updated on 04-Oct-2021 10:00:07

401 Views

Suppose we have an array nums and another value p, we remove the smallest subarray (not the whole array) such that the sum of the remaining values is divisible by p. We have to find the length of the smallest subarray that we need to remove, if there is no such subarray then return -1.So, if the input is like nums = [8, 2, 6, 5, 3] p = 7, then the output will be 1 because if we remove 3, then total sum will be 21 and that is divisible by 7.To solve this, we will follow these steps ... Read More

Program to find maximum sum obtained of any permutation in Python

Arnab Chakraborty
Updated on 04-Oct-2021 09:44:48

475 Views

Suppose we have an array nums, and another array called requests where requests[i] = [start_i, end_i], this represents the ith request asks for the sum of nums[start_i] + nums[start_i+1] + ... + nums[end_i-1] + nums[end_i]. We have to find the maximum total sum of all requests among all permutations of nums. The answer may be very large, so return it modulo 10^9+7.So, if the input is like nums = [10, 20, 30, 40, 50] requests = [[1, 3], [0, 1]], then the output will be 190, because if we arrange like [30, 50, 40, 20, 10] we get: from requests[0] ... Read More

Program to find minimum cost to connect all points in Python

Arnab Chakraborty
Updated on 04-Oct-2021 09:32:53

477 Views

Suppose we have an array called points with some points in the form (x, y). Now the cost of connecting two points (xi, yi) and (xj, yj) is the Manhattan distance between them, the formula is |xi - xj| + |yi - yj|. We have to find the minimum cost to make all points connected.So, if the input is like points = [(0, 0), (3, 3), (2, 10), (6, 3), (8, 0)], then the output will be 22 becauseso here total distance is (6+5+3+8) = 22.To solve this, we will follow these steps −points_set := a new set holding numbers ... Read More

Program to count number of unhappy friends in Python

Arnab Chakraborty
Updated on 04-Oct-2021 09:18:51

259 Views

Suppose we have a list of preferences for n(even) different friends. For each person i, the preferences[i] holds a list of friends sorted in the order of preference. So, a friend earlier in the list is more preferred than a friend later in the list. The friends in each list are numbered by integers from 0 to n-1. All of the friends are divided into different pairs. where pairs[i] = [xi, yi] represents xi is paired with yi and/or yi is paired with xi. But a friend x is unhappy if x is paired with y and there exists a ... Read More

Program to find minimum deletion cost to avoid repeating letters in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:50:25

685 Views

Suppose we have a string s and another array of integers called cost where cost[i] represents the cost of deleting the ith character in s. We have to find the minimum cost of deletions such that there are no two same letters next to each other. We have to keep in mind that that we will delete the chosen characters at the same time. So after deleting a character, the costs of deleting other characters will not change.So, if the input is like s = "pptpp", cost = [2, 3, 4, 5, 2], then the output will be 4 because ... Read More

Program to find number of ways where square of number is equal to product of two numbers in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:39:40

204 Views

Suppose we have two arrays nums1 and nums2, we have to find of triplets formed (type 1 and type 2) following these two rules −Triplet (i, j, k) if nums1[i]^2 = nums2[j] * nums2[k] where [0

Program to find shortest subarray to be removed to make array sorted in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:12:57

270 Views

Suppose we have an array called arr, we have to remove a subarray of arr such that the remaining elements in arr are in non-decreasing order. We have to find the length of the shortest subarray to remove.So, if the input is like arr = [10, 20, 30, 100, 40, 20, 30, 50], then the output will be 3 because we can remove [100, 40, 20] which is smallest subarray of length 3, and by removing these all are in non-decreasing order [10, 20, 30, 30, 50].To solve this, we will follow these steps:n := size of arrarr := insert ... Read More

Program to find number of ways to split a string in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:02:11

405 Views

Suppose we have a binary string s we can split s into 3 non-empty strings s1, s2, s3 such that (s1 concatenate s2 concatenate s3 = s). We have to find the number of ways s can be split such that the number of characters '1' is the same in s1, s2, and s3. The answer may be very large so return answer mod 10^9+7.So, if the input is like s = "11101011", then the output will be 2 because we can split them like "11 | 1010 | 11" and "11 | 101 | 011".To solve this, we will ... Read More

Python - Density Plots with Pandas for a specific attribute

AmitDiwan
Updated on 04-Oct-2021 07:42:33

608 Views

We will use plot.density() to density plot on a Dataset in the form of a csv file. Let’s say the following is our dataset − Cricketers2.csvAt first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Plotting the density plot. Attribute considered is "Age" −dataFrame.Age.plot.density(color='green')ExampleFollowing is the complete code −import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") # plotting the density plot # attribute considered is "Age" dataFrame.Age.plot.density(color='green') plt.title('Density ... Read More

Python Pandas - Draw a vertical violinplot grouped by a categorical variable with Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:39:55

561 Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used for this. We will plotti violin plot with the columns grouped by a categorical variable.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting violin plot with Role and Age grouped by a categorical variable −sb.violinplot(x = 'Role', y = "Age", data = dataFrame)ExampleFollowing ... Read More

Advertisements