Server Side Programming Articles

Page 290 of 2109

Program to find maximize palindrome length from subsequences in Python

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

Given two strings s and t, we need to find the maximum length palindrome that can be formed by concatenating a subsequence from s with a subsequence from t. A subsequence preserves the relative order of characters but doesn't need to be contiguous. Problem Approach The key insight is to use dynamic programming to find the longest palindromic subsequence in the combined string, then check all possible ways to form palindromes using characters from both strings ? Concatenate both strings to form a combined string Use DP to find longest palindromic subsequence for any substring Check ...

Read More

Program to check whether one point can be converted to another or not in Python

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

Suppose we have a starting point (sx, sy) and a target point (tx, ty). We need to check whether a sequence of moves exists to transform the start point to the end point. Each move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). This problem can be solved using a recursive approach by working backwards from the target point to check if we can reach the starting point. Algorithm Steps To solve this problem, we follow these steps ? If sx > tx or sy > ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 417 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 625 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 752 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 277 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 302 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 551 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
Showing 2891–2900 of 21,090 articles
« Prev 1 288 289 290 291 292 2109 Next »
Advertisements