Server Side Programming Articles

Page 298 of 2109

Program to find maximum erasure value in Python

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

Suppose we have an array called nums (with positive values only) and we want to erase a subarray containing unique elements. We will get a score that is the sum of subarray elements. We have to find the maximum score we can get by erasing exactly one subarray. So, if the input is like nums = [6, 3, 2, 3, 6, 3, 2, 3, 6], then the output will be 11, because the optimal subarray is either [6, 3, 2] or [2, 3, 6], so the sum is 11. Algorithm To solve this, we will follow these ...

Read More

Program to find sum of beauty of all substrings in Python

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

Given a string, we need to find the sum of beauty of all its substrings. The beauty of a string is the difference between the frequencies of the most frequent and least frequent characters. For example, if the string is "abaacc", the frequency of 'a' is 3 and 'b' is 1, so beauty = 3 - 1 = 2. Problem Example If the input string is "xxyzy", the substrings with non-zero beauty are: "xxy" → beauty = 2 - 1 = 1 "xxyz" → beauty = 2 - 1 = 1 "xxyzy" → beauty = ...

Read More

Program to find minimum difference of stone games score in Python

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

Suppose we have an array called stones where stones[i] represents the value of the ith stone from the left. Two friends Amal and Bimal are playing a turn-based game with these stones and Amal starts first always. There are n stones arranged in a row. Each player can remove either the leftmost stone or the rightmost stone from the row and get points equal to the sum of the remaining stones' values in the row. The player with the higher score wins. Bimal found that he will always lose this game, so he decided to minimize the score's difference. ...

Read More

Program to check whether number is a sum of powers of three in Python

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

Suppose we have a number n, we have to check whether it is possible to represent n as the sum of distinct powers of three or not. An integer y is said to be power of three if there exists an integer x such that y = 3^x. So, if the input is like n = 117, then the output will be True because 117 = 3^4 + 3^3 + 3^2 = 81 + 27 + 9. Algorithm To solve this, we will follow these steps ? for i in range 16 to 0, decrease ...

Read More

Program to partitioning into minimum number of Deci- Binary numbers in Python

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

Given a decimal number as a string, we need to find the minimum number of deci-binary numbers whose sum equals the given number. A deci-binary number contains only digits 0 and 1. For example, if the input is "132", the output will be 3 because 132 can be expressed as the sum of three deci-binary numbers: 100 + 11 + 21 = 132. Algorithm The key insight is that the minimum number of deci-binary numbers needed equals the maximum digit in the input number. This is because: Each deci-binary number can contribute at most 1 ...

Read More

Program to find winner of stone game in Python

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

Suppose Amal and Bimal are playing a stone game where Amal goes first. The game works as follows − There are n stones in a pile. Each player can take a stone and receive points based on their individual valuation of that stone. We have two arrays A_Values and B_Values where A_Values[i] and B_Values[i] represent how Amal and Bimal value the ith stone respectively. The player with the highest total score wins. If scores are equal, it's a draw. Both players play optimally and know each other's valuations. Return 1 if Amal wins, -1 if Bimal wins, and ...

Read More

Program to find maximum width ramp in Python

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

A ramp in an array is a pair of indices (i, j) where i < j and nums[i] ≤ nums[j]. The width of a ramp is the difference (j - i). We need to find the maximum width ramp in the given array. For example, in the array [6, 0, 8, 2, 1, 5], the maximum width ramp is at indices (1, 5) where nums[1] = 0 and nums[5] = 5, giving us a width of 4. Approach The solution uses a value-to-indices mapping approach ? Group indices by their values in a dictionary Process ...

Read More

Program to find sum of absolute differences in a sorted array in Python

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

Given a sorted array in non-decreasing order, we need to create a result array where each element contains the sum of absolute differences between that element and all other elements in the array. For example, with nums = [5, 7, 12], the result will be [9, 7, 12] because: |5−5| + |5−7| + |5−12| = 0+2+7 = 9 |7−5| + |7−7| + |7−12| = 2+0+5 = 7 |12−5| + |12−7| + |12−12| = 7+5+0 = 12 Naive Approach The straightforward approach calculates absolute differences for each element ? def solve_naive(nums): ...

Read More

Program to find equal sum arrays with minimum number of operations in Python

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

We are given two arrays nums1 and nums2 with values between 1 and 6 (inclusive). In one operation, we can update any value in either array to any value between 1 and 6. Our goal is to find the minimum number of operations needed to make the sum of values in nums1 equal to the sum of values in nums2. So, if the input is like nums1 = [1, 5, 6], nums2 = [4, 1, 1], then the output will be 2 because we can change nums2 from [4, 1, 1] to [4, 1, 6] in first operation, and ...

Read More

Program to find concatenation of consecutive binary numbers in Python

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

When working with binary representations, we often need to concatenate binary strings of consecutive numbers. This problem asks us to find the decimal value of a binary string formed by concatenating binary representations of numbers from 1 to n. So, if the input is like n = 4, then the output will be 220 because concatenating binary representations from 1 to 4 gives us "1" + "10" + "11" + "100" = "1101111100", which equals 220 in decimal. Algorithm Steps To solve this problem, we follow these steps − Initialize ans := 1 (binary representation ...

Read More
Showing 2971–2980 of 21,090 articles
« Prev 1 296 297 298 299 300 2109 Next »
Advertisements