Server Side Programming Articles

Page 303 of 2109

Program to find the most competitive subsequence in Python

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

A competitive subsequence is a subsequence where elements appear in their original order from the array. Given an array and a target size k, we need to find the most competitive subsequence of size k. A subsequence s1 is more competitive than s2 if at the first differing position, s1 has a smaller number than s2. For example, given nums = [4, 6, 3, 7] and k = 2, among all possible subsequences of size 2: [4, 6], [4, 3], [4, 7], [6, 3], [6, 7], [3, 7], the subsequence [3, 7] is most competitive. Algorithm We ...

Read More

Program to maximize the minimum force between balls in a bucket using Python

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

Suppose we are given several buckets and x number of balls. If the balls are put into the bucket, a special force acts within them and we have to find out a way to maximize the minimum force between two balls. The force between two balls in a bucket of position p and q is |p - q|. The input given to us is the array containing the bucket positions and the number of balls x. We have to find out the minimum force between them. So, if the input is like pos = [2, 4, 6, 8, 10, ...

Read More

Program to find array of doubled pairs using Python

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

Given an array of integers with even length, we need to check if it's possible to reorder the array such that nums[2*i + 1] = 2*nums[2*i] for every index 0 cnt[2 * x]: return False cnt[2 * x] -= cnt[x] return True # Test case 1 nums1 = [4, -2, 2, -4] print("Array:", nums1) print("Can form doubled pairs:", solve(nums1)) # Test case 2 nums2 = [2, ...

Read More

Program to merge in between linked lists in Python

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

Suppose we have two linked lists L1 and L2 of length m and n respectively, we also have two positions a and b. We have to remove nodes from L1 from a-th node to b-th node and merge L2 in between. So, if the input is like L1 = [1, 5, 6, 7, 1, 6, 3, 9, 12] L2 = [5, 7, 1, 6] a = 3 b = 6, then the output will be [1, 5, 6, 5, 7, 1, 6, 9, 12] Algorithm To solve this, we will follow these steps − ...

Read More

Program to find out the number of people who get a food packet using Python

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

Suppose in a conference, there are two types of people. The first type prefers vegetarian lunch (denoted by 0), and the other type prefers non-vegetarian lunch (denoted by 1). There are limited food packets, and if vegetarians receive a non-vegetarian packet or vice versa, they will not take that packet and wait until they get their preferred one. We are given two arrays: one containing food packets and another containing the queue of people with their preferences. If a person does not receive their preferred packet, they re-enter the queue at the end. We need to find the number ...

Read More

Program to find ways to make a fair array in Python

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

Suppose we have an array called nums. We can select exactly one index and remove the element from that index. (The index of the elements may change after the removal). We can say an array is fair when the sum of the odd-indexed values equals the sum of the even-indexed values. We have to find the number of indices that we could select such that after the removal, nums is fair. Problem Understanding Let's understand with an example. If the input is nums = [5, 3, 7, 2], let's check each removal ? ...

Read More

Program to find smallest string with a given numeric value in Python

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

Given two values n and k, we need to find the lexicographically smallest string whose length is n and numeric value equals k. The numeric value of a lowercase character is its position in the alphabet (starting from 1), so 'a' = 1, 'b' = 2, and so on. The numeric value of a string is the sum of its characters' numeric values. For example, if n = 4 and k = 16, the output will be "aaam" because the numeric value is 1+1+1+13 = 16, and this is the smallest string with length 4 and value 16. ...

Read More

Program to find minimum operations to reduce X to zero in Python

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

We have an array called nums and a value x. In one operation, we can delete either the leftmost or rightmost element from the array and subtract its value from x. We need to find the minimum number of operations to reduce x to exactly 0. The key insight is to use prefix and suffix sums. We can precompute all possible left prefix sums, then iterate through right suffix sums to find the optimal combination. Algorithm Steps The approach involves these steps − Create a map of left prefix sums with their indices For each ...

Read More

Program to count minimum deletions needed to make character frequencies unique in Python

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

Given a string s, we need to make it "good" by ensuring no two different characters have the same frequency. A good string has unique frequencies for each character. We must find the minimum number of character deletions required. For example, if s = "ssstttuu", we have 3 's', 3 't', and 2 'u' characters. Since both 's' and 't' have frequency 3, we need to delete characters to make frequencies unique. Approach The strategy is to count character frequencies, sort them, and reduce duplicate frequencies by deleting characters − Count ...

Read More

Program to count sorted vowel strings in Python

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

Suppose we have a number n, we have to find the number of strings of size n that consist only of vowels (a, e, i, o, u) and they are lexicographically sorted. We can say that a string s is lexicographically sorted when for all valid index i, s[i] is the same as or comes before s[i+1] in the alphabet. So, if the input is like n = 2, then the output will be 15 because there are many strings like ["aa", "ae", "ai", "ao", "au", "ee", "ei", "eo", "eu", "ii", "io", "iu", "oo", "ou", "uu"]. Understanding the ...

Read More
Showing 3021–3030 of 21,090 articles
« Prev 1 301 302 303 304 305 2109 Next »
Advertisements