Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 32 of 377

Program to find sum of digits that are inside one alphanumeric string in Python

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

Suppose we have an alphanumeric string with digits from "0" to "9" and lowercase English letters. We need to find the sum of all numbers present in the string. If digits are consecutive, they form a single number. For example, if the input is s = "hello25world63power86", the output will be 174 because 25 + 63 + 86 = 174. Algorithm To solve this problem, we follow these steps − Initialize ret = 0 (final sum) and curr = 0 (current number being built) For each character in the string: If character is a ...

Read More

Program to find the maximum sum of the subarray modulo by m in Python

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

Finding the maximum sum of a subarray modulo m is a challenging problem that requires tracking cumulative sums and using binary search for efficiency. This problem asks us to find the subarray with the largest sum when taken modulo m. Problem Understanding Given an array nums and integer m, we need to find the maximum value of any subarray sum modulo m. A subarray is a contiguous sequence of elements. For example, with nums = [1, 5, 7, 3] and m = 5: [1] mod 5 = 1 [5] mod 5 = 0 [7] mod ...

Read More

Program to check we can rearrange array to make difference between each pair of elements same in Python

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

Suppose we have a list called nums, we have to check whether we can rearrange the order of nums in such a way that the difference between every pair of consecutive two numbers is same. This is essentially checking if the array can form an arithmetic progression. So, if the input is like nums = [8, 2, 6, 4], then the output will be True, because if we rearrange nums like [2, 4, 6, 8], then the difference between every two pair of consecutive numbers is 2. Algorithm To solve this, we will follow these steps − N := size of nums if N

Read More

Program to add one to a number that is shown as a digit list in Python

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

Suppose we have an array called nums, containing decimal digits of a number. For example, [2, 5, 6] represents 256. We have to add 1 to this number and return the list in the same format as before. So, if the input is like nums = [2, 6, 9], then the output will be [2, 7, 0] (269 + 1 = 270). Algorithm Steps To solve this, we will follow these steps − i := size of nums - 1 while i >= 0, do if nums[i] + 1 = 0: if nums[i] + 1 = 0: if nums[i] + 1

Read More

Program to count number of 5-star reviews required to reach threshold percentage in Python

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

Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent. So, if the input is like reviews = [[3, 4], [1, 2], [4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% ...

Read More

Program to count number of similar substrings for each query in Python

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

Finding similar substrings based on character pattern matching is a common string processing problem. Two substrings are similar if they have the same length and maintain the same character relationship pattern. This means if characters at positions i and j are equal in one substring, they must be equal in the other substring too. Understanding Similar Substrings Two strings are similar if they follow these rules − They are of same length For each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] ...

Read More

Program to create a lexically minimal string from two strings in python

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

Creating a lexically minimal string from two strings involves comparing suffixes and selecting characters that produce the smallest lexicographical result. We compare remaining portions of both strings and choose the character from the string with the lexicographically smaller suffix. So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALS. Algorithm Steps If we compare the two strings step-by-step ? TUTORIALS vs POINT TUTORIALS vs OINT → P (choose from input_2) TUTORIALS vs INT → O (choose from input_2) TUTORIALS vs NT → I (choose from input_2) ...

Read More

Program to find out the number of shifts required to sort an array using insertion sort in python

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

Insertion sort moves elements one position at a time to place them in their correct sorted position. Each movement is called a shift. We need to count the total number of shifts required to sort an array using insertion sort. The key insight is that the number of shifts for each element equals the number of larger elements that appear before it in the array. This is called the inversion count. Example Walkthrough Given array [4, 5, 3, 1, 2], let's trace the insertion sort process ? Step 1: [4, 5, 3, 1, 2] → ...

Read More

Program to find largest color value in a directed graph in Python

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

Suppose we have a directed graph with n colored nodes and m different edges. The nodes are numbered from 0 to n-1. We have a string col with lowercase letters, where col[i] represents the color of the ith node in this graph (0-indexed). We also have an edge list where edges[j] = (u, v) represents there is an edge between u and v. A valid path in the graph is a sequence of nodes xi for all i from 1 to k, such that there is a directed edge from xi to xi+1. The color value of the path ...

Read More

Program to find minimum interval to include each query in Python

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

Given a list of intervals and queries, we need to find the smallest interval that contains each query value. For each query, we return the size of the smallest interval that contains it, or -1 if no such interval exists. Problem Understanding Each interval is represented as [left, right] where both endpoints are inclusive. For a query value q, we need to find the interval with minimum size where left ≤ q ≤ right. The size of an interval [left, right] is right - left + 1. Example If the input is intervals = [[2, 5], ...

Read More
Showing 311–320 of 3,768 articles
« Prev 1 30 31 32 33 34 377 Next »
Advertisements