Python Articles

Page 328 of 855

AngularJS – forEach() function

Mayank Agarwal
Mayank Agarwal
Updated on 26-Mar-2026 5K+ Views

The forEach() function in AngularJS uses an iterator to iterate over collections of items, objects, or arrays. The iterator function is called with three parameters: value, key, and obj. value represents the object property or array element key specifies the object property key or array element index obj represents the whole object reference Note that the forEach() function does not iterate over inherited properties. Syntax angular.forEach(obj, iterator, [context]) Parameters obj − The object or array to iterate over iterator − The function to be called for each item context ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 387 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 454 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

Program to find closest room from queries in Python

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

Given an array of rooms where rooms[i] = [roomId_i, size_i] and an array of queries where queries[j] = [preferred_j, minSize_j], we need to find the closest room for each query based on specific criteria. For each query, we need to find a room that: Has size of at least minSize_j |roomId - preferred_j| is minimized In case of tie, choose the room with smallest id Return -1 if no such room exists Example If rooms = [[2, 2], [1, 2], [3, 2]] and queries = [[3, 1], [3, 3], [5, 2]], the output is ...

Read More

Program to find maximum building height in Python

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

Suppose we have a value n and another list of pairs called restrictions. We want to build n new buildings in a city with specific constraints. Buildings are placed in a line and labeled from 1 to n. Each restriction has the format restrictions[i] = (id_i, max_height_i), indicating that building id_i must have height less than or equal to max_height_i. The city has the following height restrictions − The height of each building must be 0 or positive values. First building height must be 0. ...

Read More

Program to find XOR sum of all pairs bitwise AND in Python

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

Suppose we have two arrays arr1 and arr2. The XOR sum of a list is the bitwise XOR of all its elements. If the list has only one element, then its XOR sum will be the element itself. Now, consider a list containing the result of arr1[i] AND arr2[j] (bitwise AND) for every index pair (i, j) where 0 ≤ i < length of arr1 and 0 ≤ j < length of arr2. We have to find the XOR sum of that list. So, if the input is like arr1 = [5, 3, 4] and arr2 = [2, 6], ...

Read More

Program to find minimum number of operations to make string sorted in Python

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

Suppose we have a string s. We have to perform the following operation on s until we get a sorted string − Select largest index i such that 1 ≤ i < length of s and s[i] < s[i - 1]. Select largest index j such that i ≤ j < length of s and s[k] < s[i - 1] for all the possible values of k in the range [i, j] inclusive. Exchange two characters at indices i - 1 and j. ...

Read More

Program to find number of different subsequences GCDs in Python

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

Suppose we have an array nums with positive values. We have to find the number of different GCDs among all non-empty subsequences of nums. As we know, the GCD of a sequence of numbers is the greatest value that divides all the numbers in the sequence evenly. So, if the input is like nums = [4, 6, 18], then the output will be 4 because: gcd([4]) = 4 gcd([6]) = 6 gcd([18]) = 18 gcd([4, 6]) = 2 gcd([4, 18]) = 2 gcd([6, 18]) = 6 gcd([4, 6, 18]) = 2 So all different GCD ...

Read More

Program to find maximum number of groups getting fresh donuts in Python

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

Suppose we have a value batchSize and an array groups where groups[i] denotes that there is a group of groups[i] customers that will visit the shop. So there is a donuts shop that bakes donuts in batches of given batchSize. But they have one rule: they must serve all of the donuts of a batch before serving any donuts of the next batch. Each customer will get exactly one donut. When a group enters the shop, all customers of that group must be served before addressing any next groups. One group may be happy if they all get fresh ...

Read More

Program to maximize number of nice divisors in Python

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

Given a number pf representing the number of prime factors, we need to construct a positive number n that maximizes the number of nice divisors. A divisor is considered "nice" when it is divisible by every prime factor of n. Problem Requirements The number of prime factors of n (may or may not be distinct) is at most pf The number of nice divisors of n is maximized Return the count of nice divisors modulo 109 + 7 For example, if pf = 5, we can construct n = 200 with prime factors [2, 2, ...

Read More
Showing 3271–3280 of 8,546 articles
« Prev 1 326 327 328 329 330 855 Next »
Advertisements