Server Side Programming Articles

Page 289 of 2109

Program to find minimum number of operations to make string sorted in Python

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

Suppose we have a string s. We have to perform the following operation on s until we get a sorted string − Select largest index i such that 1 ≤ i < length of s and s[i] < s[i - 1]. Select largest index j such that i ≤ j < length of s and s[k] < s[i - 1] for all the possible values of k in the range [i, j] inclusive. Exchange two characters at indices i - 1 and j. ...

Read More

Program to find number of different subsequences GCDs in Python

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

Suppose we have an array nums with positive values. We have to find the number of different GCDs among all non-empty subsequences of nums. As we know, the GCD of a sequence of numbers is the greatest value that divides all the numbers in the sequence evenly. So, if the input is like nums = [4, 6, 18], then the output will be 4 because: gcd([4]) = 4 gcd([6]) = 6 gcd([18]) = 18 gcd([4, 6]) = 2 gcd([4, 18]) = 2 gcd([6, 18]) = 6 gcd([4, 6, 18]) = 2 So all different GCD ...

Read More

Program to find maximum number of groups getting fresh donuts in Python

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

Suppose we have a value batchSize and an array groups where groups[i] denotes that there is a group of groups[i] customers that will visit the shop. So there is a donuts shop that bakes donuts in batches of given batchSize. But they have one rule: they must serve all of the donuts of a batch before serving any donuts of the next batch. Each customer will get exactly one donut. When a group enters the shop, all customers of that group must be served before addressing any next groups. One group may be happy if they all get fresh ...

Read More

Program to maximize number of nice divisors in Python

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

Given a number pf representing the number of prime factors, we need to construct a positive number n that maximizes the number of nice divisors. A divisor is considered "nice" when it is divisible by every prime factor of n. Problem Requirements The number of prime factors of n (may or may not be distinct) is at most pf The number of nice divisors of n is maximized Return the count of nice divisors modulo 109 + 7 For example, if pf = 5, we can construct n = 200 with prime factors [2, 2, ...

Read More

Program to count pairs with XOR in a range in Python

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

Suppose we have an array nums and two values l and r. We need to find the number of nice pairs, where a nice pair is (i, j) such that 0 >= 1 return result // 2 # Divide by 2 to avoid double counting return count_pairs_less_than(nums, r + 1) - count_pairs_less_than(nums, l) # Example usage nums = [4, 1, 7, 2] l = 2 r = 6 result = count_pairs_with_xor_in_range(nums, l, ...

Read More

Program to find maximize score after n operations in Python

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

We have an array nums of size 2*n and need to perform n operations. In each operation i (1-indexed), we select two elements x and y, get a score of i * gcd(x, y), and remove both elements. The goal is to maximize the total score. Algorithm Approach We use dynamic programming with bitmasks to solve this problem. The key insight is that we need to try all possible pairs at each step and choose the combination that gives maximum score ? Use a bitmask to represent which elements are already used For each state, try ...

Read More

Program to find out the maximum value of a 'valid' array in Python

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

Suppose we have an array of n integers 'nums'. Each value in 'nums' represents its 'power'. The array will be evaluated as 'valid' if the length of the array is greater than two and the first and last values of the array are equal. We have to make the array valid by deleting elements from the array so that the remaining elements satisfy the condition. As output, we return the maximum possible power value by adding all the power values of the valid array. So, if the input is like nums = [3, 4, 5, 3, 4], then the ...

Read More

Program to find maximum score of a good subarray in Python

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

Suppose we have an array called nums and a value k. The score of a subarray (i, j) is defined as the minimum value in subarray nums[i..j] multiplied by its length (j-i+1). A good subarray is one where i = minNum while i > -1 and nums[i] >= minNum: i -= 1 # Expand right while elements are >= minNum ...

Read More

Program to make the XOR of all segments equal to zero in Python

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

Suppose we have an array called nums and another value k. The XOR of a segment [left, right] (left 0: for j, prev in enumerate(dp): new_dp[i ^ j] = max(new_dp[i ^ j], prev + cnt) dp = new_dp ...

Read More

Program to find max chunks to make array sorted in Python

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

Suppose we have an array nums, we have to split the array into some number of partitions, and individually sort each of them. After concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made? So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5]. Algorithm To solve this, we will follow these steps − real := sort the list nums p1 := 0, p2 := 1, ...

Read More
Showing 2881–2890 of 21,090 articles
« Prev 1 287 288 289 290 291 2109 Next »
Advertisements