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 141 of 855
Program to find maximum sum obtained of any permutation in Python
Given an array nums and an array requests where requests[i] = [start_i, end_i], each request asks for the sum of elements from nums[start_i] to nums[end_i] inclusive. We need to find the maximum total sum of all requests among all permutations of nums. The answer should be returned modulo 10^9+7. Problem Understanding For example, if nums = [10, 20, 30, 40, 50] and requests = [[1, 3], [0, 1]], we want to arrange the array to maximize the sum. The optimal arrangement [30, 50, 40, 20, 10] gives ? Request [1, 3]: nums[1] + nums[2] + nums[3] ...
Read MoreProgram to find minimum cost to connect all points in Python
Suppose we have an array called points with some coordinates in the form (x, y). The cost of connecting two points (xi, yi) and (xj, yj) is the Manhattan distance between them, calculated as |xi - xj| + |yi - yj|. We need to find the minimum cost to connect all points using a Minimum Spanning Tree (MST) approach. So, if the input is like points = [(0, 0), (3, 3), (2, 10), (6, 3), (8, 0)], then the output will be 22. (0, 0) ...
Read MoreProgram to count number of unhappy friends in Python
In this problem, we need to count unhappy friends based on their preferences and pairings. A friend is unhappy if they are paired with someone but prefer another person who also prefers them back over their current partners. Problem Definition Given preferences and pairs, a friend x is unhappy if: x is paired with y There exists a friend u (paired with v) such that: x prefers u over y, and u prefers x over v Algorithm Steps We solve this by building a preference graph: Create a graph ...
Read MoreProgram to find minimum deletion cost to avoid repeating letters in Python
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 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 ...
Read MoreProgram to find number of ways where square of number is equal to product of two numbers in Python
Suppose we have two arrays nums1 and nums2, we have to find the number 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
Read MoreProgram to find shortest subarray to be removed to make array sorted in Python
Given an array, we need to find the shortest subarray to remove so that the remaining elements are in non-decreasing (sorted) order. This problem involves finding the longest possible sorted subsequence that can be formed by keeping elements from both ends of the array. 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 the smallest subarray of length 3, and by removing these all remaining elements are in non-decreasing order [10, 20, 30, 30, 50]. Algorithm ...
Read MorePython - Density Plots with Pandas for a specific attribute
A density plot shows the probability density function of a continuous variable. In Pandas, you can create density plots using the plot.density() method to visualize the distribution of numerical data. What is a Density Plot? A density plot is a smoothed version of a histogram that shows the distribution of values in a dataset. It's useful for understanding the shape, central tendency, and spread of your data. Creating Sample Data Let's create a sample dataset with age information to demonstrate density plotting ? import pandas as pd import matplotlib.pyplot as plt import numpy as ...
Read MorePython Pandas - Draw a vertical violinplot grouped by a categorical variable with Seaborn
A violin plot combines a box plot with a kernel density estimate to show the distribution of data. Seaborn's violinplot() function creates violin plots grouped by categorical variables, making it perfect for comparing distributions across different categories. Understanding Violin Plots Violin plots display: Distribution shape − The width shows density at different values Quartiles − Like a box plot, showing median and quartiles Data range − The full extent of the data Creating Sample Data Let's create a dataset similar to cricket player data to demonstrate violin plots − import seaborn as ...
Read MorePython Pandas- Create multiple CSV files from existing CSV file
Pandas provides powerful functionality to split CSV files into multiple files based on specific columns. This is useful when you need to segregate data by categories, such as creating separate files for different car brands, departments, or regions. SalesRecords.csv Car | Date_of_Purchase BMW | 10/10/2020 Lexus | 10/12/2020 BMW | 10/17/2020 Jaguar | 10/16/2020 ...
Read MoreProgram to find maximum length of subarray with positive product in Python
When working with arrays containing positive and negative numbers, we often need to find the maximum length of a subarray where the product of all elements is positive. A product is positive when there's an even number of negative values in the subarray. So, if the input is like nums = [2, -2, -4, 5, -3], then the output will be 4 because the first four elements [2, -2, -4, 5] form a subarray with product 2×(-2)×(-4)×5 = 80, which is positive. Algorithm Approach To solve this problem, we need to ? Split the array ...
Read More