Programming Articles

Page 291 of 2547

Program to find list that shows closest distance of character c from that index in Python

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

Given a string s and a character c that exists in the string, we need to find the closest distance from each character position to any occurrence of character c. The result is a list where each element represents the minimum distance from that index to the nearest occurrence of c. For example, if s = "ppqppq" and c = "q", the output will be [2, 1, 0, 1, 1, 0] because: Index 0 ('p'): closest 'q' is at index 2, distance = 2 Index 1 ('p'): closest 'q' is at index 2, distance = 1 Index ...

Read More

Program to check two spheres can ever meet by accelerating or not in a 3D space in Python

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

Suppose there are two spheres with radius values r1 and r2. They are at coordinates (x1, y1, z1) and (x2, y2, z2). Their acceleration values are given as (ax1, ay1, az1) and (ax2, ay2, az2). We need to check whether these two spheres will ever meet in 3D space if they move with given accelerations. So, if the input is like r1 = 1, r2 = 2, pos1 = (0, 0, 0), acc1 = (100, 0, 0), pos2 = (4, 0, 0), acc2 = (0, 0, 0), then the output will be True, because the second sphere has no ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 677 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 341 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 264 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 793 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 884 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 434 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 219 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
Showing 2901–2910 of 25,466 articles
« Prev 1 289 290 291 292 293 2547 Next »
Advertisements