Programming Articles

Page 294 of 2547

Program to find out the sum of evenly spaced elements in an array in Python

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

Suppose, there is an array nums of size n containing positive integers. We have another array queries that contain integer pairs (pi, qi). For every query in the array queries, the answer will be the sum of numbers in the array nums[j] where pi ≤ j < n and (j - pi) is divisible by qi. We have to return the answer of all such queries, and if it is a large value, we return the answer modulo 10^9 + 7. So, if the input is like nums = [2, 3, 4, 5, 6, 7, 8, 9, 10], queries ...

Read More

Program to count the number of ways to distribute n number of candies in k number of bags in Python

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

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 More

Program to find maximum subarray min-product in Python

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

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 More

Program to find maximum distance between a pair of values in Python

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

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 More

Program to find maximum element after decreasing and rearranging in Python

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

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 More

Program to implement seat reservation manager in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

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 More

Program to find smallest value of K for K-Similar Strings in Python

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

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 More

Program to find closest subsequence sum in Python

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

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 More

Program to find longest substring of all vowels in order in Python

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

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 More

Program to find maximum ice cream bars in Python

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

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 More
Showing 2931–2940 of 25,466 articles
« Prev 1 292 293 294 295 296 2547 Next »
Advertisements