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 330 of 855
Program to count the number of ways to distribute n number of candies in k number of bags in Python
Suppose we have n candies and k bags, and we need to distribute the candies such that each bag contains at least one candy. Since every candy is unique, we must count all possible ways to distribute them among the bags. This is a classic problem that can be solved using Stirling numbers of the second kind, which count the number of ways to partition n distinct objects into k non-empty subsets, multiplied by k! to account for the distinct bags. Problem Understanding For n = 3 candies and k = 2 bags, the possible distributions are ...
Read MoreProgram to find maximum subarray min-product in Python
Suppose we have an array nums, we need to find the maximum min-product of each non-empty subarray. The min-product of an array is equal to the minimum value in the array multiplied by the sum of the array. Since the answer can be large, we return it modulo 10^9+7. For example, if we have an array [4, 3, 6], the minimum value is 3 and the sum is 13, so the min-product is 3 × 13 = 39. Problem Example If the input is nums = [2, 3, 4, 3], the output will be 30. We can ...
Read MoreProgram to find maximum distance between a pair of values in Python
Given two arrays nums1 and nums2, we need to find the maximum distance between valid index pairs (i, j). A pair is valid if i
Read MoreProgram to find maximum element after decreasing and rearranging in Python
We are given an array and need to perform operations to satisfy these conditions: The first element must be 1 The absolute difference between any two adjacent elements must be at most 1 We can perform two operations any number of times: Decrease any value to a smaller positive number Rearrange elements in any order We need to find the maximum possible value after performing operations to satisfy the conditions. Example If the input is arr = [3, 3, 2, 3, 2], the output will be 3. We can decrease ...
Read MoreProgram to implement seat reservation manager in Python
A seat reservation manager system tracks the availability of n seats numbered from 1 to n. We need to implement a SeatReserveManager class that can reserve the smallest available seat and unreserve specific seats efficiently. Problem Requirements The SeatReserveManager class should have: Constructor that takes n as input and initializes n seats numbered from 1 to n. Initially all seats are available. reserve() method that finds the smallest-numbered unreserved seat, reserves it, and returns its number. unreserve(seatNumber) method that unreserves a previously reserved seat with the given seat number. Algorithm Approach We use ...
Read MoreProgram to find smallest value of K for K-Similar Strings in Python
Given two anagram strings s and t, we need to find the minimum number of swaps required to transform s into t. Two strings are K-similar if we can swap exactly K pairs of characters in s to make it equal to t. For example, if s = "abc" and t = "bac", we need only 1 swap to transform "abc" to "bac" by swapping characters at positions 1 and 2. Algorithm We use BFS (Breadth-First Search) to find the minimum number of swaps ? neighbors() function: Generates all possible strings after one valid swap ...
Read MoreProgram to find closest subsequence sum in Python
Suppose we have an array nums and another value goal. We want to select a subsequence of nums such that the sum of its elements is the nearest to goal. In other words, if the sum of the subsequence's elements is s, then we want to minimize the absolute difference |s - goal|. We have to find the minimum possible value of |s - goal|. Example If the input is nums = [8, -8, 16, -1] and goal = -3, then the output will be 2 because we select the subsequence [8, -8, -1], with a sum ...
Read MoreProgram to find longest substring of all vowels in order in Python
A beautiful substring contains all five vowels (a, e, i, o, u) in alphabetical order, with each vowel appearing at least once. We need to find the longest such substring in a given string. Problem Definition A string is beautiful if it satisfies these conditions − Each of the 5 vowels must appear at least once in it Letters must be sorted in alphabetical sequence For example, in string "aaioaaaaeiiouuooaauu", the substring "aaaaeiiouu" is beautiful and has length 10. Algorithm We use a two-pointer sliding window approach − Initialize vowels list ...
Read MoreProgram to find maximum ice cream bars in Python
Suppose we have an array costs with n elements, where costs[i] is the price of the ith ice cream bar in coins. We initially have c number coins to spend, and we want to buy as many ice cream bars as possible. We have to find the maximum number of ice cream bars we can buy with c coins. So, if the input is like costs = [3, 1, 4, 5, 2], c = 10, then the output will be 4 because we can buy ice cream bars at indices 1, 4, 0, 2 (after sorting: costs [1, 2, ...
Read MoreProgram to find maximum XOR for each query in Python
Suppose we have a presorted array called nums of size n and a value m. We want to perform the following query n times: Search for a non-negative value k < 2^m such that XOR of all elements in nums and k is maximized. So k is the answer to the ith query. Remove the last element from the current array nums. We have to find an array answer, where answer[i] is the answer to the ith query. Example Walkthrough If the input is like nums ...
Read More