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
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
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
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
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
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
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
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
Given two strings of equal length, we need to determine if we can make them identical by performing at most one swap operation on exactly one of the strings. A swap operation exchanges two characters at any positions within a string. Problem Analysis For two strings to become equal with at most one swap, they must satisfy these conditions: If strings are already identical (0 differences) − return True If strings have exactly 2 differences − check if swapping makes them equal If strings have more than 2 differences − return False Both strings must contain ... Read More
Binary strings often need validation to check specific patterns. A common requirement is to verify if a binary string has at most one contiguous segment of ones. This means all the '1's should be grouped together without any '0's in between. For example, "111000" has one segment of ones, while "11010" has two separate segments. Problem Understanding Given a binary string without leading zeros, we need to determine if it contains at most one contiguous segment of ones. The string should have all '1's grouped together, followed by all '0's (if any). Examples "111000" ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance