Server Side Programming Articles

Page 369 of 2109

Program to find maximum population year using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 670 Views

Given a table with birth and death years of people, we need to find the earliest year with the maximum population. A person is counted as alive during year y if y is in the range [birth, death - 1] (they are not counted in their death year). For example, with the following data: Birth Death 1970 2010 1960 2020 1940 1970 We need to find the year when the most people were alive simultaneously. Algorithm We will use a dictionary to count the population ...

Read More

Program to find minimum distance to the target element using Python

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

We need to find the minimum distance from a start position to any occurrence of a target element in an array. The distance is calculated as the absolute difference between indices. Given an array nums, a target value that exists in the array, and a start index, we find the index i where nums[i] = target and |i - start| is minimized. Example If nums = [3, 4, 5, 6, 7], target = 7, and start = 2, the target 7 is found at index 4. The distance is |4 - 2| = 2. Algorithm ...

Read More

Program to replace all digits with characters using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

Suppose we have an alphanumeric string that contains lowercase English letters in its even positions and digits in its odd positions. We need to perform a shift operation where each digit at odd position i gets replaced with the character obtained by shifting the letter at position i-1 by the digit value. The shift operation works as follows: shift('p', 5) = 'u' and shift('a', 0) = 'a'. For every odd index i, we replace the digit s[i] with shift(s[i-1], s[i]). Problem Example If the input is s = "a2b1d4f3h2", then the output will be "acbcdhfihj" because ? ...

Read More

Program to find sum of digits in base K using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 807 Views

Converting a number from base 10 to base K and finding the sum of its digits is a common programming problem. We need to repeatedly divide the number by K and sum up all the remainders. So, if the input is like n = 985 k = 8, then the output will be 12 because the number 985 in octal is 1731, so the digit sum is 1+7+3+1 = 12. Algorithm To solve this, we will follow these steps ? Initialize ans := 0 while n >= k, do ans := ans + n ...

Read More

Program to find sign of the product of an array using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 288 Views

Finding the sign of a product without actually computing the multiplication is an efficient approach that avoids overflow issues. We can determine the result by counting zeros and negative numbers in the array. Algorithm The key insight is that we don't need to calculate the actual product. Instead ? If any element is zero, the product is zero If there's an even number of negative elements, the product is positive If there's an odd number of negative elements, the product is negative Example def find_product_sign(nums): zeroes = 0 ...

Read More

Program to find k partitions after truncating sentence using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 169 Views

Suppose we have a sentence s containing English words separated by single spaces with no leading or trailing spaces. We also have a value k. We need to find only the first k words after truncating the sentence. So, if the input is like s = "Coding challenges are really helpful for students" and k = 5, then the output will be "Coding challenges are really helpful". Algorithm To solve this, we will follow these steps − Split the sentence s by spaces to get individual words Take the first k words from the words ...

Read More

Program to determine color of a chessboard square using Python

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

Suppose we have a chessboard coordinate, that is a string that represents the coordinates of row and column of the chessboard. Below is a chessboard for your reference. ...

Read More

Program to find number of different integers in a string using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 480 Views

When working with alphanumeric strings, we often need to extract and count distinct integers. This involves replacing non-digit characters with spaces and identifying unique numbers, where leading zeros don't affect the integer value. Given a lowercase alphanumeric string, we need to replace every non-digit character with a space, leaving integers separated by spaces. Two numbers are considered the same if their decimal representations without leading zeros are identical. Example If the input string is s = "ab12fg012th5er67", we extract numbers ["12", "012", "5", "67"]. Since "12" and "012" represent the same integer value, we have 3 distinct ...

Read More

Program to find maximum ascending subarray sum using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 698 Views

Suppose we have an array of positive values called nums, we have to find the maximum possible sum of an ascending subarray in nums. We can say a subarray [nums_l, nums_l+1, ..., nums_r-1, nums_r] is ascending when for all i where l nums[i-1], then current_sum := current_sum + nums[i] ...

Read More

Program to find second largest digit in a string using Python

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

When working with alphanumeric strings, we often need to extract and analyze the numerical digits. This program finds the second largest digit in a string, returning -1 if there aren't enough unique digits. So, if the input is like s = "p84t3ho1n", then the output will be 4 as the digits are [1,3,4,8], so second largest digit is 4. Algorithm To solve this, we will follow these steps − Create a set to store unique digits For each character in the string, do If character is a digit, add it to the set as integer If size of set

Read More
Showing 3681–3690 of 21,090 articles
« Prev 1 367 368 369 370 371 2109 Next »
Advertisements