Python Articles

Page 149 of 855

Program to find number of optimal steps needed to reach destination by baby and giant steps in Python

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

Suppose we have a list of queries Q, where each query Q[i] contains a triplet [a_i, b_i, d_i]. Consider we are at position (0, 0) initially, then in one step we can move from some position (x1, y1) to (x2, y2) where Euclidean distance between these two points is at least a and at most b. Now for each query we have to find minimum number of steps needed to reach (d_i, 0) from (0, 0). For example, if the input is Q = [(2, 3, 1), (1, 2, 0), (3, 4, 11)], then the output will be [2, ...

Read More

Program to find removed term from arithmetic sequence in Python

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

Suppose we have an array called nums holding n-1 arithmetic sequence terms. One element except the first or last element was removed from the original sequence. We have to find the removed number. So, if the input is like nums = [5, 7, 11, 13], then the output will be 9 because the items follow an arithmetic sequence with common difference 2, and the missing term at index 2 should be 9. Algorithm To solve this, we will follow these steps ? If size of nums is 2, return the average ...

Read More

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 488 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 166 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 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
Showing 1481–1490 of 8,549 articles
« Prev 1 147 148 149 150 151 855 Next »
Advertisements