Server Side Programming Articles

Page 305 of 2109

Program to make sum divisible by P in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 477 Views

Given an array nums and a value p, we need to remove the smallest subarray such that the sum of remaining elements is divisible by p. We return the length of the smallest subarray to remove, or -1 if no such subarray exists. For example, with nums = [8, 2, 6, 5, 3] and p = 7, removing element 3 gives us a remaining sum of 21, which is divisible by 7. Algorithm The approach uses prefix sums and modular arithmetic ? s := remainder when total sum is divided by p Use a dictionary ...

Read More

Program to find maximum sum obtained of any permutation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 578 Views

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 More

Program to find minimum cost to connect all points in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 557 Views

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 More

Program to count number of unhappy friends in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 343 Views

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 772 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 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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 273 Views

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 369 Views

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 More

Python - Density Plots with Pandas for a specific attribute

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 670 Views

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 More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 632 Views

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 More

Python Pandas- Create multiple CSV files from existing CSV file

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

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 More
Showing 3041–3050 of 21,090 articles
« Prev 1 303 304 305 306 307 2109 Next »
Advertisements