Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 54 of 377

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 286 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 164 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 473 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 690 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

Program to check whether one string swap can make strings equal or not using Python

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

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

Program to check if binary string has at most one segment of ones or not using Python

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

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

Program to find nearest point that has the same x or y coordinate using Python

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

Finding the nearest point that shares the same x or y coordinate is a common problem in computational geometry. We need to find a valid point (one that shares either x or y coordinate) with the smallest Manhattan distance from our current location. The Manhattan distance between two points (a, b) and (p, q) is calculated as |a - p| + |b - q|. When multiple points have the same minimum distance, we return the one with the smallest index. Problem Statement Given a list of points and a current location, find the index of the valid ...

Read More

Program to count items matching a rule using Python

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

Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item matches the rule if one of the following is true − ruleKey = "type" and ruleValue = type_i. ruleKey = "color" and ruleValue = color_i. ruleKey = "name" and ruleValue = name_i. We have to find the ...

Read More
Showing 531–540 of 3,768 articles
« Prev 1 52 53 54 55 56 377 Next »
Advertisements