Python Articles

Page 292 of 855

Program to find number of pairs where elements square is within the given range in Python

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

Given two lists of numbers nums1 and nums2, and two boundary values lower and upper, we need to find the number of pairs (i, j) such that lower ≤ nums1[i]² + nums2[j]² ≤ upper. Problem Understanding For example, if nums1 = [5, 3, 2], nums2 = [8, 12, 6], lower = 10, and upper = 50, we need to check all possible combinations: Pair (1, 2): 3² + 6² = 9 + 36 = 45 (10 ≤ 45 ≤ 50) ✓ Pair (2, 2): 2² + 6² = 4 + 36 = 40 (10 ≤ 40 ...

Read More

Program to check whether robot is moving inside a bounded box or not in Python

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

Suppose we have a string s that represents the moves of a robot. The robot starts at position (0, 0) and faces north. We need to determine if the robot stays within a bounded area after repeatedly executing the move sequence. Move Commands The move string s contains these characters ? "F" − move forward one unit in current direction "L" − rotate 90 degrees left "R" − rotate 90 degrees right Algorithm Logic If a robot returns to its starting position (0, 0) after executing the move sequence up to 4 times, ...

Read More

Program to find bitwise AND of range of numbers in given range in Python

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

Suppose we have two values start and end, we have to find the bitwise AND of all numbers in the range [start, end] (both inclusive). So, if the input is like start = 8 end = 12, then the output will be 8. Here's why: 8 is 1000 in binary and 12 is 1100 in binary, so 1000 AND 1001 AND 1010 AND 1011 AND 1100 is 1000 which is 8. Algorithm To solve this, we will follow these steps − n := end - start + 1 ...

Read More

Program to find number of sublists with sum k in a binary list in Python

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

Given a binary list containing only 0s and 1s, we need to find the number of contiguous sublists whose sum equals a given value k. This problem can be efficiently solved using the prefix sum technique with a hash map to track cumulative sums. Problem Understanding For a binary list nums = [1, 0, 0, 1, 1, 1, 0, 1] and k = 3, we need to find all contiguous sublists that sum to 3. The valid sublists are: [1, 0, 0, 1, 1] starting at index 0 [0, 0, 1, 1, 1] starting at index ...

Read More

Program to find list showing total distance required to move all balls to current position in Python

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

Suppose we have a binary list called nums containing only 0s and 1s where a 0 indicates empty cell and 1 indicates the cell is filled by a ball. We have to find a new list of say L, whose size is also same like nums size, where L[i] is set to the total distance required to move all the balls to L[i]. Here the distance to move a ball from index j to index i is |j - i|. So, if the input is like nums = [1, 1, 0, 1], then the output will be [4, 3, ...

Read More

Program to balance the direction string so that each direction occurred quarter times in Python

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

Suppose we have a string s with four directions "N", "S", "W" and "E" for North, South, West and East respectively. We have to find the size of the shortest substring we can update such that each of the four directions occur n/4 times each, where n is the size of string s. So, if the input is like s = "NNSWWESN", then the output will be 1. Here n is 8, so 8/4 is 2, so if we change the last N to E, all directions will be there twice. Algorithm To solve this, we will ...

Read More

Program to check how many queries finds valid arithmetic sequence in Python

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

Suppose we have a list of numbers called nums and also have a list of queries. Each query element contains [i, j] asking whether the sublist of nums from index i to j (both inclusive) forms an arithmetic sequence. We need to count how many queries return true. An arithmetic sequence has a constant difference between consecutive elements. For example, [2, 4, 6, 8] has a common difference of 2. Example If the input is nums = [2, 4, 6, 8, 7, 6, 5, 2] and queries = [[3, 4], [0, 3], [2, 4]], then the output ...

Read More

Program to find maximum number of people we can make happy in Python

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

Sometimes we need to maximize the number of happy customers in a store by strategically changing their moods. This problem involves finding the optimal sublist of customers to make happy using a sliding window approach. Problem Statement We have two lists of equal length: customers and mood, plus an integer k. At minute i, customers[i] people enter the store. When mood[i] = 1, customers are happy; when mood[i] = 0, they are sad. We can change at most k consecutive sad customers to happy and find the maximum total happy customers. Example If customers = [2, ...

Read More

Program to check string is palindrome or not with equivalent pairs in Python

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

Suppose we have a lowercase alphabet string called s and also have a list of pairs called pairs. Each element in pairs has two strings [a, b] where the character 'a' and 'b' are considered equivalent. If there are two pairs like [a, b] and [b, c], then we can say a and b are equivalent, also b and c are equivalent, so a and c are also equivalent through transitivity. Any character is equivalent to itself. We need to check whether s is a palindrome or not with the given equivalence relations. So, if the input is like ...

Read More

Program to check whether there is any pair of words which are almost same in Python

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

Suppose we have a list of lowercase strings called words where each word is of same length. We have to check whether there are two strings that differ only in one character. So, if the input is like words = ["seed", "pick", "lick", "root", "live"], then the output will be True, as "pick" and "lick" are almost same. Algorithm To solve this, we will follow these steps − Create a new set to store patterns For each word in words, do ...

Read More
Showing 2911–2920 of 8,546 articles
« Prev 1 290 291 292 293 294 855 Next »
Advertisements